* refactor(thumbnails): one resolver for where previews live on disk (#320) Add py/api/preview_store.py: the store of a source, a preview's path, URL parts to disk, containment (owns), a write generation and the lock around every preview's final rename. Every preview reader, writer and remover now asks it instead of joining <source>/thumbnails by hand: serve, on-demand, capture (its path resolved when the write starts), the after-scan queue, the thumbnail plans and job payloads, clear, the temp sweep, History covers, the auto-tag listing and the owned-preview lookups. Source mode only: every path is the one built before, so nothing changes on disk or on the wire. A structural test keeps new thumbnails joins out of the backend. * refactor(thumbnails): keep the after-scan queue's store rule and pin the write lock (#320) A planned target keeps the plan's own containment rule (inside the store, as the thumbnail job writes it); captures keep the serve rule. The structural test also covers PREVIEW_FOLDER joins and the repo-root modules; the write-lock test asserts the lock instead of waiting on a timer; new tests cover nested roots in the after-scan queue and a legacy upper-case preview served through the resolver. * feat(thumbnails): keep previews in one folder, set from config.json (#320) thumbnails.location in config.json can now name one folder for every source's previews: <folder>/<source key>/<rel>_thumb<ext>. A source key is a readable slug plus a short hash of the source's identity, stored in the folder's marker (.promptmanager-thumbnails.json) with the install that made it. Serving, on-demand and capture writes, listings, clear, delete, duplicates, the temp sweep and History covers all follow it; a leftover in <source>/thumbnails is never served and other text under thumbnails/ is refused. A missing, unplugged or foreign folder is unavailable (a probe checks it every 5 s within 2 s; a stalled or failing store call flips it at once): originals are served, nothing is written anywhere, the folder is never re-created, and generate, convert and clear answer 409 location-unavailable. A bad config value is unavailable too, never a fall-back to the sources. No UI yet: POST /settings refuses thumbnail_location (409 location-needs-move); GET /settings reports it with its path, the thumbnail status without one. Sources inside or around the folder and a library move into it are refused. * fix(thumbnails): never follow a marker or a link out of the thumbnail folder (#320) Marker keys are data, not paths: only a single <slug>-<hex> folder name is ever joined onto the thumbnail folder; anything else is dropped and logged. Every preview write and delete passes one ownership check (below a key folder, lexically and resolved, no link, junction or reparse point on the way); make_parent refuses anything else and writes nothing while the folder lacks this install's marker (an unmounted mount point). Clear never descends into a junction. The configured location is in force before any request (unchecked folder mode is unavailable), and a startup check that fails leaves it invalid, never the sources. Older listings that run on the event loop no longer touch the store in folder mode. A leftover under another case of thumbnails/ on a case-insensitive disk is refused too. * test(memory): let the musl test fake os.confstr on Windows (#523) The portability run on main fails since #523: Windows has no os.confstr, so patching it without create=True raises before the test runs. * feat(thumbnails): move thumbnails between the sources and one folder (#320) POST /images/thumbnails/plan reviews a move (what is copied, removed and left, free space, too-long Windows paths, sources no longer listed) and hands out a token; POST /jobs/thumbnails {mode: move} runs it as a thumbnails job: hold automatic previews, copy and check every preview on the job pool, switch with one config commit that records the old copies, then remove them. Per-file errors still switch; a folder-wide error, a cancel before the switch or a crash leaves nothing changed (a journal is undone at the next start); old copies that could not go are removed by a finishing job at the next start. Leaving a folder removes the previews of this install's sources that are no longer listed (Q320-8). Recreate folder and Use this folder answer an unavailable folder; the Thumbnail maintenance scope prune removes the thumbnails of removed sources. * feat(system): choose where thumbnails are kept and move them (#320) System › Sources & scans gains the Thumbnail location row (D196): Inside each source or One folder, the folder path, the status line and Review move…, whose review (D197) lists what is copied, removed and left before Move thumbnails starts; a refused folder is explained under the field, a folder of your own asks for a confirmation first. Activity shows a move and a prune with their own tiles and step line (D198). An unavailable folder shows a notice on Overview and Sources & scans with Recreate folder or Use this folder (D199). Thumbnail maintenance offers Remove thumbnails of removed sources in folder mode (D200), and Move library's review says where the thumbnails stay (D201). Closes #320 * fix(thumbnails): keep a move's own previews and never walk out of a store (#320) A cleanup that waited from an earlier move never names a key folder the new move just filled, so moving back keeps the previews. Removing empty folders walks top-down and never into a link or junction. Any error while copying undoes the copies; once the switch is committed, a journal that cannot be removed no longer undoes it. Hand-edited cleanup entries and marker keys never name a folder outside the stores; removal stops at interpreter exit. The path field carries aria-invalid with its reason, and a review that went stale reviews again. * test(thumbnails): cover the prune scope, the Overview notice and the space refusal (#320) The frontend coverage gate counted the new branches of thumbnail maintenance, the Activity views, the notices' Change location and the api-client's space refusal as uncovered; page and unit tests now drive them. * fix(thumbnails): report removals that fail and explain the quiet excepts (#320) An old thumbnail the finishing move cannot remove (held open on Windows) is now an error row of the job with its path, and logged; a failed copy's temp that cannot go is logged with its path. Every except that only passes says why, and the thread runners say their caller re-raises. * fix(thumbnails): never plan or delete through a linked key folder (#320) Review finding P1 on #591: in folder mode a source key folder that is a link or junction to a folder outside was still handed to the planner, so Convert outdated could remove an older-format file outside the thumbnail folder. ensure_store now refuses a linked store before any planning, and each removal of an old preview passes may_write at the moment it runs. * fix(thumbnails): the temp sweep never walks into a link (#320) Review finding P2 on #591: Clear thumbnails and the startup sweep removed crashed-write temps with a plain os.walk, which on Windows walks into a junction even without followlinks. The sweeper now skips a folder that is a link, junction or reparse point (the top one too), and each temp passes may_write before it is removed. * fix(thumbnails): lock the folder marker across processes (#320) Review finding P2 on #591: two installs sharing one thumbnail folder could each read the marker, add a key and replace it, so the last writer dropped the other's key. write_marker now re-reads, changes and replaces the marker under a lock file made with O_CREAT | O_EXCL beside it (atomic on shares, unlike flock); a lock older than 30 s, left by a writer that died, is taken over, and a wait past 1.5 s fails as a TimeoutError like an unreachable folder. Tested with three processes adding keys at once. * test(thumbnails): cover the space refusal, Overview's Use this folder and the prune review (#320) The frontend coverage floors of activity-kinds, system-overview and thumbnail-review: a disk without the space shows the review but cannot move (a fixture route fakes the free space), Use this folder on Overview reloads the host values, the prune review's text and rows are pure functions with unit tests, and Overview takes the System drafts it is always mounted with. * fix(thumbnails): never copy through a linked folder; refuse a target inside the folder in use (#320) Moving back to Inside each source followed an existing linked folder in the destination and could replace a file outside the source (Codex P1). A target nested inside the thumbnail folder in use was accepted, and the old-copy cleanup then removed the previews just copied there (Codex P2). Every copy now checks the whole destination chain for links, also when the folders exist, and the move refuses a folder inside the one in use with location-inside-current. The cancelled branches no longer mix explicit and implicit returns (code-quality note). * fix(system): one full stop in the move-back review (#320) * fix(thumbnails): a folder check answers for the folder as it is now (#320) Probe.check() joined a check already running, or a finished one nobody had collected. A folder deleted right after configure() woke the probe therefore stayed available until the next probe, which failed the Overview notice spec in the merge queue. It now waits for a running check and starts a new one, still never two at once, and a stuck check still times out. The spec also asserts the fixture's removal took effect.
419 lines
21 KiB
JavaScript
419 lines
21 KiB
JavaScript
// Run with: node --test tests/js/
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { createApiClient, resolveUrl, fetchUrl, ApiError, latestOnly, STALE } = require("../../web/js/api-client.js");
|
|
|
|
test("Prompt Manager URLs follow the page's proxy mount without losing query strings", () => {
|
|
const url = "/prompt_manager/images/output?limit=100&subfolder=2025%2F04%2F02";
|
|
assert.equal(resolveUrl(url, "/ComfyBackendDirect/prompt_manager/gallery"),
|
|
"/ComfyBackendDirect" + url);
|
|
assert.equal(resolveUrl(url, "/prompt_manager/gallery"), url);
|
|
assert.equal(resolveUrl(url, "/nested/proxy/prompt_manager/admin"), "/nested/proxy" + url);
|
|
assert.equal(resolveUrl("/other/api?x=1", "/ComfyBackendDirect/prompt_manager/gallery"), "/other/api?x=1");
|
|
assert.equal(resolveUrl("/ComfyBackendDirect/prompt_manager/images/serve/a.png", "/ComfyBackendDirect/prompt_manager/gallery"),
|
|
"/ComfyBackendDirect/prompt_manager/images/serve/a.png");
|
|
assert.equal(resolveUrl("/prompt_manager/images/serve/a.png?root=1", "/ComfyBackendDirect/prompt_manager/gallery"),
|
|
"/ComfyBackendDirect/prompt_manager/images/serve/a.png?root=1");
|
|
});
|
|
|
|
test("fetchUrl sends Prompt Manager requests under the current proxy mount", async () => {
|
|
const previousFetch = globalThis.fetch;
|
|
const previousLocation = globalThis.location;
|
|
const calls = [];
|
|
globalThis.location = { pathname: "/ComfyBackendDirect/prompt_manager/admin" };
|
|
globalThis.fetch = async (url, init) => {
|
|
calls.push([url, init]);
|
|
return { ok: true };
|
|
};
|
|
try {
|
|
await fetchUrl("/prompt_manager/settings?show=all", { method: "GET" });
|
|
assert.deepEqual(calls, [["/ComfyBackendDirect/prompt_manager/settings?show=all", { method: "GET" }]]);
|
|
} finally {
|
|
globalThis.fetch = previousFetch;
|
|
globalThis.location = previousLocation;
|
|
}
|
|
});
|
|
|
|
/** A fetch stand-in whose responses the test resolves by hand, in any order. */
|
|
function fakeFetch() {
|
|
const calls = [];
|
|
const impl = (url, init = {}) =>
|
|
new Promise((resolve, reject) => {
|
|
const call = { url, init, resolve, reject, aborted: false };
|
|
if (init.signal) {
|
|
init.signal.addEventListener("abort", () => {
|
|
call.aborted = true;
|
|
reject(new DOMException("The operation was aborted.", "AbortError"));
|
|
});
|
|
}
|
|
calls.push(call);
|
|
});
|
|
impl.calls = calls;
|
|
return impl;
|
|
}
|
|
|
|
function jsonResponse(status, body, { invalid = false } = {}) {
|
|
return {
|
|
ok: status >= 200 && status < 300,
|
|
status,
|
|
statusText: status === 200 ? "OK" : `Status ${status}`,
|
|
json: async () => {
|
|
if (invalid) throw new SyntaxError("Unexpected token");
|
|
return body;
|
|
},
|
|
};
|
|
}
|
|
|
|
test("get returns the parsed body of a successful response", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const pending = api.get("/prompt_manager/recent?page=1");
|
|
fetch.calls[0].resolve(jsonResponse(200, { success: true, results: [1, 2] }));
|
|
assert.deepEqual(await pending, { success: true, results: [1, 2] });
|
|
assert.equal(fetch.calls[0].init.method, "GET");
|
|
});
|
|
|
|
test("get forwards an AbortSignal to fetch", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const controller = new AbortController();
|
|
const pending = api.get("/x", { signal: controller.signal });
|
|
assert.equal(fetch.calls[0].init.signal, controller.signal);
|
|
controller.abort();
|
|
await assert.rejects(pending, (err) => err.name === "AbortError");
|
|
});
|
|
|
|
test("non-2xx responses throw ApiError carrying the status and server message", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const pending = api.get("/x");
|
|
fetch.calls[0].resolve(jsonResponse(404, { success: false, error: "Prompt not found" }));
|
|
await assert.rejects(pending, (err) => {
|
|
assert.ok(err instanceof ApiError);
|
|
assert.ok(err instanceof Error);
|
|
assert.equal(err.status, 404);
|
|
assert.equal(err.message, "Prompt not found");
|
|
return true;
|
|
});
|
|
});
|
|
|
|
test("non-2xx responses without a JSON body fall back to the status text", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const pending = api.get("/x");
|
|
fetch.calls[0].resolve(jsonResponse(500, null, { invalid: true }));
|
|
await assert.rejects(pending, (err) => err.status === 500 && err.message === "Status 500");
|
|
});
|
|
|
|
test("a 2xx body with success:false throws ApiError", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const pending = api.post("/x", {});
|
|
fetch.calls[0].resolve(jsonResponse(200, { success: false, error: "Nope" }));
|
|
await assert.rejects(pending, (err) => err instanceof ApiError && err.status === 200 && err.message === "Nope");
|
|
});
|
|
|
|
test("ApiError preserves only a bounded machine code without changing status or message", async () => {
|
|
assert.equal(new ApiError(409, "unchanged").code, null);
|
|
for (const [code, expected] of [
|
|
["source-changed", "source-changed"], ["a".repeat(64), "a".repeat(64)],
|
|
[undefined, null], [null, null], [4, null], [{ code: "x" }, null],
|
|
["", null], ["Source-changed", null], ["source_changed", null],
|
|
["source-changed\n", null], ["a".repeat(65), null],
|
|
]) {
|
|
for (const status of [409, 200]) {
|
|
const api = createApiClient(async () => jsonResponse(status, {
|
|
success: false, error: "unchanged", code, private: "never expose",
|
|
}));
|
|
await assert.rejects(api.get("/x"), error => {
|
|
assert.ok(error instanceof ApiError);
|
|
assert.equal(error.status, status);
|
|
assert.equal(error.message, "unchanged");
|
|
assert.equal(error.code, expected);
|
|
assert.equal(error.private, undefined);
|
|
assert.equal(error.body, undefined);
|
|
return true;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
test("a 2xx body that is not JSON throws ApiError", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const pending = api.get("/x");
|
|
fetch.calls[0].resolve(jsonResponse(200, null, { invalid: true }));
|
|
await assert.rejects(pending, (err) => err instanceof ApiError && err.status === 200);
|
|
});
|
|
|
|
test("post, put and del send JSON bodies with the right method", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const p1 = api.post("/a", { text: "x" });
|
|
const p2 = api.put("/b", { rating: 3 });
|
|
const p3 = api.del("/c");
|
|
assert.equal(fetch.calls[0].init.method, "POST");
|
|
assert.equal(fetch.calls[0].init.body, JSON.stringify({ text: "x" }));
|
|
assert.equal(fetch.calls[0].init.headers["Content-Type"], "application/json");
|
|
assert.equal(fetch.calls[1].init.method, "PUT");
|
|
assert.equal(fetch.calls[1].init.body, JSON.stringify({ rating: 3 }));
|
|
assert.equal(fetch.calls[2].init.method, "DELETE");
|
|
assert.equal(fetch.calls[2].init.body, undefined);
|
|
for (const c of fetch.calls) c.resolve(jsonResponse(200, { success: true }));
|
|
await Promise.all([p1, p2, p3]);
|
|
});
|
|
|
|
test("del sends an optional JSON body", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const controller = new AbortController();
|
|
const pending = api.del("/prompt_manager/images/favorite", { body: { root_index: 1, path: "a/b.png" }, signal: controller.signal });
|
|
assert.equal(fetch.calls[0].init.method, "DELETE");
|
|
assert.equal(fetch.calls[0].init.body, JSON.stringify({ root_index: 1, path: "a/b.png" }));
|
|
assert.equal(fetch.calls[0].init.headers["Content-Type"], "application/json");
|
|
assert.equal(fetch.calls[0].init.signal, controller.signal);
|
|
fetch.calls[0].resolve(jsonResponse(200, { success: true }));
|
|
await pending;
|
|
});
|
|
|
|
test("network failures propagate as ordinary errors", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const pending = api.get("/x");
|
|
fetch.calls[0].reject(new TypeError("Failed to fetch"));
|
|
await assert.rejects(pending, (err) => err instanceof TypeError && !(err instanceof ApiError));
|
|
});
|
|
|
|
test("createApiClient without an argument uses the global fetch", async () => {
|
|
const original = globalThis.fetch;
|
|
globalThis.fetch = async () => jsonResponse(200, { success: true, via: "global" });
|
|
try {
|
|
assert.deepEqual(await createApiClient().get("/x"), { success: true, via: "global" });
|
|
} finally {
|
|
globalThis.fetch = original;
|
|
}
|
|
});
|
|
|
|
test("latestOnly: a newer call aborts the older one and the older result is STALE", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const latest = latestOnly();
|
|
|
|
const first = latest((signal) => api.get("/search?q=a", { signal }));
|
|
const second = latest((signal) => api.get("/search?q=ab", { signal }));
|
|
|
|
assert.equal(fetch.calls[0].aborted, true, "first request aborted");
|
|
assert.equal(fetch.calls[1].aborted, false);
|
|
|
|
fetch.calls[1].resolve(jsonResponse(200, { success: true, q: "ab" }));
|
|
assert.deepEqual(await second, { success: true, q: "ab" });
|
|
assert.equal(await first, STALE);
|
|
});
|
|
|
|
test("latestOnly: an older response that arrives after a newer request is STALE even without abort", async () => {
|
|
const latest = latestOnly();
|
|
let resolveFirst;
|
|
const first = latest(() => new Promise((r) => { resolveFirst = r; }));
|
|
const second = latest(async () => "new");
|
|
assert.equal(await second, "new");
|
|
resolveFirst("old");
|
|
assert.equal(await first, STALE);
|
|
});
|
|
|
|
test("latestOnly: sequential calls each return their own result", async () => {
|
|
const latest = latestOnly();
|
|
assert.equal(await latest(async () => 1), 1);
|
|
assert.equal(await latest(async () => 2), 2);
|
|
});
|
|
|
|
test("latestOnly: errors from the current call propagate, errors from superseded calls do not", async () => {
|
|
const latest = latestOnly();
|
|
let rejectFirst;
|
|
const first = latest(() => new Promise((_, rej) => { rejectFirst = rej; }));
|
|
const second = latest(async () => { throw new Error("boom"); });
|
|
await assert.rejects(second, /boom/);
|
|
rejectFirst(new Error("old failure"));
|
|
assert.equal(await first, STALE);
|
|
});
|
|
|
|
test("latestOnly with a lifetime: once it ends, no request starts and the call is STALE", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const page = new AbortController();
|
|
const latest = latestOnly({ lifetime: page.signal });
|
|
page.abort();
|
|
assert.equal(await latest((signal) => api.get("/recent", { signal })), STALE);
|
|
assert.equal(fetch.calls.length, 0, "a leaving page starts no read");
|
|
});
|
|
|
|
test("latestOnly with a lifetime: its end aborts the request in flight, which is STALE, not an error", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const page = new AbortController();
|
|
const latest = latestOnly({ lifetime: page.signal });
|
|
const call = latest((signal) => api.get("/recent", { signal }));
|
|
page.abort();
|
|
assert.equal(fetch.calls[0].aborted, true, "the read in flight is aborted");
|
|
fetch.calls[0].reject(new DOMException("The operation was aborted.", "AbortError"));
|
|
assert.equal(await call, STALE);
|
|
});
|
|
|
|
test("latestOnly with a lifetime: real errors still propagate while the page lives", async () => {
|
|
const page = new AbortController();
|
|
const latest = latestOnly({ lifetime: page.signal });
|
|
await assert.rejects(latest(async () => { throw new Error("server down"); }), /server down/);
|
|
assert.equal(await latest(async () => "fine"), "fine");
|
|
});
|
|
|
|
test("ApiError exposes only validated history recovery actions on all error paths", async () => {
|
|
const permitted = [null, { action: "refresh-history" }, { action: "reload-outputs" },
|
|
{ action: "retry-read" }, { action: "smaller-limit", limit: 1 },
|
|
{ action: "smaller-limit", limit: 100 }];
|
|
const denied = [undefined, false, "refresh-history", [], {}, { action: "unknown" },
|
|
{ action: "refresh-history", private: "secret" }, { action: "retry-read", limit: 1 },
|
|
{ action: "smaller-limit" }, { action: "smaller-limit", limit: 0 },
|
|
{ action: "smaller-limit", limit: 101 }, { action: "smaller-limit", limit: true },
|
|
{ action: "smaller-limit", limit: "1" }, { action: "smaller-limit", limit: 1.5 },
|
|
{ action: "smaller-limit", limit: NaN }, { action: "smaller-limit", limit: Infinity },
|
|
{ action: "smaller-limit", limit: 10, url: "/private" },
|
|
Object.assign(Object.create({ action: "retry-read" }), { private: "secret" })];
|
|
assert.equal(new ApiError(409, "old caller").recovery, null);
|
|
for (const recovery of permitted.concat(denied)) {
|
|
for (const status of [409, 200]) {
|
|
const api = createApiClient(async () => jsonResponse(status, {
|
|
success: false, error: "bounded", code: "history-changed", recovery,
|
|
}));
|
|
await assert.rejects(api.get("/x"), error => {
|
|
assert.ok(error instanceof ApiError);
|
|
assert.equal(error.code, "history-changed");
|
|
assert.deepEqual(error.recovery, permitted.includes(recovery) ? recovery : null);
|
|
if (recovery && permitted.includes(recovery)) {
|
|
assert.notEqual(error.recovery, recovery, "server objects must be copied");
|
|
}
|
|
return true;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
test("patch sends a JSON body with the PATCH method and forwards an AbortSignal", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
assert.equal(typeof api.patch, "function");
|
|
const controller = new AbortController();
|
|
const pending = api.patch("/prompt_manager/library/7", { metadata: { rating: null } }, { signal: controller.signal });
|
|
assert.equal(fetch.calls[0].init.method, "PATCH");
|
|
assert.equal(fetch.calls[0].init.body, JSON.stringify({ metadata: { rating: null } }));
|
|
assert.equal(fetch.calls[0].init.headers["Content-Type"], "application/json");
|
|
assert.equal(fetch.calls[0].init.signal, controller.signal);
|
|
fetch.calls[0].resolve(jsonResponse(409, { success: false, code: "metadata-conflict", error: "changed" }));
|
|
await assert.rejects(pending, error => error instanceof ApiError && error.status === 409 && error.code === "metadata-conflict");
|
|
});
|
|
|
|
test("get forwards an explicit cache mode and keeps the default request otherwise", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const fresh = api.get("/prompt_manager/library/options", { cache: "no-store" });
|
|
const plain = api.get("/prompt_manager/library/options");
|
|
assert.equal(fetch.calls[0].init.cache, "no-store");
|
|
assert.equal("cache" in fetch.calls[1].init, false);
|
|
assert.deepEqual(fetch.calls[1].init, { method: "GET", headers: {} });
|
|
for (const call of fetch.calls) call.resolve(jsonResponse(200, { success: true }));
|
|
await Promise.all([fresh, plain]);
|
|
const writes = api.post("/x", {}, { cache: "no-store" });
|
|
assert.equal("cache" in fetch.calls[2].init, false, "only reads take a cache mode");
|
|
fetch.calls[2].resolve(jsonResponse(200, { success: true }));
|
|
await writes;
|
|
});
|
|
|
|
test("ApiError exposes only a validated current tag set from a tag-conflict body", async () => {
|
|
const permitted = [{ tags: [], tag_revision: 0 }, { tags: ["warm", "Dune"], tag_revision: 7 }];
|
|
const denied = [undefined, null, { tags: ["a"] }, { tag_revision: 1 }, { tags: "a", tag_revision: 1 },
|
|
{ tags: [1], tag_revision: 1 }, { tags: ["a"], tag_revision: -1 }, { tags: ["a"], tag_revision: 1.5 },
|
|
{ tags: ["a"], tag_revision: true }, { tags: Array(101).fill("a"), tag_revision: 1 }];
|
|
assert.equal(new ApiError(409, "old caller").current, null);
|
|
for (const current of permitted.concat(denied)) {
|
|
const api = createApiClient(async () => jsonResponse(409, {
|
|
success: false, error: "changed", code: "tag-conflict", ...(current || {}),
|
|
}));
|
|
await assert.rejects(api.put("/x", {}), error => {
|
|
assert.ok(error instanceof ApiError);
|
|
assert.deepEqual(error.current, permitted.includes(current) ? current : null);
|
|
if (permitted.includes(current)) assert.notEqual(error.current.tags, current.tags, "server arrays must be copied");
|
|
return true;
|
|
});
|
|
}
|
|
});
|
|
|
|
test("keepalive is forwarded only when asked, so a request can outlive its page", async () => {
|
|
const fetch = fakeFetch();
|
|
const api = createApiClient(fetch);
|
|
const writes = [api.del("/x", { keepalive: true }), api.put("/x", {}, { keepalive: true }), api.post("/x", {}, { keepalive: true }),
|
|
api.del("/x"), api.put("/x", {}), api.get("/x")];
|
|
assert.deepEqual(fetch.calls.map(call => call.init.keepalive), [true, true, true, undefined, undefined, undefined]);
|
|
assert.equal("keepalive" in fetch.calls[3].init, false);
|
|
for (const call of fetch.calls) call.resolve(jsonResponse(200, { success: true }));
|
|
await Promise.all(writes);
|
|
});
|
|
|
|
test("ApiError keeps the job that applied a used plan (plan-used, System S7)", () => {
|
|
const body = { job: { id: "b".repeat(32), title: "Optimize database" } };
|
|
const used = new ApiError(409, "This plan was already applied.", "plan-used", null, body);
|
|
assert.deepEqual(used.job, { id: "b".repeat(32), title: "Optimize database" });
|
|
assert.notEqual(used.job, body.job, "server objects must be copied");
|
|
// The applying job may already be pruned: its title is null, its id still links Activity.
|
|
assert.deepEqual(new ApiError(409, "used", "plan-used", null, { job: { id: "c".repeat(32), title: null } }).job,
|
|
{ id: "c".repeat(32), title: null });
|
|
assert.equal(new ApiError(409, "busy", "busy", null, body).job, null);
|
|
for (const job of [null, "x", { id: 1, title: "t" }, { id: "a b", title: "t" }, { id: "a", title: 7 },
|
|
{ id: "a", title: "x".repeat(201) }, { id: "a".repeat(65), title: "t" }]) {
|
|
assert.equal(new ApiError(409, "used", "plan-used", null, { job }).job, null);
|
|
}
|
|
});
|
|
|
|
test("ApiError keeps a move refused for space with its review (#320)", () => {
|
|
const plan = { totals: { files: 3 }, insufficient: { text: "Not enough space" } };
|
|
assert.equal(new ApiError(507, "no space", "insufficient-space", null, { plan }).plan, plan);
|
|
for (const bad of [null, "x", [1], undefined]) {
|
|
assert.equal(new ApiError(507, "no space", "insufficient-space", null, { plan: bad }).plan, null);
|
|
}
|
|
assert.equal(new ApiError(409, "busy", "busy", null, { plan }).plan, null);
|
|
assert.equal(new ApiError(507, "no space", "insufficient-space").plan, null);
|
|
});
|
|
|
|
test("ApiError keeps a validated running job on busy and a validated source on invalid-source", () => {
|
|
const body = { running: { id: "a".repeat(32), title: "Scan for metadata" }, source: "output/gone" };
|
|
const busy = new ApiError(409, "busy", "busy", null, body);
|
|
assert.deepEqual([busy.running, busy.source], [{ id: "a".repeat(32), title: "Scan for metadata" }, null]);
|
|
assert.notEqual(busy.running, body.running, "server objects must be copied");
|
|
const invalid = new ApiError(400, "refused", "invalid-source", null, body);
|
|
assert.deepEqual([invalid.running, invalid.source], [null, "output/gone"]);
|
|
for (const running of [null, "x", { id: 1, title: "t" }, { id: "a b", title: "t" }, { id: "a", title: "" },
|
|
{ id: "a", title: "x".repeat(201) }, { id: "a".repeat(65), title: "t" }]) {
|
|
assert.equal(new ApiError(409, "busy", "busy", null, { running }).running, null);
|
|
}
|
|
for (const source of [null, 7, "", "x".repeat(4097)]) {
|
|
assert.equal(new ApiError(400, "refused", "invalid-source", null, { source }).source, null);
|
|
}
|
|
assert.deepEqual([new ApiError(409, "old caller").running, new ApiError(409, "old caller").source], [null, null]);
|
|
});
|
|
|
|
test("ApiError keeps a validated operation on model-busy only (#328)", () => {
|
|
const body = { operation: "Tagging model download · WD14 ViT" };
|
|
assert.equal(new ApiError(409, "busy", "model-busy", null, body).operation, "Tagging model download · WD14 ViT");
|
|
assert.equal(new ApiError(409, "busy", "busy", null, body).operation, null);
|
|
for (const operation of [null, 7, "", "x".repeat(201)]) {
|
|
assert.equal(new ApiError(409, "busy", "model-busy", null, { operation }).operation, null);
|
|
}
|
|
assert.equal(new ApiError(409, "busy", "model-busy").operation, null);
|
|
});
|
|
|
|
test("ApiError says whether the server itself refused (a parsed success:false body; S7 review I1)", () => {
|
|
assert.equal(new ApiError(400, "bad", null, null, { success: false, error: "bad" }).refused, true);
|
|
assert.equal(new ApiError(409, "busy", "busy", null, { success: false, code: "busy" }).refused, true);
|
|
// A proxy's page, a body that was not JSON, or no body at all: nothing says the server refused.
|
|
for (const body of [null, undefined, "<html>", { success: true }, [], { error: "x" }]) {
|
|
assert.equal(new ApiError(504, "Gateway Timeout", null, null, body).refused, false);
|
|
}
|
|
});
|