Files
DeepTutor/web/tests/knowledge-helpers.test.ts
Lan-zk 3f1894e0c4 fix(knowledge): harden GraphRAG model and embedding compatibility (#747)
* fix(knowledge): add GraphRAG model compatibility layer

* fix(graphrag): harden compatibility preflight

* fix: align GraphRAG compatibility with current dev

* test: skip GraphRAG config test without optional extra

---------

Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
2026-08-17 01:12:05 +08:00

160 lines
4.0 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import {
kbCanReindex,
resolveKnowledgeIndexFailure,
taskFailureMessage,
providerConnectionStatus,
type KnowledgeBase,
} from "../lib/knowledge-helpers";
function kb(overrides: Partial<KnowledgeBase>): KnowledgeBase {
return {
name: "kb",
status: "ready",
statistics: { raw_documents: 1 },
...overrides,
};
}
test("kbCanReindex allows failed knowledge bases with source files", () => {
assert.equal(
kbCanReindex(
kb({
status: "error",
statistics: { raw_documents: 1, active_match: true },
}),
),
true,
);
});
test("kbCanReindex keeps empty failed knowledge bases disabled", () => {
assert.equal(
kbCanReindex(
kb({
status: "error",
statistics: { raw_documents: 0, active_match: false },
}),
),
false,
);
});
test("kbCanReindex preserves mismatch and needs-reindex behavior", () => {
assert.equal(
kbCanReindex(kb({ statistics: { raw_documents: 1, needs_reindex: true } })),
true,
);
assert.equal(
kbCanReindex(kb({ statistics: { raw_documents: 1, active_match: false } })),
true,
);
assert.equal(
kbCanReindex(kb({ statistics: { raw_documents: 1, active_match: true } })),
false,
);
});
test("resolveKnowledgeIndexFailure preserves actionable backend metadata", () => {
assert.deepEqual(
resolveKnowledgeIndexFailure(
kb({
status: "error",
progress: {
stage: "error",
error: "Choose a chat model that supports structured output.",
error_code: "graphrag_model_incompatible",
retryable: false,
},
}),
),
{
code: "graphrag_model_incompatible",
message: "Choose a chat model that supports structured output.",
retryable: false,
requiresModelChange: true,
settingsHref: "/settings/models",
},
);
});
test("resolveKnowledgeIndexFailure distinguishes configuration from transient failures", () => {
const authentication = resolveKnowledgeIndexFailure(
kb({
status: "error",
progress: {
stage: "error",
error_code: "graphrag_model_authentication_failed",
retryable: false,
},
}),
);
const rateLimit = resolveKnowledgeIndexFailure(
kb({
status: "error",
progress: {
stage: "error",
error_code: "graphrag_model_rate_limited",
retryable: true,
},
}),
);
assert.equal(authentication?.requiresModelChange, true);
assert.equal(authentication?.settingsHref, "/settings/models");
assert.equal(rateLimit?.requiresModelChange, false);
assert.equal(rateLimit?.settingsHref, undefined);
assert.equal(rateLimit?.retryable, true);
});
test("resolveKnowledgeIndexFailure routes embedding configuration failures to embedding settings", () => {
const endpointFailure = resolveKnowledgeIndexFailure(
kb({
status: "error",
progress: {
stage: "error",
error_code: "graphrag_embedding_endpoint_failed",
retryable: false,
},
}),
);
assert.equal(endpointFailure?.requiresModelChange, true);
assert.equal(endpointFailure?.settingsHref, "/settings/embedding");
});
test("taskFailureMessage keeps trace details out of the primary error", () => {
assert.equal(
taskFailureMessage({
detail: "GraphRAG preflight failed.",
details: "Traceback: sensitive internal diagnostics",
}),
"GraphRAG preflight failed.",
);
});
test("IMA is connected per knowledge base instead of globally ready", () => {
assert.equal(
providerConnectionStatus({ id: "ima", configured: true }),
"per_kb",
);
assert.equal(
providerConnectionStatus({ id: "llamaindex", configured: true }),
"ready",
);
assert.equal(
providerConnectionStatus({
id: "pageindex",
configured: false,
requires_api_key: true,
}),
"needs_key",
);
assert.equal(
providerConnectionStatus({ id: "graphrag", configured: false }),
"unavailable",
);
});