Files
DeepTutor/web/tests/ima-connection.test.ts
Bingxi Zhao (Frank) 96d6df9eb1 release: v1.5.13
Book is the bulk of this one. Compilation outlives the request that
started it, so a bus owned by that request dropped every background event
and "watch your book being written" degraded into a frozen page; the
stream is now owned per book by the process and survives a refresh or a
reconnect. Reading progress is finally recorded — `visited_page_ids` and
`bookmarked_page_ids` had been declared but never written for the life of
the feature — and rolls up into a completion score and weak chapters. A
book can also leave the app now, as one self-contained Markdown document,
with non-ASCII titles surviving the download.

Two scheduling defects that were invisible in production are fixed: the
same page compiled twice concurrently (billed twice, one result
discarded), and a book grinding on through a provider outage until every
chapter was half-generated. The source sweep is bounded and allocated
round-robin rather than spent on the first few knowledge bases, and
retrieval now tells indexed bases from connected pointers, so an Obsidian
vault or a remote IMA library no longer contributes zero while looking
like a source with nothing relevant. Confirming a spine shows a live
per-chapter estimate derived from the Section Architect's own templates.

Also here: three home starter suggestions built from memory recall,
mastery-path handoff moved into one module that changes lease, session
preference, and live turn together, and account-level Tencent IMA
credentials with the per-KB pair still winning.

Notes: assets/releases/ver1-5-13.md

This commit also carries the README prose sync, the eleven translated
READMEs, the release-notes archive sweep, and the version bump. Two gate
fixes ride along: the `Stamped` protocol is declared read-only so the
frozen `EntityStamp` satisfies it, and six GraphRAG adapter tests that
reached a deep `graphrag` import without guarding it now skip like their
four neighbours instead of failing where the extra is not installed.
2026-08-17 11:22:26 +08:00

158 lines
4.3 KiB
TypeScript

import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import path from "node:path";
import test from "node:test";
import type { RagProviderSummary } from "../lib/knowledge-api";
import {
canConnectIma,
createProviders,
emptyImaLookupState,
linkSourceEnabled,
mergeImaKnowledgeBases,
nextAutoName,
} from "../lib/ima-connection";
test("IMA belongs only to link-existing provider choices", () => {
const providers = [
{ id: "ima", name: "IMA", description: "remote" },
{ id: "llamaindex", name: "LlamaIndex", description: "local" },
{ id: "pageindex", name: "PageIndex", description: "cloud" },
] as RagProviderSummary[];
assert.deepEqual(
createProviders(providers).map((provider) => provider.id),
["llamaindex", "pageindex"],
);
assert.equal(linkSourceEnabled(providers[0]), true);
assert.equal(linkSourceEnabled({ ...providers[2], linkable: false }), false);
assert.equal(linkSourceEnabled({ ...providers[1], linkable: true }), true);
});
test("leaving the IMA source clears credentials from the mounted flow", () => {
const hookSource = readFileSync(
path.resolve("hooks/useImaConnection.ts"),
"utf8",
);
const modalSource = readFileSync(
path.resolve("components/knowledge/CreateKbModal.tsx"),
"utf8",
);
assert.match(hookSource, /setClientId\(""\);\s+setApiKey\(""\);/);
assert.match(
modalSource,
/const handleClose = \(\) => {\s+imaConnection\.reset\(\);/,
);
assert.match(
modalSource,
/if \(source !== IMA_PROVIDER\) imaConnection\.reset\(\);/,
);
});
test("automatic names never overwrite a manual DeepTutor name", () => {
assert.equal(nextAutoName("", null, "IMA Notes"), "IMA Notes");
assert.equal(nextAutoName("IMA Old", "IMA Old", "IMA New"), "IMA New");
assert.equal(
nextAutoName("My manual name", "IMA Old", "IMA New"),
"My manual name",
);
});
test("IMA pagination merges by id and keeps list order", () => {
const merged = mergeImaKnowledgeBases(
[
{ id: "kb-1", name: "Alpha", description: null },
{ id: "kb-2", name: "Beta", description: null },
],
[
{ id: "kb-2", name: "Beta", description: "enriched" },
{ id: "kb-3", name: "Gamma", description: null },
],
);
assert.deepEqual(merged, [
{ id: "kb-1", name: "Alpha", description: null },
{ id: "kb-2", name: "Beta", description: "enriched" },
{ id: "kb-3", name: "Gamma", description: null },
]);
});
test("empty IMA lookup state removes selections and verification", () => {
assert.deepEqual(emptyImaLookupState(), {
status: "idle",
knowledgeBases: [],
selectedId: "",
nextCursor: "",
isEnd: true,
manualVerification: null,
lastAutoName: null,
});
});
test("automatic IMA connection requires credentials and a selected id", () => {
const base = {
mode: "automatic" as const,
name: "Notes",
clientId: "cid",
apiKey: "key",
credentialsReady: true,
selectedId: "kb-1",
manualKnowledgeBaseId: "",
manualVerification: null,
};
assert.equal(canConnectIma(base), true);
assert.equal(canConnectIma({ ...base, credentialsReady: false }), false);
assert.equal(canConnectIma({ ...base, selectedId: "" }), false);
});
test("the account credential pair connects without any typed credentials", () => {
// Nothing is typed and nothing is sent — the server resolves the pair.
assert.equal(
canConnectIma({
mode: "automatic",
name: "Notes",
clientId: "",
apiKey: "",
credentialsReady: true,
selectedId: "kb-1",
manualKnowledgeBaseId: "",
manualVerification: null,
}),
true,
);
});
test("manual IMA connection accepts only the current verified credential tuple", () => {
const base = {
mode: "manual" as const,
name: "Notes",
clientId: "cid",
apiKey: "key",
credentialsReady: true,
selectedId: "",
manualKnowledgeBaseId: "kb-1",
manualVerification: {
ok: true,
clientId: "cid",
apiKey: "key",
knowledgeBaseId: "kb-1",
},
};
assert.equal(canConnectIma(base), true);
assert.equal(canConnectIma({ ...base, apiKey: "new-key" }), false);
assert.equal(
canConnectIma({ ...base, manualKnowledgeBaseId: "kb-2" }),
false,
);
assert.equal(
canConnectIma({
...base,
manualVerification: { ...base.manualVerification, ok: false },
}),
false,
);
});