Files
archie/internal/domain/workflow/store_integration_test.go
T
sam 5e94cef784 refactor(workflow): relocate Task/Status/Source and define workflow.Store (#754)
* refactor(workflow,store): relocate Task/Status/Source and define workflow.Store

Move store.Task, store.Status*/store.Source*, and the 3-method workflow
store interface out of internal/store into internal/domain/workflow as
workflow.Task/workflow.Status/workflow.Source plus a consumer-owned
workflow.Store, superseding store.WorkflowStore. internal/domain/workflow
now imports zero internal/store symbols; internal/store persists the
domain type instead (infrastructure depends on domain), and its Store
satisfies workflow.Store.

Updates every production and test caller of the relocated symbols across
daemon, webui, gateway, agentworker, agenttransport/nats, storerpc,
taskrun, and worktreerpc, and removes TestRecordDispatchViaExplicitTx per
docs/prds/state-store-contract.md (rev. 2c) sections 4 and 12 step 2.

Two workflow-package integration tests that exercise the real *store.Store
move to an external workflow_test package (store_integration_test.go) to
avoid a workflow(test)->store->workflow(prod) import cycle now that store
depends on workflow.

* no-mistakes(document): docs(state-store): mark §4.8 relocation done in state-store-contract
2026-09-07 00:39:26 +10:00

108 lines
3.4 KiB
Go

package workflow_test
import (
"context"
"log/slog"
"path/filepath"
"testing"
"github.com/samcharles93/archie-core/internal/agentexec"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/store"
)
// storeIntegrationRunner is a minimal agentexec.Runner stub, duplicated
// here (rather than reused from the in-package tests) because this file
// lives in the external workflow_test package: it needs the real
// *store.Store as a workflow.Store, and the package under test now owns
// workflow.Task/Status/Source, which would otherwise cycle back through
// internal/store's dependency on internal/domain/workflow.
type storeIntegrationRunner struct {
result agentexec.Result
}
func (r *storeIntegrationRunner) Run(
_ context.Context, _ string, req agentexec.Request, _ agentexec.ToolCallReporter,
) (agentexec.Result, error) {
res := r.result
if res.Version == 0 {
res.Version = agentexec.ProtocolVersion
}
res.TaskID = req.TaskID
res.Attempt = req.Attempt
res.Stage = req.Stage
return res, nil
}
func TestAgentStagePersistsReturnedNotes(t *testing.T) {
st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "archie.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := st.Close(); err != nil {
t.Errorf("close store: %v", err)
}
})
ctx := context.Background()
if _, err := st.EnqueueIssue(ctx, "owner", "repo", 1, "title", "body", "", ""); err != nil {
t.Fatal(err)
}
task, err := st.ClaimNext(ctx)
if err != nil {
t.Fatal(err)
}
runner := &storeIntegrationRunner{result: agentexec.Result{
Version: agentexec.ProtocolVersion, Status: agentexec.StatusPassed,
AppendedNotes: []string{"checked with go test"},
}}
stage := workflow.AgentStage{
Name: "build", Role: "builder", Mission: func(*workflow.TaskContext) string { return "build" },
}.Stage()
tc := &workflow.TaskContext{
Task: task, Store: st, Agent: runner, Log: slog.New(slog.DiscardHandler),
Cfg: config.Config{Models: map[string]string{"builder": "provider/model"}},
}
if err := stage.Run(ctx, tc); err != nil {
t.Fatal(err)
}
if task.Notes != "- checked with go test\n" {
t.Fatalf("task notes = %q", task.Notes)
}
}
func TestRunLeavesInterruptedTaskForCrashRecovery(t *testing.T) {
st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "archie.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := st.Close(); err != nil {
t.Errorf("close store: %v", err)
}
})
if _, err := st.EnqueueIssue(context.Background(), "owner", "repo", 2, "title", "body", "", ""); err != nil {
t.Fatal(err)
}
task, err := st.ClaimNext(context.Background())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
workflow.Run(ctx, workflow.Workflow{Name: "test", Stages: []workflow.Stage{{
Name: "agent", Run: func(ctx context.Context, _ *workflow.TaskContext) error { return ctx.Err() },
}}}, &workflow.TaskContext{
Task: task, Store: st, Repo: config.Repo{Owner: "owner", Name: "repo"},
Log: slog.New(slog.DiscardHandler),
})
got, err := st.TaskByIssue(context.Background(), "owner", "repo", 2)
if err != nil {
t.Fatal(err)
}
if got.Status != workflow.StatusRunning || got.ParkReason != "" {
t.Fatalf("interrupted task status=%q park_reason=%q, want running with no park reason", got.Status, got.ParkReason)
}
}