package offscreen import ( "context" "errors" "testing" "time" "keel/internal/ai" "keel/internal/harness" "keel/internal/memory" "keel/internal/tasks" ) // stubBackend is a local fake ai.Backend; ai's package-internal fake is not // reachable from this package, so we define our own here. type stubBackend struct { out string err error } func (b stubBackend) Name() string { return "stub" } func (b stubBackend) Run(context.Context, string) (string, error) { return b.out, b.err } type fakeTasks struct { created []string } func (f *fakeTasks) Today(context.Context) ([]tasks.Task, error) { return nil, nil } func (f *fakeTasks) Create(_ context.Context, t tasks.Task) error { f.created = append(f.created, t.Title) return nil } func newTestMode(t *testing.T, ft *fakeTasks, backend ai.Backend) *Mode { t.Helper() svc := harness.Services{ AI: ai.NewService(backend), Tasks: ft, Clock: func() time.Time { return time.Unix(0, 0) }, Notify: func() {}, } return New(svc) } // waitStatus polls the mode's View until status reaches want, or fails after 2s. func waitStatus(t *testing.T, m *Mode, want string) { t.Helper() deadline := time.Now().Add(2 * time.Second) for time.Now().Before(deadline) { v, _ := m.View().(map[string]any) if v != nil && v["status"] == want { return } time.Sleep(5 * time.Millisecond) } t.Fatalf("status never reached %q (last view: %+v)", want, m.View()) } func okBackend() stubBackend { return stubBackend{out: `{"next_action":"call mum","rationale":"people goal"}`} } func TestConfirmFilesExactlyOneTask(t *testing.T) { ft := &fakeTasks{} m := newTestMode(t, ft, okBackend()) if err := m.Command(context.Background(), "start", nil); err != nil { t.Fatalf("start: %v", err) } waitStatus(t, m, statusProposed) if err := m.Command(context.Background(), "confirm", nil); err != nil { t.Fatalf("confirm: %v", err) } if len(ft.created) != 1 { t.Fatalf("created %d tasks, want 1", len(ft.created)) } if ft.created[0] != "call mum" { t.Fatalf("created task title = %q, want %q", ft.created[0], "call mum") } if m.Active() { t.Fatal("mode should be done after confirm") } } func TestDismissWritesNothing(t *testing.T) { ft := &fakeTasks{} m := newTestMode(t, ft, okBackend()) if err := m.Command(context.Background(), "start", nil); err != nil { t.Fatalf("start: %v", err) } waitStatus(t, m, statusProposed) if err := m.Command(context.Background(), "dismiss", nil); err != nil { t.Fatalf("dismiss: %v", err) } if len(ft.created) != 0 { t.Fatalf("dismiss created %d tasks, want 0", len(ft.created)) } if m.Active() { t.Fatal("mode should be done after dismiss") } } func TestProposeErrorSurfacesInView(t *testing.T) { ft := &fakeTasks{} m := newTestMode(t, ft, stubBackend{err: errors.New("boom")}) if err := m.Command(context.Background(), "start", nil); err != nil { t.Fatalf("start: %v", err) } waitStatus(t, m, statusError) v, _ := m.View().(map[string]any) if v == nil || v["error"] == nil || v["error"] == "" { t.Fatalf("expected error field in view, got %+v", v) } if !m.Active() { t.Fatal("mode should stay active on error so the user can retry or dismiss") } } func TestConfirmWithoutProposalErrors(t *testing.T) { ft := &fakeTasks{} m := newTestMode(t, ft, okBackend()) // No start → no proposal. if err := m.Command(context.Background(), "confirm", nil); err == nil { t.Fatal("confirm without a proposal should error") } if len(ft.created) != 0 { t.Fatalf("confirm without proposal created %d tasks, want 0", len(ft.created)) } } func TestUnknownCommandErrors(t *testing.T) { m := newTestMode(t, &fakeTasks{}, okBackend()) if err := m.Command(context.Background(), "bogus", nil); err == nil { t.Fatal("unknown command should error") } } // newTestModeMem is newTestMode with an injected in-memory Store. func newTestModeMem(t *testing.T, ft *fakeTasks, backend ai.Backend, mem *memory.Fake) *Mode { t.Helper() svc := harness.Services{ AI: ai.NewService(backend), Tasks: ft, Memory: mem, Clock: func() time.Time { return time.Unix(1000, 0) }, Notify: func() {}, } return New(svc) } // waitEvent polls the fake until an event of kind appears, or fails after 2s. func waitEvent(t *testing.T, f *memory.Fake, kind string) memory.Event { t.Helper() deadline := time.Now().Add(2 * time.Second) for time.Now().Before(deadline) { for _, e := range f.Events() { if e.Kind == kind { return e } } time.Sleep(5 * time.Millisecond) } t.Fatalf("event %q never recorded (events: %+v)", kind, f.Events()) return memory.Event{} } func TestProposeRecordsProposalMade(t *testing.T) { mem := memory.NewFake() m := newTestModeMem(t, &fakeTasks{}, okBackend(), mem) if err := m.Command(context.Background(), "start", nil); err != nil { t.Fatalf("start: %v", err) } waitStatus(t, m, statusProposed) ev := waitEvent(t, mem, "proposal_made") if ev.Data["next_action"] != "call mum" || ev.Data["proposal_id"] == "" { t.Fatalf("proposal_made data = %+v", ev.Data) } } func TestConfirmRecordsActionTakenWithSameID(t *testing.T) { mem := memory.NewFake() m := newTestModeMem(t, &fakeTasks{}, okBackend(), mem) _ = m.Command(context.Background(), "start", nil) waitStatus(t, m, statusProposed) made := waitEvent(t, mem, "proposal_made") if err := m.Command(context.Background(), "confirm", nil); err != nil { t.Fatalf("confirm: %v", err) } taken := waitEvent(t, mem, "action_taken") if taken.Data["proposal_id"] != made.Data["proposal_id"] { t.Fatalf("action_taken id %v != proposal_made id %v", taken.Data["proposal_id"], made.Data["proposal_id"]) } } func TestDismissRecordsProposalDismissed(t *testing.T) { mem := memory.NewFake() m := newTestModeMem(t, &fakeTasks{}, okBackend(), mem) _ = m.Command(context.Background(), "start", nil) waitStatus(t, m, statusProposed) _ = waitEvent(t, mem, "proposal_made") if err := m.Command(context.Background(), "dismiss", nil); err != nil { t.Fatalf("dismiss: %v", err) } _ = waitEvent(t, mem, "proposal_dismissed") }