mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 05:49:26 +08:00
Every progress line the knowledge log box shows was a hardcoded English string — 22 producers across the initializer, the document adder and the router, rendered verbatim in the UI. #933 added one more ("Describing images: m/n"), which is what surfaced the class. Indexing runs detached from any request, so the backend has no viewer language and the language of record lives in the browser anyway. So the wire now carries the English `{{name}}` template plus its values, and the frontend renders it with `t()` where the language is actually known. `ProgressTracker.update` fills `message` from that same template, so every consumer without i18n of its own — logs, the persisted snapshot, the CLI — reads what it read before, and no call site spells the string twice. Untouched producers keep working: `progressMessage()` falls back to the rendered `message` when no template is named.
79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
|
||
import {
|
||
appendTaskLog,
|
||
taskStateAfterProgress,
|
||
type TaskState,
|
||
} from "../hooks/useKnowledgeProgress";
|
||
|
||
const running: TaskState = {
|
||
taskId: "task-1",
|
||
kind: "create",
|
||
label: "Create KB",
|
||
logs: ["Queued"],
|
||
executing: true,
|
||
error: null,
|
||
};
|
||
|
||
test("progress messages append once and complete the active task", () => {
|
||
assert.deepEqual(appendTaskLog(["Queued"], "Indexing"), [
|
||
"Queued",
|
||
"Indexing",
|
||
]);
|
||
assert.deepEqual(appendTaskLog(["Queued", "Indexing"], "Indexing"), [
|
||
"Queued",
|
||
"Indexing",
|
||
]);
|
||
|
||
const completed = taskStateAfterProgress(running, "task-1", {
|
||
task_id: "task-1",
|
||
stage: "completed",
|
||
message: "Index ready",
|
||
});
|
||
assert.equal(completed.executing, false);
|
||
assert.equal(completed.error, null);
|
||
assert.deepEqual(completed.logs, ["Queued", "Index ready"]);
|
||
});
|
||
|
||
test("progress from another task cannot overwrite current state", () => {
|
||
assert.equal(
|
||
taskStateAfterProgress(running, "task-2", {
|
||
task_id: "task-2",
|
||
stage: "error",
|
||
error: "wrong task",
|
||
}),
|
||
running,
|
||
);
|
||
});
|
||
|
||
test("translates a templated progress line and falls back without one", () => {
|
||
const t = (key: string, options?: Record<string, unknown>) =>
|
||
key === "Describing images: {{current}}/{{total}}"
|
||
? `图片描述:${options?.current}/${options?.total}`
|
||
: key;
|
||
|
||
const translated = taskStateAfterProgress(
|
||
{ ...running, logs: [] },
|
||
"task-1",
|
||
{
|
||
task_id: "task-1",
|
||
stage: "processing_documents",
|
||
message: "Describing images: 3/7",
|
||
message_key: "Describing images: {{current}}/{{total}}",
|
||
message_params: { current: 3, total: 7 },
|
||
},
|
||
t,
|
||
);
|
||
assert.deepEqual(translated.logs, ["图片描述:3/7"]);
|
||
|
||
// A producer that has not been converted still shows its rendered English.
|
||
const legacy = taskStateAfterProgress(
|
||
{ ...running, logs: [] },
|
||
"task-1",
|
||
{ task_id: "task-1", stage: "processing_documents", message: "Saving…" },
|
||
t,
|
||
);
|
||
assert.deepEqual(legacy.logs, ["Saving…"]);
|
||
});
|