Files
OmniRoute/tests/unit/compression/llmlingua-worker.test.ts
Webman 118840131d fix(deps): upgrade @atjsh/llmlingua-2 to 2.0.5 and drop @tensorflow/tfjs (#10610)
Implements #10536: upgrade @atjsh/llmlingua-2 2.0.3 → 2.0.5 and drop @tensorflow/tfjs from the LLMLingua SLM optional stack.

Validated in an isolated worktree boarded onto origin/release/v3.8.50 (0 conflicts, 20 files):
- 48/48 focused llmlingua/colocate/docker unit tests pass (author-reported, reproduced).
- check-file-size, check-changelog-integrity: OK.
- grep confirms no remaining source imports of @tensorflow/tfjs.
- typecheck:core: clean.
- check-complexity / check-cognitive-complexity: OK, both under baseline.

Co-authored-by: jonlwheat2-gif <jonlwheat2-gif@users.noreply.github.com>
2026-08-20 22:02:14 -03:00

92 lines
3.2 KiB
TypeScript

/**
* Tests for the real LLMLingua worker-thread backend (`worker.ts` + `onnxWorker.ts`).
*
* The three optional deps (`@atjsh/llmlingua-2`, `@huggingface/transformers`,
* `js-tiktoken`) are NOT installed in this worktree, so the
* default path MUST fail-open WITHOUT spawning a worker:
*
* 1. Deps absent → fail-open, no spawn (ALWAYS runs here): the backend returns the
* ORIGINAL text unchanged, fast (no model load / worker spawn).
* 2. Type smoke: `workerBackend` is a function.
* 3. GATED real compression (RUN_LLMLINGUA_INT=1): real shrink with deps present;
* no-op skip here (deps absent).
*/
import { test, after } from "node:test";
import assert from "node:assert/strict";
import { createRequire } from "node:module";
import {
workerBackend,
__resetLlmlinguaWorkerForTests,
} from "../../../open-sse/services/compression/engines/llmlingua/worker.ts";
const require = createRequire(import.meta.url);
/** Whether all three optional deps resolve in this environment. */
function depsResolve(): boolean {
try {
require.resolve("@atjsh/llmlingua-2");
require.resolve("@huggingface/transformers");
require.resolve("js-tiktoken");
return true;
} catch {
return false;
}
}
// Let the process exit cleanly: terminate any spawned worker + reset singletons.
after(() => {
__resetLlmlinguaWorkerForTests();
});
test("deps absent → fail-open, no spawn, returns original text fast", async () => {
if (depsResolve()) {
// Premise of this test is that the optional deps are NOT installed (the CI
// default). When they ARE present (e.g. a local integration setup), the backend
// legitimately spawns the worker and compresses, so this assertion no longer
// applies — the real path is covered by the gated test below.
console.log("skip: optional deps present — fail-open-when-absent test N/A");
return;
}
const input = "hello world this is some prose";
const start = Date.now();
const out1 = await workerBackend(input, {});
const elapsed = Date.now() - start;
// EXACT original text (fail-open), unchanged.
assert.equal(out1, input);
// Fast: no model load / worker spawn (proves the optional-deps gate short-circuits).
assert.ok(elapsed < 1000, `expected <1000ms, got ${elapsed}ms`);
// Second call exercises the memoized gate — still fail-open, still fast.
const out2 = await workerBackend(input, {});
assert.equal(out2, input);
});
test("type smoke: workerBackend is a function", () => {
assert.equal(typeof workerBackend, "function");
});
test("GATED real compression (RUN_LLMLINGUA_INT=1)", async () => {
if (process.env.RUN_LLMLINGUA_INT !== "1") {
console.log("skip: RUN_LLMLINGUA_INT!=1");
return;
}
if (!depsResolve()) {
console.log("skip: deps absent");
return;
}
// Long prose well above any practical floor so compression has room to shrink.
const LONG_PROSE =
"The quick brown fox jumps over the lazy dog while the sun sets slowly behind the distant hills. ".repeat(
120
);
const out = await workerBackend(LONG_PROSE, { model: "tinybert", compressionRate: 0.5 });
assert.equal(typeof out, "string");
assert.ok(out.length < LONG_PROSE.length, "expected a real shrink");
});