Files
ComfyUI-H3-Continuum-Tiled/tests/frontend_storage_settings.mjs

129 lines
7.9 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
import vm from "node:vm";
class Element extends EventTarget {
constructor(tag) { super(); this.tag = tag; this.children = []; this.style = {}; this.dataset = {}; this.attributes = {}; this.value = ""; this.disabled = false; this.textContent = ""; }
append(child) { child.parentElement = this; this.children.push(child); }
replaceChildren() { for (const child of this.children) child.parentElement = null; this.children = []; }
setAttribute(name, value) { this.attributes[name] = value; }
get isConnected() { return this === document.body || !!this.parentElement?.isConnected; }
closest() { let node = this; while (node) { if (node.tag === "dialog" || node.attributes.role === "dialog") return node; node = node.parentElement; } return null; }
remove() { this.parentElement.children = this.parentElement.children.filter(x => x !== this); this.parentElement = null; }
showModal() { this.open = true; }
close() { this.open = false; this.dispatchEvent(new Event("close")); }
focus() {}
click() { if (!this.disabled) this.dispatchEvent(new Event("click")); }
}
const all = [];
globalThis.document = { createElement: tag => { const e = new Element(tag); all.push(e); return e; }, body: new Element("body") };
Object.defineProperty(globalThis, "navigator", { value: { language: "en" }, configurable: true });
let definition;
globalThis.__app = { ui: { settings: { getSettingValue: () => "en", addSetting: d => { definition = d; } } }, registerExtension: d => d.setup() };
const api = globalThis.__api = new EventTarget();
const requests = [];
let responseMode = "normal";
let state = { path: "", revision: 0, busy: false, read_only: false, memory: { available_bytes: 8 * 1024 ** 3 }, roots: [], volumes: [], last_backend: null };
api.fetchApi = async (path, options) => {
const action = path.split("/").at(-1);
const body = options.body ? JSON.parse(options.body) : undefined;
requests.push({ action, body, path, options });
if (responseMode === "network") throw new TypeError("fetch failed");
if (responseMode === "both404") return new Response("", { status: 404 });
if (responseMode === "empty200") return new Response("", { status: 200 });
if (responseMode === "empty403") return new Response("", { status: 403 });
if (responseMode === "empty500") return new Response("", { status: 500 });
if (responseMode === "html") return new Response("<!doctype html><title>ComfyUI</title>", { status: 200 });
if (responseMode === "schema") return Response.json({});
if (responseMode === "cleanupEmpty" && action === "cleanup") return new Response("", { status: 200 });
let value = structuredClone(state), ok = true;
if (action === "settings") { assert.equal(body.revision, state.revision); state = { ...state, path: body.path, revision: state.revision + 1 }; value = state; }
if (action === "validate") {
ok = !body.path || body.path.startsWith("/");
value = ok ? { path: body.path, roots: [{ free_bytes: 10 * 1024 ** 3 }] } : { error: "absolute path required" };
}
if (action === "cleanup") { assert.deepEqual(body, {}); value = { roots: [] }; }
return Response.json(value, { status: ok ? 200 : 400 });
};
const source = fs.readFileSync(new URL("../web/storage_settings.js", import.meta.url), "utf8")
.replace('import { app } from "../../scripts/app.js";', "const app = globalThis.__app;")
.replace('import { api } from "../../scripts/api.js";', "const api = globalThis.__api;")
.replace("export function openStorageDialog", "function openStorageDialog");
vm.runInThisContext(source);
const settings = new Element("section"); settings.setAttribute("role", "dialog"); document.body.append(settings);
const row = definition.type(); settings.append(row);
const launch = row.children[0].children[0];
const connected = tag => all.filter(x => x.tag === tag && x.isConnected);
const dialog = () => connected("dialog")[0];
const control = name => all.find(x => x.tag === "button" && x.isConnected && x.textContent === name);
const settle = () => new Promise(resolve => setImmediate(resolve));
launch.click(); await settle();
assert.equal(dialog().parentElement, settings, "preserve Core's settings dialog during native-modal interactions");
assert.equal(connected("input")[0].value, "");
assert.equal(requests.filter(r => r.action === "status").length, 1);
launch.click(); assert.equal(connected("dialog").length, 1, "no duplicate modal");
const input = connected("input")[0];
input.value = "relative"; input.dispatchEvent(new Event("input"));
control("Validate").click(); await settle();
assert.ok(all.some(x => x.isConnected && x.textContent === "absolute path required"));
input.value = "/scratch"; input.dispatchEvent(new Event("input"));
control("Save").click(); control("Save").click(); await settle();
assert.equal(requests.filter(r => r.action === "settings").length, 1, "one save while request is pending");
assert.equal(state.path, "/scratch");
const status = new Event("status"); status.detail = { exec_info: { queue_remaining: 1 } }; api.dispatchEvent(status);
assert.equal(control("Save").disabled, true);
assert.equal(control("Delete unused temporary files").disabled, true);
status.detail.exec_info.queue_remaining = 0; api.dispatchEvent(status);
control("Delete unused temporary files").click(); await settle();
assert.equal(requests.filter(r => r.action === "cleanup").length, 1);
control("Close").click(); assert.equal(connected("dialog").length, 0);
const before = requests.length;
for (let n = 0; n < 100; n++) {
launch.click(); await settle();
assert.equal(connected("input")[0].value, "/scratch");
control("Close").click();
}
await settle();
assert.equal(requests.length - before, 100, "one fetch per open and no periodic polling");
assert.equal(connected("dialog").length, 0);
// A disposed dialog's handler must not receive later status events.
api.dispatchEvent(status);
assert.equal(requests.length - before, 100);
state.read_only = true;
launch.click(); await settle();
assert.equal(control("Save").disabled, true);
assert.equal(control("Delete unused temporary files").disabled, true);
assert.equal(connected("input")[0].disabled, true);
control("Close").click();
state.read_only = false;
for (const [mode, text, count] of [["both404", "HTTP 404", 1], ["empty403", "HTTP 403", 1],
["empty500", "HTTP 500", 1], ["empty200", "HTTP 200", 1], ["html", "HTTP 200", 1],
["schema", "HTTP 200", 1], ["network", "current address", 1]]) {
responseMode = mode;
const before = requests.length;
launch.click(); await settle();
assert.equal(requests.length - before, count, mode);
assert.ok(all.some(x => x.isConnected && x.attributes.role === "status" && x.textContent.includes(text)), mode);
assert.equal(connected("input")[0].disabled, true, "no mutations with unavailable status");
assert.equal(control("Save").disabled, true);
assert.equal(connected("select")[0].disabled, true);
assert.ok(connected("select")[0].children[0].textContent.includes("Refresh"));
assert.equal(control("Refresh").disabled, false, "manual recovery stays available");
responseMode = "normal";
control("Refresh").click(); await settle();
assert.equal(control("Save").disabled, false, "recover without reloading the browser");
control("Close").click();
}
// A successful save/cleanup with an empty response must never be replayed.
responseMode = "normal"; launch.click(); await settle();
responseMode = "cleanupEmpty";
const beforeCleanup = requests.length;
control("Delete unused temporary files").click(); await settle();
assert.equal(requests.length - beforeCleanup, 1, "never retry a mutating request");
assert.ok(all.some(x => x.isConnected && x.attributes.role === "status" && x.textContent.includes("HTTP 200")));
control("Close").click();
console.log("Temporary storage frontend: settings nesting, deduplication, errors, save, busy/read-only gates, cleanup, and 100 reopen cycles passed");
console.log("Empty/invalid HTTP responses, recovery, and no POST replay passed");