| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/event" |
| 9 | ) |
| 10 | |
| 11 | func TestInboxExpectedSessionCannotSubmitOrConfirmReplacement(t *testing.T) { |
| 12 | dir := t.TempDir() |
| 13 | first, second := filepath.Join(dir, "first.jsonl"), filepath.Join(dir, "second.jsonl") |
| 14 | c := newOwnedTestController(t, Options{SessionDir: dir, SessionPath: first, Sink: event.Discard}) |
| 15 | defer c.Close() |
| 16 | if err := c.SetInboxPaused(true); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | request := InboxRequest{ExpectedSessionPath: first, Submit: "original", Idempotency: "original"} |
| 20 | receipt, err := c.TryEnqueueFollowup(request) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | confirmed, found, err := c.LookupInboxReceiptForSession(first, request.Idempotency) |
| 25 | if err != nil || !found || confirmed.ItemID != receipt.ItemID { |
| 26 | t.Fatalf("original confirmation = %+v, %v, %v", confirmed, found, err) |
| 27 | } |
| 28 | c.SetSessionPath(second) |
| 29 | if err := c.SetInboxPaused(true); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if _, err := c.TryEnqueueFollowup(request); !errors.Is(err, ErrInboxSessionChanged) { |
| 33 | t.Fatalf("stale request = %v", err) |
| 34 | } |
| 35 | if _, _, err := c.LookupInboxReceiptForSession(first, request.Idempotency); !errors.Is(err, ErrInboxSessionChanged) { |
| 36 | t.Fatalf("stale lookup = %v", err) |
| 37 | } |
| 38 | if got := c.InboxSnapshot(); got.SessionPath != second || len(got.Items) != 0 { |
| 39 | t.Fatalf("replacement mutated: %+v", got) |
| 40 | } |
| 41 | request.ExpectedSessionPath = "" |
| 42 | if _, err := c.TryEnqueueFollowup(request); err != nil { |
| 43 | t.Fatalf("legacy request no longer works: %v", err) |
| 44 | } |
| 45 | } |
| 46 |