Files
hyperframes/packages/cli/src/registry/localModel.test.ts
T
Miguel Ángel d290c05b09 fix(cli): --yes in an unattended run no longer overrides a no saved by another process (#4547)
* fix(cli): --yes in an unattended run no longer overrides a no saved by another process

* fix(cli): every settings writer takes the lock, and --yes needs a real terminal to reverse a no

* fix(cli): the thin-results download offer also needs a real terminal

* fix(cli): one owner-checked settings lock, and an unreadable settings file never counts as a yes

* fix(cli): settings lock removals go through a reaper, and a non-boolean answer counts as a no

* fix(cli): a settings lock that cannot be released no longer fails a write that landed

* fix(cli): a leftover settings lock is never taken over, and doctor names it

* fix(cli): a background update that cannot be recorded is not launched again every run

* fix(cli): every on-device install re-reads the saved answer first, and an unsaved yes installs nothing

* fix(cli): the thin-results offer stops when a yes cannot be saved

* fix(cli): only the consent-checked step installs the on-device runtime, and a leftover lock warns once

* fix(cli): the leftover-lock warning follows the attended-terminal rule
2026-09-27 01:39:17 +00:00

209 lines
6.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const readConfig = vi.fn();
const disk = { consent: undefined as boolean | undefined };
const existsSync = vi.fn();
const readFileSync = vi.fn();
const unlinkSync = vi.fn();
const downloadFile = vi.fn();
const digest = vi.fn();
vi.mock("../telemetry/config.js", () => ({
readConfig: () => readConfig(),
updateLocalModelConsent: (decide: (onDisk: boolean | undefined) => boolean | undefined) =>
(disk.consent = decide(disk.consent)),
}));
vi.mock("node:fs", async (importOriginal) => ({
...(await importOriginal<typeof import("node:fs")>()),
existsSync: (p: string) => existsSync(p),
mkdirSync: vi.fn(),
readFileSync: (p: string) => readFileSync(p),
unlinkSync: (p: string) => unlinkSync(p),
}));
vi.mock("node:crypto", () => ({
createHash: () => {
const hash = {
update: () => hash,
digest: () => digest(),
};
return hash;
},
}));
vi.mock("../utils/download.js", () => ({
downloadFile: (...args: unknown[]) => downloadFile(...args),
}));
const {
LOCAL_MODEL_DIMENSIONS,
LOCAL_MODEL_ARTIFACTS,
LOCAL_MODEL_SIZE_MB,
QUERY_INSTRUCTION,
downloadOfferMessage,
ensureLocalModel,
isLocalModelReady,
localModelConsent,
localModelStatus,
localTokenizerPath,
localModelPath,
recordLocalModelConsent,
assumeLocalModelConsent,
} = await import("./localModel.js");
function returnMatchingDigests(rounds = 1): void {
for (let round = 0; round < rounds; round += 1) {
for (const artifact of LOCAL_MODEL_ARTIFACTS) digest.mockReturnValueOnce(artifact.sha256);
}
}
function makeDownloadsAppearOnDisk(): void {
const present = new Set<string>();
existsSync.mockImplementation((path: string) => present.has(path));
downloadFile.mockImplementation(async (_url: string, path: string) => {
present.add(path);
});
}
beforeEach(() => {
readConfig.mockReturnValue({});
existsSync.mockReturnValue(false);
disk.consent = undefined;
readFileSync.mockReturnValue(Buffer.from("artifact"));
unlinkSync.mockReset();
downloadFile.mockReset();
digest.mockReset();
});
afterEach(() => vi.clearAllMocks());
describe("consent state", () => {
it("reports never-asked as undefined", () => {
expect(localModelConsent()).toBeUndefined();
});
it("records a person's answer over whatever was saved", () => {
disk.consent = false;
recordLocalModelConsent(true);
expect(disk.consent).toBe(true);
recordLocalModelConsent(false);
expect(disk.consent).toBe(false);
});
it("assumes yes only for a question never asked, and keeps a saved no", () => {
disk.consent = undefined;
expect(assumeLocalModelConsent()).toBe(true);
disk.consent = false;
expect(assumeLocalModelConsent()).toBe(false);
expect(disk.consent).toBe(false);
});
});
describe("readiness", () => {
it("needs both the model and its tokenizer", () => {
// A model without its tokenizer cannot turn text into tensors, so half a
// download is not a usable state.
existsSync.mockImplementation((p: string) => p === localModelPath());
expect(isLocalModelReady()).toBe(false);
existsSync.mockImplementation((p: string) => p === localTokenizerPath());
expect(isLocalModelReady()).toBe(false);
existsSync.mockReturnValue(true);
returnMatchingDigests();
expect(isLocalModelReady()).toBe(true);
});
it("rejects present files whose digest does not match the pinned revision", () => {
existsSync.mockReturnValue(true);
digest.mockReturnValue("wrong");
expect(isLocalModelReady()).toBe(false);
});
it("bounds each download and rejects a digest mismatch before reporting ready", async () => {
makeDownloadsAppearOnDisk();
digest.mockReturnValue("wrong");
expect(await ensureLocalModel()).toBe(false);
expect(downloadFile).toHaveBeenCalledWith(
expect.stringContaining("model_quantized.onnx"),
localModelPath(),
{ maxBytes: 34_014_426 },
);
expect(unlinkSync).toHaveBeenCalledWith(localModelPath());
});
it("accepts only two bounded downloads that match both pinned digests", async () => {
makeDownloadsAppearOnDisk();
returnMatchingDigests(2);
expect(await ensureLocalModel()).toBe(true);
expect(downloadFile).toHaveBeenNthCalledWith(
2,
expect.stringContaining("tokenizer.json"),
localTokenizerPath(),
{ maxBytes: 711_396 },
);
});
});
describe("status", () => {
it("is declined when the user said no, even if files somehow exist", () => {
readConfig.mockReturnValue({ localEmbeddingEnabled: false });
existsSync.mockReturnValue(true);
expect(localModelStatus()).toEqual({ status: "declined" });
});
it("is ready when consented and downloaded", () => {
readConfig.mockReturnValue({ localEmbeddingEnabled: true });
existsSync.mockReturnValue(true);
returnMatchingDigests();
expect(localModelStatus()).toEqual({ status: "ready" });
});
it("is not-asked when never answered and nothing downloaded", () => {
expect(localModelStatus()).toEqual({ status: "not-asked" });
});
it("is unavailable when consented but not yet downloaded", () => {
readConfig.mockReturnValue({ localEmbeddingEnabled: true });
expect(localModelStatus().status).toBe("unavailable");
});
it("never downloads or prompts by itself", () => {
// The caller owns the prompt, because only it knows whether word matching
// already answered well enough to make the offer pointless.
expect(() => localModelStatus()).not.toThrow();
expect(disk.consent).toBeUndefined();
});
});
describe("the offer text", () => {
it("names the cost rather than implying a privacy tradeoff", () => {
const message = downloadOfferMessage(0);
expect(message).toContain(`${LOCAL_MODEL_SIZE_MB} MB`);
expect(message).toMatch(/stays on your machine/);
expect(message).toMatch(/nothing is sent/);
});
it("leads with what the user just saw", () => {
expect(downloadOfferMessage(0)).toMatch(/^No matches/);
expect(downloadOfferMessage(1)).toMatch(/^Only 1 match\b/);
expect(downloadOfferMessage(3)).toMatch(/^Only 3 matches/);
});
it("does not claim word search already ran for an explicit on-device request", () => {
expect(downloadOfferMessage()).toMatch(/^Use on-device meaning search/);
});
});
describe("model contract", () => {
it("uses the dimension the bundled vectors are built at", () => {
expect(LOCAL_MODEL_DIMENSIONS).toBe(384);
});
it("carries the query instruction bge retrieval expects", () => {
// Omitting this measurably degrades short-query retrieval for bge models.
expect(QUERY_INSTRUCTION).toMatch(/searching relevant passages/);
});
});