Files
DeepTutor/web/tests/marginnote4-api.test.ts
Bingxi Zhao (Frank) da856ad670 release: v1.6.9
Ship folder-based workspaces and data migration, daily practice, redesigned
provider and model settings, and streaming conversation improvements.
Synchronize README translations and document the archive/permanent-delete
upgrade behavior in assets/releases/ver1-6-9.md.

Reduce initial JavaScript by deduplicating English resources and loading
animation features after hydration. Keep the existing route size budgets.
Update regression fixtures and browser audits for scoped URLs and the new
settings and learning interfaces.
2026-09-21 02:30:40 +08:00

141 lines
4.3 KiB
TypeScript

import assert from "node:assert/strict";
import test from "node:test";
import { connectMarginNote4Library } from "../features/knowledge/api/catalog";
import {
getMarginNote4Status,
listMarginNote4Devices,
pairMarginNote4Device,
revokeMarginNote4Device,
} from "../lib/marginnote4-api";
function jsonResponse(status: number, body: unknown): Response {
return new Response(JSON.stringify(body), {
status,
headers: { "Content-Type": "application/json" },
});
}
function stubFetch(
handler: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
): () => void {
const original = globalThis.fetch;
(globalThis as { fetch: typeof fetch }).fetch = handler;
return () => {
(globalThis as { fetch: typeof fetch }).fetch = original;
};
}
const header = (init: RequestInit | undefined, name: string): string | null =>
new Headers(init?.headers).get(name);
test("every device-bridge call names its library through X-MN4-KB", async () => {
// The backend derives one SQLite store from this name, so a call that omits
// it pairs a device into a library the add-on's syncs never look at.
const calls: { input: string; method: string; kb: string | null }[] = [];
const restore = stubFetch(async (input, init) => {
calls.push({
input: String(input),
method: init?.method || "GET",
kb: header(init, "X-MN4-KB"),
});
if (String(input).endsWith("/devices") && (init?.method || "GET") === "GET")
return jsonResponse(200, []);
return jsonResponse(200, {
status: "ok",
devices: 0,
objects: 0,
device_id: "d1",
token: "t1",
device_name: "",
device_kind: "macos",
});
});
try {
await pairMarginNote4Device({ kbName: "My Lib", deviceName: "iPad" });
await listMarginNote4Devices("My Lib");
await revokeMarginNote4Device({ kbName: "My Lib", deviceId: "d1" });
await getMarginNote4Status("My Lib");
assert.deepEqual(
calls.map((c) => [c.method, c.input, c.kb]),
[
["POST", "/api/marginnote4/pair?dt_workspace=", "My Lib"],
["GET", "/api/marginnote4/devices?dt_workspace=", "My Lib"],
["DELETE", "/api/marginnote4/devices/d1?dt_workspace=", "My Lib"],
["GET", "/api/marginnote4/status?dt_workspace=", "My Lib"],
],
);
} finally {
restore();
}
});
test("pairing sends the device name and returns the one-time token", async () => {
let body: unknown = null;
const restore = stubFetch(async (_input, init) => {
body = JSON.parse(String(init?.body));
return jsonResponse(200, {
device_id: "dev-1",
token: "secret",
device_name: "iPad",
device_kind: "macos",
});
});
try {
const pairing = await pairMarginNote4Device({
kbName: "Lib",
deviceName: "iPad",
});
assert.deepEqual(body, { device_name: "iPad", device_kind: "macos" });
assert.equal(pairing.token, "secret");
} finally {
restore();
}
});
test("a device id is escaped into the revoke path", async () => {
let url = "";
const restore = stubFetch(async (input) => {
url = String(input);
return jsonResponse(200, { status: "revoked" });
});
try {
await revokeMarginNote4Device({ kbName: "Lib", deviceId: "a/b?c" });
assert.equal(url, "/api/marginnote4/devices/a%2Fb%3Fc?dt_workspace=");
} finally {
restore();
}
});
test("a failed call surfaces the server's detail", async () => {
const restore = stubFetch(async () =>
jsonResponse(501, { detail: "pairing and sync would resolve differently" }),
);
try {
await assert.rejects(
pairMarginNote4Device({ kbName: "Lib", deviceName: "" }),
/resolve differently/,
);
} finally {
restore();
}
});
test("connecting a library never pins db_path", async () => {
// Leaving it blank is what keeps one rule for where the store lives, so
// pairing, the add-on's syncs and the capability binding cannot disagree.
let body: Record<string, unknown> = {};
const restore = stubFetch(async (_input, init) => {
body = JSON.parse(String(init?.body));
return jsonResponse(200, { status: "connected", name: "Lib" });
});
try {
await connectMarginNote4Library({ name: "Lib" });
assert.deepEqual(body, { name: "Lib", description: "" });
assert.equal("db_path" in body, false);
} finally {
restore();
}
});