mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 10:30:46 +08:00
The button inferred success from its handler's promise *resolving*, and the handler it was given could not fail: `copyAssistantMessage` awaited `navigator.clipboard.writeText` inside a try/catch that logged and swallowed. So the only failure signal was destroyed one frame before the only code that could have used it — and every clipboard failure rendered 已复制 with a checkmark. Because the label is also the `aria-label` on an `aria-live` element, the false success was announced to screen readers. The failure is not exotic. `launcher.py` binds the web app to 0.0.0.0 while printing a localhost URL, so opening the same instance from another machine on the LAN is an ordinary way to run DeepTutor — and on that plain-http origin `navigator.clipboard` does not exist, so reading `.writeText` throws synchronously. `FilePreviewDrawer` already carried a comment naming this exact cause; the handling was local instead of shared. So: one `copyText` helper that rejects when the text did not reach the clipboard, with a selection-based fallback for insecure origins (probed before any await, since `execCommand` needs the click's user activation); `CopyActionButton` narrowed to a `Promise<void>` handler and given a real failure state, so the contract cannot be reintroduced by the next consumer; and the three consumers plus the two other optimistic sites moved onto it. `Promise.resolve().then(() => onCopy(...))` replaces `Promise.resolve(onCopy(...))` because the latter runs the handler outside the chain, which is where the synchronous TypeError was escaping. Also adds the `attachments` and `references` locale keys. Both call sites pass a lowercase noun for inline reading, neither key existed, and i18next is configured to return a missing key verbatim — which is why a Chinese UI rendered "5 attachments" in English next to this very button.
145 lines
4.1 KiB
TypeScript
145 lines
4.1 KiB
TypeScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
|
|
import { copyText } from "../lib/clipboard";
|
|
|
|
type GlobalName = "navigator" | "window" | "document";
|
|
|
|
/**
|
|
* `globalThis.navigator` is an accessor on modern Node, so a plain assignment
|
|
* throws. Define the property instead, and restore the original descriptor
|
|
* afterwards so one case cannot leak into the next.
|
|
*/
|
|
function setGlobal(name: GlobalName, value: unknown): () => void {
|
|
const previous = Object.getOwnPropertyDescriptor(globalThis, name);
|
|
Object.defineProperty(globalThis, name, {
|
|
value,
|
|
configurable: true,
|
|
writable: true,
|
|
});
|
|
return () => {
|
|
if (previous) Object.defineProperty(globalThis, name, previous);
|
|
else delete (globalThis as Record<string, unknown>)[name];
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Stand in for the three browser objects `copyText` touches. Each field is
|
|
* optional so a case can model an environment where one is simply absent —
|
|
* which is the whole point: on a plain-http origin `navigator.clipboard` does
|
|
* not exist.
|
|
*/
|
|
function withBrowser(
|
|
parts: {
|
|
clipboard?: { writeText: (text: string) => Promise<void> };
|
|
isSecureContext?: boolean;
|
|
execCommand?: (command: string) => boolean;
|
|
},
|
|
run: () => Promise<void>,
|
|
): Promise<void> {
|
|
const appended: unknown[] = [];
|
|
const restore = [
|
|
setGlobal("navigator", parts.clipboard ? { clipboard: parts.clipboard } : {}),
|
|
setGlobal("window", { isSecureContext: parts.isSecureContext ?? false }),
|
|
setGlobal(
|
|
"document",
|
|
parts.execCommand
|
|
? {
|
|
activeElement: null,
|
|
body: { appendChild: (node: unknown) => void appended.push(node) },
|
|
createElement: () => ({
|
|
value: "",
|
|
style: {},
|
|
setAttribute: () => undefined,
|
|
select: () => undefined,
|
|
setSelectionRange: () => undefined,
|
|
remove: () => undefined,
|
|
}),
|
|
execCommand: parts.execCommand,
|
|
}
|
|
: undefined,
|
|
),
|
|
];
|
|
|
|
return run().finally(() => {
|
|
for (const undo of restore.reverse()) undo();
|
|
});
|
|
}
|
|
|
|
test("copyText rejects when there is nothing to copy", async () => {
|
|
await assert.rejects(() => copyText(" "), /nothing to copy/i);
|
|
});
|
|
|
|
test("copyText uses the async Clipboard API in a secure context", async () => {
|
|
const written: string[] = [];
|
|
await withBrowser(
|
|
{
|
|
clipboard: {
|
|
writeText: async (text: string) => void written.push(text),
|
|
},
|
|
isSecureContext: true,
|
|
execCommand: () => {
|
|
throw new Error("the selection fallback must not run here");
|
|
},
|
|
},
|
|
async () => {
|
|
await copyText("eigenvectors");
|
|
},
|
|
);
|
|
assert.deepEqual(written, ["eigenvectors"]);
|
|
});
|
|
|
|
/**
|
|
* The reported bug: reaching a DeepTutor instance over the LAN means a
|
|
* plain-http origin, where `navigator.clipboard` is undefined. The old code
|
|
* threw a TypeError here, swallowed it, and rendered 已复制 anyway.
|
|
*/
|
|
test("copyText falls back to the selection path on an insecure origin", async () => {
|
|
const commands: string[] = [];
|
|
await withBrowser(
|
|
{
|
|
isSecureContext: false,
|
|
execCommand: (command: string) => {
|
|
commands.push(command);
|
|
return true;
|
|
},
|
|
},
|
|
async () => {
|
|
await copyText("eigenvectors");
|
|
},
|
|
);
|
|
assert.deepEqual(commands, ["copy"]);
|
|
});
|
|
|
|
test("copyText rejects when neither path can write", async () => {
|
|
await withBrowser(
|
|
{ isSecureContext: false, execCommand: () => false },
|
|
async () => {
|
|
await assert.rejects(() => copyText("eigenvectors"), /not available/i);
|
|
},
|
|
);
|
|
});
|
|
|
|
/** A rejected `writeText` (lost focus, denied permission) still gets one try. */
|
|
test("copyText falls back when the async API rejects", async () => {
|
|
const commands: string[] = [];
|
|
await withBrowser(
|
|
{
|
|
clipboard: {
|
|
writeText: async () => {
|
|
throw new Error("Document is not focused");
|
|
},
|
|
},
|
|
isSecureContext: true,
|
|
execCommand: (command: string) => {
|
|
commands.push(command);
|
|
return true;
|
|
},
|
|
},
|
|
async () => {
|
|
await copyText("eigenvectors");
|
|
},
|
|
);
|
|
assert.deepEqual(commands, ["copy"]);
|
|
});
|