| 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 { 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 sends = 0; |
| 22 | await act(async () => { |
| 23 | // As in Composer.submit, track the submission itself for exit persistence. |
| 24 | await target.trackTask!(target.draftId, target.generation, |
| 25 | sendPersistedComposer(ref.sessionId, "你好", "你好", "formal-diagnostic-send", async () => { sends++; })); |
| 26 | }); |
| 27 | assert.equal(sends, 1); |
| 28 | assert.equal(editor.blocked, false); |
| 29 | assert.equal(editor.error, undefined); |
| 30 | assert.equal(JSON.parse((await backend.GetSessionComposerState(ref)).contentJson).text, undefined); |
| 31 | console.log("PASS formal submission does not count itself as an unfinished attachment; send callback=1"); |
| 32 | } finally { |
| 33 | await act(async () => { root.unmount(); }); |
| 34 | stub.uninstall(); |
| 35 | dom.window.close(); |
| 36 | } |
| 37 |