The best way to control Claude Code, Codex and more from any computer or phone

main
ts 157 lines 5.92 KB
Raw
1 import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2 import { describe, expect, it } from "vite-plus/test";
3
4 import { appAtomRegistry } from "../../state/atom-registry";
5 import {
6 INITIAL_NEW_TASK_FLOW_STATE,
7 newTaskFlowKey,
8 newTaskFlowStateAtom,
9 pruneNewTaskFlowStates,
10 readNewTaskFlowState,
11 resolveRouteDraft,
12 stampedEnvironmentProjectsLoading,
13 updateNewTaskFlowState,
14 } from "./new-task-flow-state";
15
16 function draftRoute(key: string, draftId: string) {
17 return { key, name: "Thread", params: { draft: { draftId } } };
18 }
19
20 describe("new task flow state", () => {
21 it("keeps a covered draft route's flow while another draft route is pushed above it", () => {
22 const routeA = draftRoute("route-a", "draft-a");
23 const routeB = draftRoute("route-b", "draft-b");
24 const flowA = newTaskFlowKey(routeA)!;
25 const flowB = newTaskFlowKey(routeB)!;
26
27 // The provider subscribes to A, writes its selection, then moves to B.
28 const unmountA = appAtomRegistry.mount(newTaskFlowStateAtom(flowA));
29 updateNewTaskFlowState(flowA, { projectKey: "env:project-a", draftKey: "draft-a" });
30 unmountA();
31 const unmountB = appAtomRegistry.mount(newTaskFlowStateAtom(flowB));
32 pruneNewTaskFlowStates({ routes: [{ key: "home", name: "Home" }, routeA, routeB] });
33
34 expect(readNewTaskFlowState(flowB)).toBe(INITIAL_NEW_TASK_FLOW_STATE);
35 expect(appAtomRegistry.get(newTaskFlowStateAtom(flowA))).toMatchObject({
36 projectKey: "env:project-a",
37 draftKey: "draft-a",
38 });
39
40 // Popping B returns to A with its state intact.
41 unmountB();
42 pruneNewTaskFlowStates({ routes: [{ key: "home", name: "Home" }, routeA] });
43 expect(readNewTaskFlowState(flowA).projectKey).toBe("env:project-a");
44 });
45
46 it("drops a flow when its route is popped or promoted to a live thread", () => {
47 const routeA = draftRoute("route-a", "draft-a");
48 const routeB = draftRoute("route-b", "draft-b");
49 updateNewTaskFlowState(newTaskFlowKey(routeA)!, { projectKey: "env:project-a" });
50 updateNewTaskFlowState(newTaskFlowKey(routeB)!, { projectKey: "env:project-b" });
51
52 pruneNewTaskFlowStates({
53 routes: [{ key: "route-a", name: "Thread", params: { environmentId: "env", threadId: "t" } }],
54 });
55
56 expect(readNewTaskFlowState(newTaskFlowKey(routeA)!)).toBe(INITIAL_NEW_TASK_FLOW_STATE);
57 expect(readNewTaskFlowState(newTaskFlowKey(routeB)!)).toBe(INITIAL_NEW_TASK_FLOW_STATE);
58 });
59 });
60
61 describe("resolveRouteDraft", () => {
62 const environmentId = EnvironmentId.make("env");
63 const projectId = ProjectId.make("project");
64 const stamp = { environmentId, projectId };
65 const project = { environmentId, id: projectId };
66 const base = {
67 flow: INITIAL_NEW_TASK_FLOW_STATE,
68 routeDraftId: "new-task:draft-1",
69 draftsLoaded: true,
70 stamp,
71 projects: [project],
72 stampedEnvironmentLoading: false,
73 };
74
75 it("binds the route's draft and its stamped project", () => {
76 expect(resolveRouteDraft(base)).toEqual({
77 kind: "open",
78 flow: {
79 ...INITIAL_NEW_TASK_FLOW_STATE,
80 environmentId,
81 projectKey: "env:project",
82 draftKey: "new-task:draft-1",
83 },
84 });
85 });
86
87 it("leaves a flow that already bound a draft alone", () => {
88 const flow = { ...INITIAL_NEW_TASK_FLOW_STATE, draftKey: "new-task:other" };
89 expect(resolveRouteDraft({ ...base, flow })).toEqual({ kind: "none" });
90 });
91
92 it("waits for hydration, and for the stamped environment's projects", () => {
93 expect(resolveRouteDraft({ ...base, draftsLoaded: false, stamp: undefined })).toEqual({
94 kind: "pending",
95 });
96 expect(resolveRouteDraft({ ...base, projects: [], stampedEnvironmentLoading: true })).toEqual({
97 kind: "pending",
98 });
99 });
100
101 it("waits for a slower environment's projects while another environment's have loaded", () => {
102 const environmentB = EnvironmentId.make("env-b");
103 const projectB = { environmentId: environmentB, id: ProjectId.make("project-b") };
104 const stampB = { environmentId: environmentB, projectId: projectB.id };
105
106 expect(resolveRouteDraft({ ...base, stamp: stampB, stampedEnvironmentLoading: true })).toEqual({
107 kind: "pending",
108 });
109 expect(
110 resolveRouteDraft({ ...base, stamp: stampB, projects: [project, projectB] }),
111 ).toMatchObject({ kind: "open", flow: { environmentId: environmentB } });
112 });
113
114 it("reports a draft that is gone", () => {
115 expect(resolveRouteDraft({ ...base, stamp: undefined })).toEqual({ kind: "missing" });
116 });
117
118 it("asks for a project for the same draft when its project is gone", () => {
119 expect(
120 resolveRouteDraft({ ...base, projects: [{ environmentId, id: ProjectId.make("other") }] }),
121 ).toEqual({ kind: "pickProject" });
122 });
123 });
124
125 describe("stampedEnvironmentProjectsLoading", () => {
126 const settled = {
127 connectionsReady: true,
128 connectionState: "connected" as const,
129 shellCacheRead: true,
130 shellStatus: "live" as const,
131 shellHasError: false,
132 };
133
134 it("waits while the environment is still delivering its first project list", () => {
135 expect(stampedEnvironmentProjectsLoading({ ...settled, connectionsReady: false })).toBe(true);
136 expect(stampedEnvironmentProjectsLoading({ ...settled, shellCacheRead: false })).toBe(true);
137 expect(stampedEnvironmentProjectsLoading({ ...settled, connectionState: "connecting" })).toBe(
138 true,
139 );
140 expect(stampedEnvironmentProjectsLoading({ ...settled, shellStatus: "synchronizing" })).toBe(
141 true,
142 );
143 });
144
145 it("settles once the list is live, or when nothing more will arrive", () => {
146 expect(stampedEnvironmentProjectsLoading(settled)).toBe(false);
147 expect(stampedEnvironmentProjectsLoading({ ...settled, connectionState: null })).toBe(false);
148 expect(
149 stampedEnvironmentProjectsLoading({
150 ...settled,
151 connectionState: "reconnecting",
152 shellStatus: "synchronizing",
153 }),
154 ).toBe(false);
155 expect(stampedEnvironmentProjectsLoading({ ...settled, shellHasError: true })).toBe(false);
156 });
157 });