Publish curated ITWorx-ModelForge source from 990401737f96c8d46a9f7b0d84994516cadcb6a0

This commit is contained in:
Public Source Publisher committed 2026-09-30 09:23:17 +00:00
1 parent 7082ab955a
commit 848f0d8f8a
434 files changed
+67298 -4315

No files matched your search

@@ -1,6 +1,7 @@
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { setOperatorCredential, clearOperatorCredential } from "../lib/api";
import { CapabilityWorkspace } from "./CapabilityWorkspace";
import type { CapabilityDeployment, SchedulerBudget } from "../types";
@@ -35,6 +36,7 @@ function mockServing(options: { deployments?: CapabilityDeployment[]; invokeErro
const url = String(input);
let payload: unknown = [];
let status = 200;
if (url.endsWith("/api/v1/capabilities")) payload = [{ key: "rag.embedding", version: 1, description: "Local retrieval", stable_deployment: null, contract: { capability: "rag.embedding", version: 1, description: "Local retrieval", modalities: { input: ["text"], output: ["vector"] }, upgrade_class: "requires_reindex", vector: { dimensionality: 1024, normalized: true, cross_deployment_compatible: false }, slo: {}, privacy: { classification: "confidential", allow_persistence: false, allow_logging_payloads: false, allow_network_egress: false }, resources: { accelerator_required: true, cpu_fallback_allowed: false }, fallback: { allowed: false, mode: "hard_fail" }, production_priority: "production", default_residency: "keep_warm", estate: { category: "RAG", purpose: "Local retrieval", stability: "stable", resource_class: "MEDIUM", evaluation_type: "retrieval", consumers: [] } } }];
if (url.endsWith("/api/v1/capability-deployments")) payload = options.deployments ?? [deployment];
if (url.endsWith("/api/v1/capability-estate")) payload = [
{ capability: "rag.embedding", version: 1, category: "RAG", purpose: "Local retrieval", declared_stability: "stable", operational_state: options.deployments?.length === 0 ? "not_deployed" : "stable", current_deployment_id: "deployment-1", model: "Nomic", revision: "a".repeat(40), runtime: "sentence_transformers", node: "node-1", resource_class: "MEDIUM", measured_required_vram_bytes: 1_600_000_000, consumers: ["examplerag"], privacy_class: "confidential", evaluation_type: "retrieval", evaluation_state: "completed" },
@@ -59,25 +61,27 @@ function mockServing(options: { deployments?: CapabilityDeployment[]; invokeErro
}));
}
afterEach(() => { cleanup(); vi.unstubAllGlobals(); });
afterEach(() => { clearOperatorCredential(); cleanup(); vi.unstubAllGlobals(); vi.restoreAllMocks(); });
describe("M5 capability operations UI", () => {
it("shows the healthy stable capability and warm residency", async () => {
mockServing(); render(<CapabilityWorkspace onError={vi.fn()} />);
mockServing(); setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
expect(await screen.findByText("rag.embedding@1")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Inspect rag.embedding@1" }));
fireEvent.click(screen.getByRole("button", { name: "Inspect deployment deployment-1" }));
expect(screen.getByText("healthy")).toBeInTheDocument();
expect(screen.getAllByText("warm").length).toBeGreaterThan(0);
expect(screen.getByText("requires_reindex")).toBeInTheDocument();
expect(screen.getAllByText("requires_reindex").length).toBeGreaterThan(0);
});
it("shows unavailable honestly when promotion has not happened", async () => {
mockServing({ deployments: [] }); render(<CapabilityWorkspace onError={vi.fn()} />);
expect(await screen.findByText("unavailable")).toBeInTheDocument();
expect(screen.getByText(/No stable deployment exists/)).toBeInTheDocument();
fireEvent.click(await screen.findByRole("button", { name: "Inspect rag.embedding@1" }));
expect(screen.getByText(/No deployment records exist/)).toBeInTheDocument();
});
it("renders real scheduler budget categories including external usage", async () => {
mockServing(); render(<CapabilityWorkspace onError={vi.fn()} />);
mockServing(); setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "scheduler" }));
expect(screen.getByText(/GPU SCHEDULING COCKPIT · GPU Node/)).toBeInTheDocument();
expect(screen.getByText("External / unmanaged")).toBeInTheDocument();
@@ -86,17 +90,15 @@ describe("M5 capability operations UI", () => {
});
it("shows a newly created credential exactly in the one-time result", async () => {
mockServing(); render(<CapabilityWorkspace onError={vi.fn()} />);
mockServing(); setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "clients" }));
fireEvent.change(screen.getByLabelText("Operator API key"), { target: { value: "admin-test" } });
fireEvent.click(screen.getByRole("button", { name: "Unlock operational view" }));
fireEvent.click(await screen.findByRole("button", { name: /Create one-time credential/ }));
expect(await screen.findByText("mf_test_one_time_secret")).toBeInTheDocument();
expect(screen.getByText(/Only its hash is stored/)).toBeInTheDocument();
});
it("invokes by capability without asking for a concrete model name", async () => {
mockServing(); render(<CapabilityWorkspace onError={vi.fn()} />);
mockServing(); setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "playground" }));
expect(screen.queryByLabelText(/model name/i)).not.toBeInTheDocument();
expect(screen.getByRole("option", { name: "document.ocr@1" })).toBeInTheDocument();
@@ -116,18 +118,105 @@ describe("M5 capability operations UI", () => {
});
});
describe("Capability state read semantics", () => {
it("shows a typed initial failure and retries instead of displaying an empty estate", async () => {
mockServing();
const normalFetch = fetch;
let failFirstRead = true;
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL, init?: RequestInit) => {
if (failFirstRead && String(input).endsWith("/api/v1/capability-estate")) {
failFirstRead = false;
return Promise.resolve(new Response(JSON.stringify({ error: {
code: "SERVICE_UNAVAILABLE", message: "Capability estate read failed", correlation_id: "corr-capability-123",
details: { failure_code: "CAPABILITY_READ_UNAVAILABLE" },
} }), { status: 503 }));
}
return normalFetch(input, init);
}));
const onError = vi.fn();
render(<CapabilityWorkspace onError={onError} />);
expect(await screen.findByRole("alert")).toHaveTextContent("Capability serving state is unavailable");
expect(screen.getByText(/No authoritative snapshot was loaded/)).toBeInTheDocument();
expect(screen.getByText("CAPABILITY_READ_UNAVAILABLE")).toBeInTheDocument();
expect(screen.getByText("corr-capability-123")).toBeInTheDocument();
expect(screen.getByText("Yes")).toBeInTheDocument();
expect(screen.queryByText(/0 declared contracts/)).not.toBeInTheDocument();
expect(screen.queryByText(/0 deployment records/)).not.toBeInTheDocument();
expect(onError).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole("button", { name: "Retry capability state" }));
expect(await screen.findByRole("heading", { name: "1 declared contracts" })).toBeInTheDocument();
expect(screen.getByText("1 deployment records")).toBeInTheDocument();
});
it("discloses an offline polling failure with its stale snapshot and recovers on retry", async () => {
mockServing();
const pollCallbacks: Array<() => void> = [];
vi.spyOn(window, "setInterval").mockImplementation(((callback: TimerHandler) => {
if (typeof callback === "function") pollCallbacks.push(callback as () => void);
return 1;
}) as typeof window.setInterval);
vi.spyOn(window, "clearInterval").mockImplementation(() => undefined);
const onError = vi.fn();
render(<CapabilityWorkspace onError={onError} />);
expect(await screen.findByRole("heading", { name: "1 declared contracts" })).toBeInTheDocument();
expect(pollCallbacks.length).toBeGreaterThan(0);
const normalFetch = fetch;
let failNextRead = true;
let offlineRequestAttempted = false;
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL, init?: RequestInit) => {
if (failNextRead && String(input).endsWith("/api/v1/capability-estate")) {
failNextRead = false;
offlineRequestAttempted = true;
return Promise.reject(new TypeError("Failed to fetch"));
}
return normalFetch(input, init);
}));
await act(async () => { pollCallbacks.forEach(callback => callback()); });
expect(offlineRequestAttempted).toBe(true);
expect(await screen.findByText("Capability data may be out of date")).toBeInTheDocument();
expect(screen.getByText("OFFLINE")).toBeInTheDocument();
expect(screen.getByText("NETWORK_UNAVAILABLE")).toBeInTheDocument();
expect(screen.getByText("Retryable").parentElement).toHaveTextContent("Yes");
expect(screen.getByRole("heading", { name: "1 declared contracts" })).toBeInTheDocument();
expect(screen.getByText("1 deployment records")).toBeInTheDocument();
expect(onError).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole("button", { name: "Retry capability state" }));
await waitFor(() => expect(screen.queryByText("Capability data may be out of date")).not.toBeInTheDocument());
expect(screen.getByRole("heading", { name: "1 declared contracts" })).toBeInTheDocument();
});
it("keeps manual refresh inside the public boundary when an admin-role session is inactive", async () => {
mockServing();
setOperatorCredential("viewer-session");
const normalFetch = fetch;
const requestedUrls: string[] = [];
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL, init?: RequestInit) => {
requestedUrls.push(String(input));
return normalFetch(input, init);
}));
render(<CapabilityWorkspace role="viewer" onError={vi.fn()} />);
expect(await screen.findByRole("heading", { name: "1 declared contracts" })).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Refresh capability state" }));
await waitFor(() => expect(requestedUrls.filter(url => url.endsWith("/api/v1/capability-deployments")).length).toBe(2));
expect(requestedUrls.some(url => url.includes("/api/v1/admin/"))).toBe(false);
});
});
describe("M18 closure · capability tabs and guarded residency unload", () => {
async function openScheduler(calls: string[]) {
mockServing({ calls });
render(<CapabilityWorkspace onError={vi.fn()} />);
setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "scheduler" }));
fireEvent.change(screen.getByLabelText("Operator API key"), { target: { value: "admin-test" } });
fireEvent.click(screen.getByRole("button", { name: "Unlock operational view" }));
return await screen.findByRole("button", { name: "Unload idle" });
}
it("pairs each capability tab with the panel it controls", async () => {
mockServing(); render(<CapabilityWorkspace onError={vi.fn()} />);
mockServing(); setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
await screen.findByRole("tablist", { name: "Capability operations" });
const selected = screen.getByRole("tab", { selected: true });
const panel = screen.getByRole("tabpanel");
@@ -184,3 +273,82 @@ describe("M18 closure · capability tabs and guarded residency unload", () => {
expect(calls).toHaveLength(0);
});
});
describe("Capability administrative role boundary", () => {
it.each(["viewer", "operator"] as const)("does not fetch admin data or offer another credential for %s", async role => {
mockServing(); setOperatorCredential("test-read-session"); render(<CapabilityWorkspace role={role} onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "promotion" }));
expect(screen.getByText(/Read-only access/)).toBeInTheDocument();
expect(screen.queryByLabelText("Operator API key")).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: /Record reviewed/ })).not.toBeInTheDocument();
expect(vi.mocked(fetch).mock.calls.some(([url]) => String(url).includes("/admin/"))).toBe(false);
});
it("keeps a locked admin session read-only", async () => {
mockServing(); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "promotion" }));
expect(screen.getByText(/Read-only access/)).toBeInTheDocument();
expect(vi.mocked(fetch).mock.calls.some(([url]) => String(url).includes("/admin/"))).toBe(false);
});
it("renders nullable embedding metadata without blocking access to promotion", async () => {
mockServing({ deployments: [{ ...deployment, embedding_space: null }] }); setOperatorCredential("test-admin"); render(<CapabilityWorkspace role="admin" onError={vi.fn()} />);
fireEvent.click(await screen.findByRole("tab", { name: "promotion" }));
expect(screen.getByRole("heading", { name: "Promotion evidence" })).toBeInTheDocument();
});
});
describe("Capability declaration refresh ordering", () => {
async function pendingRefreshes() {
mockServing();
const onError = vi.fn();
render(<CapabilityWorkspace onError={onError} />);
fireEvent.click(await screen.findByRole("button", { name: "Inspect rag.embedding@1" }));
await screen.findByRole("region", { name: "Details for rag.embedding@1" });
const originalFetch = fetch;
const pending: Array<(response: Response) => void> = [];
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL, init?: RequestInit) => {
if (String(input).endsWith("/api/v1/capabilities")) return new Promise<Response>(resolve => pending.push(resolve));
return originalFetch(input, init);
}));
const refresh = () => fireEvent.click(screen.getByRole("button", { name: "Refresh capability state" }));
const finish = async (index: number, success: boolean) => {
await act(async () => {
const response = success ? await originalFetch("/api/v1/capabilities") : new Response(JSON.stringify({ detail: "Declarations unavailable" }), { status: 503 });
pending[index](response);
});
};
return { refresh, finish, onError };
}
it("keeps the last good snapshot disclosed after a newer refresh failed", async () => {
const { refresh, finish } = await pendingRefreshes();
refresh(); refresh();
await finish(1, false);
expect(screen.getByText("Capability data may be out of date")).toBeInTheDocument();
expect(screen.getByRole("region", { name: "Details for rag.embedding@1" })).toBeInTheDocument();
await finish(0, true);
expect(screen.getByText("Capability data may be out of date")).toBeInTheDocument();
expect(screen.getByRole("region", { name: "Details for rag.embedding@1" })).toBeInTheDocument();
});
it("does not clear newer declarations or report an older refresh failure", async () => {
const { refresh, finish, onError } = await pendingRefreshes();
refresh(); refresh();
await finish(1, true);
onError.mockClear();
await finish(0, false);
expect(screen.getByRole("region", { name: "Details for rag.embedding@1" })).toBeInTheDocument();
expect(onError).not.toHaveBeenCalled();
});
it("ignores a pending contract failure after the operator session changes", async () => {
const { refresh, finish, onError } = await pendingRefreshes();
refresh();
setOperatorCredential("replacement-viewer-session");
onError.mockClear();
await finish(0, false);
expect(onError).not.toHaveBeenCalled();
expect(screen.getByRole("region", { name: "Details for rag.embedding@1" })).toBeInTheDocument();
});
});