import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join, resolve } from "node:path";

const __dirname = dirname(fileURLToPath(import.meta.url));
const source = readFileSync(join(__dirname, "SystemExtensionView.vue"), "utf8");
const mainCss = readFileSync(resolve(__dirname, "../styles/main.css"), "utf8");

describe("SystemExtensionView workbench redesign", () => {
  it("removes the old hero explanation card and admin-only copy", () => {
    assert.doesNotMatch(source, /extension-hero/);
    assert.doesNotMatch(source, /extension-status-card/);
    assert.doesNotMatch(source, /广播通知与 AI 助手配置中心/);
    assert.doesNotMatch(source, /ADMIN ONLY/);
  });

  it("renders a compact system extension workbench shell", () => {
    [
      "system-extension-workbench",
      "extension-status-strip",
      "extension-config-shell",
      "extension-nav-rail",
      "extension-module-panel",
      "extension-save-dock"
    ].forEach((className) => {
      assert.match(source, new RegExp(className));
      assert.match(mainCss, new RegExp(`\\.${className}`));
    });
  });

  it("keeps auxiliary explanations out of the permanent workspace", () => {
    assert.doesNotMatch(source, /<small>{{ section\.desc }}<\/small>/);
    assert.doesNotMatch(source, /activeSectionMeta\.eyebrow/);
    assert.doesNotMatch(source, /extension-impact-list/);
    assert.doesNotMatch(source, /extension-detail-note/);
    assert.match(source, /extension-preview-disclosure/);
    assert.match(source, /extension-field-grid/);
  });

  it("opens broadcast editing from a dialog instead of a permanent form block", () => {
    assert.match(source, /showBroadcastDialog/);
    assert.match(source, /openNewBroadcastDialog/);
    assert.match(source, /extension-modal-backdrop/);
    assert.match(source, /extension-broadcast-ledger/);
    assert.match(source, /data-semantic-label="新增广播"/);
    assert.match(source, /新增广播/);
    assert.doesNotMatch(source, /<section v-if="activeSection === 'broadcast'" class="extension-broadcast-grid">/);
  });

  it("exposes the four configuration modules as a navigation directory", () => {
    ["广播通知", "库存批次规则", "生产对接模式", "AI 助手配置"].forEach((label) => {
      assert.match(source, new RegExp(label));
    });

    assert.match(source, /activeSection/);
    assert.match(source, /extensionSections/);
  });

  it("uses a fixed save dock with explicit save and reset actions", () => {
    assert.match(source, /放弃修改/);
    assert.match(source, /保存配置/);
    assert.match(source, /saveActiveSection/);
    assert.match(source, /resetActiveSection/);
    assert.match(source, /activeSectionDirty/);
  });

  it("keeps existing system extension API endpoints unchanged", () => {
    [
      "/system-extension/broadcasts",
      "/system-extension/assistant-config",
      "/system-extension/raw-material-lot-prefix",
      "/system-extension/smart-operation-report"
    ].forEach((endpoint) => {
      assert.ok(source.includes(endpoint), `${endpoint} endpoint should remain in source`);
    });
  });
});
