|
1 | 1 | import { createMemoryHistory } from "@tanstack/react-router"; |
2 | 2 | import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment"; |
3 | | -import { EnvironmentId, RunId, ThreadId } from "@t3tools/contracts"; |
| 3 | +import { ThreadId } from "@t3tools/contracts"; |
4 | 4 | import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; |
5 | 5 | import type * as Context from "effect/Context"; |
6 | 6 | import { expect, it, vi } from "vite-plus/test"; |
7 | | -import { cdp, page, userEvent } from "vite-plus/test/browser"; |
| 7 | +import { page, userEvent } from "vite-plus/test/browser"; |
8 | 8 |
|
9 | 9 | import { AppRoot } from "~/AppRoot"; |
10 | 10 | // The route loads ChatView lazily, and its first transform (React Compiler over ~4k lines) |
@@ -13,23 +13,13 @@ import "~/components/ChatView"; |
13 | 13 | import { DraftId, useComposerDraftStore } from "~/composerDraftStore"; |
14 | 14 | import { selectThreadDiffPanelSelection, useDiffPanelStore } from "~/diffPanelStore"; |
15 | 15 | import { __setClientSettingsForTests } from "~/hooks/useSettings"; |
16 | | -import { environmentContextRecord } from "~/lib/composerContextRecords"; |
17 | | -import { usePromptStashStore } from "~/promptStashStore"; |
18 | 16 | import { useRightPanelStore } from "~/rightPanelStore"; |
19 | 17 | import { getRouter } from "~/router"; |
20 | | -import { appAtomRegistry } from "~/rpc/atomRegistry"; |
21 | | -import { environmentThreadDetails } from "~/state/threads"; |
22 | 18 | import { |
23 | 19 | ENVIRONMENT_ID, |
24 | 20 | PROJECT_ID, |
25 | | - answerAssetUrls, |
26 | | - assistantItem, |
27 | | - dispatchedCommands, |
28 | | - emitThreadEvent, |
29 | | - hasPendingAssetUrl, |
30 | 21 | installFakeEnvironment, |
31 | 22 | threadWithHistory, |
32 | | - threadWithQuestion, |
33 | 23 | type FakeDriver, |
34 | 24 | } from "../../test/fakeEnvironment"; |
35 | 25 | import { act, render } from "../../test/render"; |
@@ -49,118 +39,6 @@ vi.mock(import("~/rpc/requestLatencyState"), async (original) => ({ |
49 | 39 | trackRpcRequestSent: () => {}, |
50 | 40 | })); |
51 | 41 |
|
52 | | -it("a machine chip stashed in one thread restores with its machine in another", async () => { |
53 | | - const other = threadWithHistory("thread-stash-restore", 1); |
54 | | - const asking = threadWithQuestion("thread-stash-question", "Which file should change?"); |
55 | | - using _environment = installFakeEnvironment([other, asking]); |
56 | | - __setClientSettingsForTests({ |
57 | | - ...DEFAULT_CLIENT_SETTINGS, |
58 | | - onboardingCompletedAt: "2026-01-01T00:00:00.000Z", |
59 | | - }); |
60 | | - const draftId = DraftId.make("draft-stash-machine"); |
61 | | - const machine = environmentContextRecord(EnvironmentId.make("environment-build"), "Build box"); |
62 | | - const drafts = useComposerDraftStore.getState(); |
63 | | - drafts.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { |
64 | | - threadId: ThreadId.make("thread-stash-machine"), |
65 | | - }); |
66 | | - drafts.setPrompt(draftId, "Deploy on "); |
67 | | - drafts.addEnvironmentContexts(draftId, [machine]); |
68 | | - const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] })); |
69 | | - await router.load(); |
70 | | - await using _app = await render(<AppRoot router={router} />, { |
71 | | - // AppRoot brings its own router. |
72 | | - router: false, |
73 | | - }); |
74 | | - const editor = page.getByTestId("composer-editor"); |
75 | | - const pressStashShortcut = async () => { |
76 | | - await userEvent.click(editor); |
77 | | - await userEvent.keyboard("{ControlOrMeta>}s{/ControlOrMeta}"); |
78 | | - }; |
79 | | - await vi.waitFor(() => expect(editor.element().textContent).toContain("Deploy on")); |
80 | | - // A draft can't stash until the shell snapshot lists its project. |
81 | | - await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible(); |
82 | | - await pressStashShortcut(); |
83 | | - await vi.waitFor(() => expect(usePromptStashStore.getState().entries).toHaveLength(1)); |
84 | | - |
85 | | - // An answer carries no machines, so an open question refuses the restore and keeps the entry. |
86 | | - await router.navigate({ to: `/${ENVIRONMENT_ID}/${asking.id}` }); |
87 | | - await expect.element(page.getByText("Which file should change?")).toBeVisible(); |
88 | | - await userEvent.click(page.getByRole("button", { name: /^Stashed prompts/ })); |
89 | | - await userEvent.click(page.getByRole("button", { name: /^Restore stashed prompt/ })); |
90 | | - await vi.waitFor(() => |
91 | | - expect(document.body.textContent).toContain("Stashed machines can't go in an answer"), |
92 | | - ); |
93 | | - expect(usePromptStashStore.getState().entries).toHaveLength(1); |
94 | | - |
95 | | - await router.navigate({ to: `/${ENVIRONMENT_ID}/${other.id}` }); |
96 | | - await expect.element(page.getByText("Answer 0 for thread-stash-restore.")).toBeVisible(); |
97 | | - await pressStashShortcut(); |
98 | | - |
99 | | - await vi.waitFor(() => expect(editor.element().textContent).toContain("Deploy on")); |
100 | | - expect( |
101 | | - useComposerDraftStore.getState().getComposerDraft(scopeThreadRef(ENVIRONMENT_ID, other.id)) |
102 | | - ?.environmentContexts, |
103 | | - ).toEqual([machine]); |
104 | | - expect( |
105 | | - useComposerDraftStore.getState().getComposerDraft(draftId)?.environmentContexts ?? [], |
106 | | - ).toEqual([]); |
107 | | -}); |
108 | | - |
109 | | -it("a question that opens while a stashed file is checked keeps a machine stash", async () => { |
110 | | - const thread = threadWithHistory("thread-stash-late-question", 1); |
111 | | - const asking = threadWithQuestion("thread-stash-late-question", "Which file should change?"); |
112 | | - const [request] = asking.runtimeRequests ?? []; |
113 | | - const question = asking.items.at(-1); |
114 | | - if (!request || !question) throw new Error("threadWithQuestion has no question"); |
115 | | - using _environment = installFakeEnvironment([thread]); |
116 | | - __setClientSettingsForTests({ |
117 | | - ...DEFAULT_CLIENT_SETTINGS, |
118 | | - onboardingCompletedAt: "2026-01-01T00:00:00.000Z", |
119 | | - }); |
120 | | - usePromptStashStore.getState().stashEntry({ |
121 | | - id: "stash-late-question", |
122 | | - createdAt: "2026-01-01T00:00:00.000Z", |
123 | | - prompt: "Deploy on ", |
124 | | - attachments: [], |
125 | | - files: [ |
126 | | - { |
127 | | - id: "file-notes", |
128 | | - name: "notes.txt", |
129 | | - mimeType: "text/plain", |
130 | | - sizeBytes: 5, |
131 | | - attachmentId: "attachment-notes", |
132 | | - environmentId: ENVIRONMENT_ID, |
133 | | - }, |
134 | | - ], |
135 | | - droppedImageNames: [], |
136 | | - records: [environmentContextRecord(EnvironmentId.make("environment-build"), "Build box")], |
137 | | - }); |
138 | | - const router = getRouter( |
139 | | - createMemoryHistory({ initialEntries: [`/${ENVIRONMENT_ID}/${thread.id}`] }), |
140 | | - ); |
141 | | - await router.load(); |
142 | | - await using _app = await render(<AppRoot router={router} />, { |
143 | | - // AppRoot brings its own router. |
144 | | - router: false, |
145 | | - }); |
146 | | - await expect.element(page.getByText("Answer 0 for thread-stash-late-question.")).toBeVisible(); |
147 | | - await userEvent.click(page.getByRole("button", { name: /^Stashed prompts/ })); |
148 | | - await userEvent.click(page.getByRole("button", { name: /^Restore stashed prompt/ })); |
149 | | - |
150 | | - // The question opens while the restore still waits on the server's file check. |
151 | | - await vi.waitFor(() => expect(hasPendingAssetUrl()).toBe(true)); |
152 | | - await emitThreadEvent(thread.id, { type: "turn-item.updated", payload: question }); |
153 | | - await emitThreadEvent(thread.id, { type: "runtime-request.updated", payload: request }); |
154 | | - await expect.element(page.getByText("Which file should change?")).toBeVisible(); |
155 | | - await answerAssetUrls(); |
156 | | - |
157 | | - await vi.waitFor(() => |
158 | | - expect(document.body.textContent).toContain("Stashed machines can't go in an answer"), |
159 | | - ); |
160 | | - expect(usePromptStashStore.getState().entries).toHaveLength(1); |
161 | | - usePromptStashStore.getState().takeEntry("stash-late-question"); |
162 | | -}); |
163 | | - |
164 | 42 | it("a new draft does not load its reserved thread before the server lists it", async () => { |
165 | 43 | using _environment = installFakeEnvironment([]); |
166 | 44 | using fetchSpy = vi.spyOn(globalThis, "fetch"); |
@@ -279,3 +157,37 @@ it("retains each thread's diff selection when returning from another thread or S |
279 | 157 | turnId: a.runs[0]!.id, |
280 | 158 | }); |
281 | 159 | }); |
| 160 | + |
| 161 | +it("a stashed prompt ending in a file chip restores with the chip", async () => { |
| 162 | + using _environment = installFakeEnvironment([]); |
| 163 | + __setClientSettingsForTests({ |
| 164 | + ...DEFAULT_CLIENT_SETTINGS, |
| 165 | + onboardingCompletedAt: "2026-01-01T00:00:00.000Z", |
| 166 | + }); |
| 167 | + const draftId = DraftId.make("draft-28338"); |
| 168 | + const drafts = useComposerDraftStore.getState(); |
| 169 | + drafts.setProjectDraftThreadId(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), draftId, { |
| 170 | + threadId: ThreadId.make("thread-28338"), |
| 171 | + }); |
| 172 | + drafts.setPrompt(draftId, "Check [package.json](package.json) "); |
| 173 | + const router = getRouter(createMemoryHistory({ initialEntries: [`/draft/${draftId}`] })); |
| 174 | + await router.load(); |
| 175 | + await using _app = await render(<AppRoot router={router} />, { |
| 176 | + // AppRoot brings its own router. |
| 177 | + router: false, |
| 178 | + }); |
| 179 | + const chip = page.getByRole("button", { name: "Preview package.json" }); |
| 180 | + await expect.element(chip).toBeVisible(); |
| 181 | + // The stash shortcut is a no-op until the shell snapshot lists the project. |
| 182 | + await expect.element(page.getByText("render", { exact: true }).first()).toBeVisible(); |
| 183 | + |
| 184 | + const editor = page.getByTestId("composer-editor"); |
| 185 | + await userEvent.click(editor); |
| 186 | + await userEvent.keyboard("{ControlOrMeta>}s{/ControlOrMeta}"); |
| 187 | + await expect.element(chip).not.toBeInTheDocument(); |
| 188 | + await userEvent.keyboard("{ControlOrMeta>}s{/ControlOrMeta}"); |
| 189 | + |
| 190 | + await vi.waitFor(() => expect(editor.element().textContent).toContain("Check")); |
| 191 | + expect(editor.element().textContent).not.toContain("[package.json](package.json)"); |
| 192 | + await expect.element(chip).toBeVisible(); |
| 193 | +}); |