Skip to content
Source
Sign inSign up
Open History to see the last commit

threadStatusAnnouncement.test.ts

82 lines2.4 KB

1import {
2 nextThreadAnnouncement,
3 type ThreadAnnouncementMemory,
4} from "@t3tools/client-runtime/state/thread-activity-announcement";
5import { RunId } from "@t3tools/contracts";
6import { describe, expect, it } from "vite-plus/test";
7​
8import {
9 nextThreadActivitySnapshot,
10 type ThreadActivityInput,
11 type ThreadActivityMemory,
12} from "./threadStatusAnnouncement";
13​
14const run = RunId.make("run-1");
15​
16/** A frame of a working turn: what the question card shows and what the shell holds. */
17function frame(card: string | null, shell: string | null): ThreadActivityInput {
18 return {
19 approvalRequestId: null,
20 userInputRequestId: card,
21 shellRequestId: shell,
22 shellPendingUserInput: shell !== null,
23 working: true,
24 run: { runId: run, status: "running" },
25 };
27​
28const idle: ThreadActivityInput = { ...frame(null, null), working: false, run: null };
29const done: ThreadActivityInput = {
30 ...frame(null, null),
31 working: false,
32 run: { runId: run, status: "completed" },
33};
34​
35function announcements(frames: ReadonlyArray<ThreadActivityInput>): ReadonlyArray<string | null> {
36 let activity: ThreadActivityMemory | null = null;
37 let memory: ThreadAnnouncementMemory | null = null;
38 return frames.map((input) => {
39 activity = nextThreadActivitySnapshot(activity, input);
40 const next = nextThreadAnnouncement(memory, activity.snapshot);
41 memory = next.memory;
42 return next.announcement;
43 });
45​
46describe("nextThreadActivitySnapshot", () => {
47 it("says each question once while the card and the shell catch up at different times", () => {
48 expect(
49 announcements([
50 idle,
51 frame(null, null),
52 // The card shows question A before the shell has it.
53 frame("a", null),
54 frame("a", "a"),
55 // Answered: the card hides A at once, the shell still holds it.
56 frame(null, "a"),
57 // Question B reaches the card while the shell still holds A, and is answered before the shell has B.
58 frame("b", "a"),
59 frame(null, "a"),
60 frame(null, "b"),
61 frame(null, null),
62 // A secret request shows only in the shell.
63 frame(null, "secret"),
64 frame(null, null),
65 done,
66 ]),
67 ).toEqual([
68 null,
69 "Working",
70 "Needs your input",
71 null,
72 null,
73 "Needs your input",
74 null,
75 null,
76 null,
77 "Needs your input",
78 null,
79 "Done",
80 ]);
81 });
82});