| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "reasonix/internal/control" |
| 9 | "reasonix/internal/event" |
| 10 | ) |
| 11 | |
| 12 | func TestReplayPendingPromptsForTabDoesNotReplaySiblingAsk(t *testing.T) { |
| 13 | type askHarness struct { |
| 14 | ctrl *control.Controller |
| 15 | events chan event.Ask |
| 16 | cancel context.CancelFunc |
| 17 | done chan struct{} |
| 18 | } |
| 19 | newHarness := func(label string) askHarness { |
| 20 | events := make(chan event.Ask, 4) |
| 21 | ctrl := control.New(control.Options{ |
| 22 | Label: label, |
| 23 | Sink: event.FuncSink(func(e event.Event) { |
| 24 | if e.Kind == event.AskRequest { |
| 25 | events <- e.Ask |
| 26 | } |
| 27 | }), |
| 28 | }) |
| 29 | ctx, cancel := context.WithCancel(t.Context()) |
| 30 | done := make(chan struct{}) |
| 31 | go func() { |
| 32 | defer close(done) |
| 33 | _, _ = ctrl.Ask(ctx, []event.AskQuestion{{ID: "choice", Prompt: "Pick one"}}) |
| 34 | }() |
| 35 | return askHarness{ctrl: ctrl, events: events, cancel: cancel, done: done} |
| 36 | } |
| 37 | waitAsk := func(label string, events <-chan event.Ask) event.Ask { |
| 38 | t.Helper() |
| 39 | select { |
| 40 | case ask := <-events: |
| 41 | return ask |
| 42 | case <-time.After(2 * time.Second): |
| 43 | t.Fatalf("timed out waiting for %s ask", label) |
| 44 | return event.Ask{} |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | tabA := newHarness("tab-a") |
| 49 | tabB := newHarness("tab-b") |
| 50 | defer func() { |
| 51 | tabA.cancel() |
| 52 | tabB.cancel() |
| 53 | <-tabA.done |
| 54 | <-tabB.done |
| 55 | tabA.ctrl.Close() |
| 56 | tabB.ctrl.Close() |
| 57 | }() |
| 58 | waitAsk("initial tab-a", tabA.events) |
| 59 | waitAsk("initial tab-b", tabB.events) |
| 60 | |
| 61 | app := NewApp() |
| 62 | app.tabs = map[string]*WorkspaceTab{ |
| 63 | "tab-a": {ID: "tab-a", Ctrl: tabA.ctrl, Ready: true}, |
| 64 | "tab-b": {ID: "tab-b", Ctrl: tabB.ctrl, Ready: true}, |
| 65 | } |
| 66 | app.tabOrder = []string{"tab-a", "tab-b"} |
| 67 | app.activeTabID = "tab-a" |
| 68 | |
| 69 | app.ReplayPendingPromptsForTab("tab-b") |
| 70 | if got := waitAsk("replayed tab-b", tabB.events); got.ID == "" { |
| 71 | t.Fatal("tab-b replay returned an empty ask") |
| 72 | } |
| 73 | select { |
| 74 | case got := <-tabA.events: |
| 75 | t.Fatalf("scoped tab-b replay also emitted tab-a ask: %+v", got) |
| 76 | default: |
| 77 | } |
| 78 | } |
| 79 |