Files
storyforge/web/storyforge-web-v4/tests/workbench-pages.test.mjs
2026-03-29 16:13:50 +08:00

143 lines
8.3 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
const ROOT = path.resolve(process.cwd(), "web/storyforge-web-v4");
const HTML = fs.readFileSync(path.join(ROOT, "index.html"), "utf8");
const APP = fs.readFileSync(path.join(ROOT, "assets/app.js"), "utf8");
const CSS = fs.readFileSync(path.join(ROOT, "assets/styles.css"), "utf8");
function extractBetween(source, startToken, endToken) {
const start = source.indexOf(startToken);
assert.notEqual(start, -1, `Missing token: ${startToken}`);
const end = source.indexOf(endToken, start);
assert.notEqual(end, -1, `Missing token: ${endToken}`);
return source.slice(start, end);
}
test("settings navigation and screen are real routes", () => {
assert.match(HTML, /data-screen-target="settings"/);
assert.match(HTML, /data-screen="settings"/);
assert.match(APP, /function renderSettingsScreen\(/);
assert.match(APP, /screenMap\.settings\.innerHTML = renderSettingsScreen\(\);/);
assert.match(APP, /window\.addEventListener\("hashchange"/);
});
test("automation screen stays user-facing and excludes admin-only panels", () => {
const source = extractBetween(APP, "function renderAutomationScreen()", "function renderOwnedScreen()");
assert.doesNotMatch(source, /renderTenantQuotaPanel\(/);
assert.doesNotMatch(source, /renderOneLinerActionRegistryPanel\(/);
assert.doesNotMatch(source, /renderAdminOpsPanel\(/);
assert.match(source, /renderDetailTabs\("automationDetailTab"/);
});
test("agent screen excludes quota and registry panels and uses page tabs", () => {
const source = extractBetween(APP, "function renderPlaybookScreen()", "function renderProductionScreen()");
assert.doesNotMatch(source, /renderTenantQuotaPanel\(/);
assert.doesNotMatch(source, /renderOneLinerActionRegistryPanel\(/);
assert.match(source, /renderDetailTabs\("playbookDetailTab"/);
assert.match(source, /renderGovernanceSummaryCard\(/);
assert.match(source, /open-user-global-policy/);
assert.match(source, /open-user-platform-policy/);
});
test("discovery, production, and admin screens use page tabs for heavy content", () => {
const discovery = extractBetween(APP, "function renderDiscoveryScreen()", "function renderTrackingScreen()");
const production = extractBetween(APP, "function renderProductionScreen()", "function renderReviewScreen()");
const admin = extractBetween(APP, "function renderAdminWorkbenchScreen()", "function renderDashboardScreen()");
assert.match(discovery, /renderDetailTabs\("discoveryDetailTab"/);
assert.match(production, /renderDetailTabs\("productionDetailTab"/);
assert.match(admin, /renderDetailTabs\("adminWorkbenchTab"/);
assert.match(admin, /renderAdminGovernanceSummaryPanel\(/);
});
test("projects screen uses an adaptive project grid instead of a fixed three-column squeeze", () => {
const projects = extractBetween(APP, "function renderProjectsScreen()", "function getActiveDetailTab(");
assert.match(projects, /project-status-grid/);
assert.match(CSS, /\.project-status-grid\s*\{[\s\S]*repeat\(auto-fit,\s*minmax\(260px,\s*1fr\)\)/);
assert.match(CSS, /\.entity-card\.pad\s*\{[\s\S]*padding:\s*15px/);
});
test("dashboard and project screens distinguish auto-connecting from truly disconnected", () => {
const dashboard = extractBetween(APP, "function renderDashboardScreen()", "function renderProjectsScreen()");
const projects = extractBetween(APP, "function renderProjectsScreen()", "function getActiveDetailTab(");
const helper = extractBetween(APP, "function isAutoConnectionPending()", "async function refreshFromAuthModal()");
assert.match(APP, /function isAutoConnectionPending\(\)/);
assert.match(APP, /function renderAutoConnectingScreen\(/);
assert.match(helper, /正在自动连接工作区/);
assert.match(dashboard, /isAutoConnectionPending\(\)/);
assert.match(dashboard, /renderAutoConnectingScreen\("项目总台"/);
assert.match(projects, /isAutoConnectionPending\(\)/);
assert.match(projects, /renderAutoConnectingScreen\("我的项目"/);
});
test("ensureAutoSession re-renders immediately when auto-connect starts", () => {
const source = extractBetween(APP, "async function ensureAutoSession(options = {})", "async function refreshFromAuthModal()");
assert.match(source, /appState\.autoConnectAttempted = true;/);
assert.match(source, /renderAll\(\);/);
});
test("bootstrap does not trust a stored session from a different backend", () => {
const helpers = extractBetween(APP, "function loadStoredSession()", "function compareDateDesc(");
const ensure = extractBetween(APP, "async function ensureAutoSession(options = {})", "async function refreshFromAuthModal()");
const bootstrap = extractBetween(APP, "async function bootstrap()", "async function markTrackingDigestRead()");
assert.match(helpers, /function normalizeBackendUrlValue\(/);
assert.match(helpers, /function hasSessionBackendMismatch\(/);
assert.match(ensure, /const backendMismatch = hasSessionBackendMismatch\(backendUrl\);/);
assert.match(ensure, /persistSession\(null\);/);
assert.match(bootstrap, /const backendMismatch = hasSessionBackendMismatch\(\);/);
assert.match(bootstrap, /await ensureAutoSession\(\{ force: backendMismatch \}\);/);
});
test("oneliner submit failures stay inside the app instead of using a browser alert", () => {
assert.doesNotMatch(APP, /alert\("OneLiner 调度失败:/);
assert.match(APP, /presentActionFailure\(error,\s*"OneLiner 调度失败"\)/);
});
test("agent control surfaces load governance endpoints for user and admin summaries", () => {
const source = extractBetween(APP, "async function loadAgentControlSurfaces(projectId = \"\")", "async function loadOneLinerMessages(sessionId)");
assert.match(source, /\/v2\/oneliner\/governance\/effective/);
assert.match(source, /\/v2\/oneliner\/governance\/user\/global/);
assert.match(source, /\/v2\/oneliner\/governance\/user\/platforms\/\$\{encodeURIComponent\(governancePlatform\)\}/);
assert.match(source, /\/v2\/admin\/oneliner\/governance\/system\/main-agent/);
assert.match(source, /\/v2\/admin\/oneliner\/governance\/system\/platforms\/\$\{encodeURIComponent\(item\.value\)\}/);
});
test("oneliner meta and action handlers expose governance entry points", () => {
const meta = extractBetween(APP, "function renderOneLinerUi()", "function openOneLinerPanel()");
const actions = extractBetween(APP, "document.addEventListener(\"click\", async (event) => {", "document.addEventListener(\"submit\", async (event) => {");
assert.match(meta, /open-user-global-policy/);
assert.match(meta, /policyScopeTagLabel/);
assert.match(actions, /name === "open-user-global-policy"/);
assert.match(actions, /name === "open-system-main-policy"/);
});
test("system governance saves refresh control surfaces after persisting", () => {
const profile = extractBetween(APP, "function openOneLinerProfileAction()", "function parsePolicyJsonField(rawValue, label = \"策略 JSON\")");
const userGlobal = extractBetween(APP, "function openUserGlobalPolicyAction()", "function openUserPlatformPolicyAction(platform)");
const userPlatform = extractBetween(APP, "function openUserPlatformPolicyAction(platform)", "function openSystemMainPolicyAction()");
const main = extractBetween(APP, "function openSystemMainPolicyAction()", "function openSystemPlatformPolicyAction(platform)");
const platform = extractBetween(APP, "function openSystemPlatformPolicyAction(platform)", "function openPlatformAgentProfileAction(platform)");
assert.match(profile, /appState\.onelinerProfile = saved;/);
assert.match(profile, /await loadAgentControlSurfaces\(project\.id\);/);
assert.match(userGlobal, /appState\.userGlobalPolicy = saved;/);
assert.match(userGlobal, /await loadAgentControlSurfaces\(project\.id\);/);
assert.match(userPlatform, /appState\.userCurrentPlatformPolicy = saved;/);
assert.match(userPlatform, /await loadAgentControlSurfaces\(project\.id\);/);
assert.match(main, /const projectId = getOneLinerProjectId\(\);/);
assert.match(main, /appState\.adminSystemMainPolicy = saved;/);
assert.match(main, /await loadAgentControlSurfaces\(projectId\);/);
assert.match(platform, /const projectId = getOneLinerProjectId\(\);/);
assert.match(platform, /appState\.adminSystemPlatformPolicies = safeArray\(appState\.adminSystemPlatformPolicies\)/);
assert.match(platform, /await loadAgentControlSurfaces\(projectId\);/);
});