返回 DeepSeek-Reasonix
formal-submission-recovery.test.tsx
根目录 / desktop / frontend / src / __tests__ / formal-submission-recovery.test.tsx
1 import assert from "node:assert/strict";
2 import React, { act } from "react";
3 import { createRoot } from "react-dom/client";
4 import { JSDOM } from "jsdom";
5 import { installDesktopHostStub } from "./desktopHostStub";
6 import { makeSessionUIMock } from "../lib/sessionUIMock";
7 import { beginPersistedComposerSubmission, sendPersistedComposer, useSessionComposerPersistence } from "../lib/sessionComposerPersistence";
8
9 const dom = new JSDOM("<div id='root'></div>", { url: "http://localhost" });
10 Object.assign(globalThis, { window: dom.window, document: dom.window.document, IS_REACT_ACT_ENVIRONMENT: true });
11 const backend = makeSessionUIMock(async () => {});
12 const stub = installDesktopHostStub(backend);
13 let editor!: ReturnType<typeof useSessionComposerPersistence>;
14 const ref = { hostId: "local", sessionId: "formal-diagnostic" };
15 function Probe() { editor = useSessionComposerPersistence(ref, ref.sessionId); return null; }
16 const root = createRoot(document.getElementById("root")!);
17 try {
18 await act(async () => { root.render(<Probe />); });
19 const target = editor.target!;
20 await act(async () => { target.onPatch!(target.draftId, target.generation, { text: "你好" }); });
21 let finishInput!: () => void;
22 const pending = new Promise<void>(done => { finishInput = done; });
23 const input = target.trackTask!(target.draftId, target.generation, pending.then(() => {
24 target.onPatch!(target.draftId, target.generation, { text: "你好 with accepted input" });
25 }), true);
26 await act(async () => {
27 await assert.rejects(beginPersistedComposerSubmission(ref.sessionId, "premature", "{}"), /Input is still being prepared/);
28 });
29 assert.equal((await backend.GetSessionComposerState(ref)).submissionId, undefined);
30 await act(async () => { finishInput(); await input; });
31 assert.equal(editor.target!.initial.text, "你好 with accepted input");
32 let sends = 0;
33 await act(async () => {
34 // As in Composer.submit, track the submission itself for exit persistence.
35 await target.trackTask!(target.draftId, target.generation,
36 sendPersistedComposer(ref.sessionId, "你好 with accepted input", "你好 with accepted input", "formal-diagnostic-send", async () => { sends++; }));
37 });
38 assert.equal(sends, 1);
39 assert.equal(editor.blocked, false);
40 assert.equal(editor.error, undefined);
41 assert.equal(JSON.parse((await backend.GetSessionComposerState(ref)).contentJson).text, undefined);
42 console.log("PASS formal submission does not count itself as an unfinished attachment; send callback=1");
43 } finally {
44 await act(async () => { root.unmount(); });
45 stub.uninstall();
46 dom.window.close();
47 }
48
48 lines Plain Text