/**
 * 发布问题场景注册表 — 问题六十二: 设计产物包场景 (11-16)
 */
import fs from "node:fs";
import path from "node:path";
import { createCompleteDesignFixture } from "./fixtures.js";
export const DESIGN_PACK_SCENARIOS = [
    // 场景 11: 新项目缺设计文档 → sf_classify→sf_expand→sf_verify→DESIGN_ARTIFACT_MISSING
    {
        scenario_id: "release-scenario-design-pack-new-project",
        test_files: ["tests/engine/design_artifact_pack.test.ts"],
        production_entrypoint: "sf_classify → sf_expand → sf_verify → design_artifact_pack",
        expected_outcome: "新项目缺设计文档 → sf_verify 返回 SF-DESIGN-PACK-VERIFY-FAILED 且 findings 包含 DESIGN_ARTIFACT_MISSING",
        runner: async () => {
            const { createToolHarness } = await import("../release_tool_harness.js");
            const harness = await createToolHarness();
            try {
                fs.mkdirSync(path.join(harness.tmpDir, "docs", "architecture"), { recursive: true });
                const clsRaw = await harness.callTool("sf_classify", { intent: "修复登录按钮 CSS，绑定 PHASE-001 SLICE-001 DD-T001 DD-A001 AC-001" });
                const cls = harness.parseResult(clsRaw);
                const taskId = cls.task_id;
                await harness.callTool("sf_expand", { task_id: taskId });
                await harness.setupPlanGate(taskId);
                const verRaw = await harness.callTool("sf_verify", {
                    task_id: taskId,
                    changed_files: ["docs/architecture/architecture.md"],
                });
                const ver = harness.parseResult(verRaw);
                if (ver.diagnostic_code !== "SF-DESIGN-PACK-VERIFY-FAILED") {
                    return { scenario_id: "release-scenario-design-pack-new-project", status: "fail", error: `expected SF-DESIGN-PACK-VERIFY-FAILED, got code=${ver.diagnostic_code} status=${ver.status}` };
                }
                const missingCodes = (ver.design_artifact_findings ?? []).filter((f) => f.code === "DESIGN_ARTIFACT_MISSING");
                if (missingCodes.length === 0) {
                    return { scenario_id: "release-scenario-design-pack-new-project", status: "fail", error: "expected DESIGN_ARTIFACT_MISSING in findings" };
                }
                const trace = await harness.collectTrace();
                return {
                    scenario_id: "release-scenario-design-pack-new-project",
                    status: "pass",
                    evidence: `sf_classify→sf_expand→sf_verify SF-DESIGN-PACK-VERIFY-FAILED findings=${missingCodes.length}`,
                    production_trace: trace,
                };
            }
            finally {
                harness.cleanup();
            }
        },
    },
    // 场景 12: API 字段表缺失 → sf_classify→sf_expand→sf_verify→API_FIELD_TABLE_INCOMPLETE
    {
        scenario_id: "release-scenario-design-pack-api-field-incomplete",
        test_files: ["tests/engine/release_issue_scenario_matrix.test.ts"],
        production_entrypoint: "sf_classify → sf_expand → sf_verify → design_artifact_pack",
        expected_outcome: "API 文档含 endpoint 但缺字段表 → sf_verify findings 包含 API_FIELD_TABLE_INCOMPLETE",
        runner: async () => {
            const { createToolHarness } = await import("../release_tool_harness.js");
            const harness = await createToolHarness();
            try {
                createCompleteDesignFixture(harness.tmpDir);
                // 替换 API 文档为含 endpoint 但缺字段表的版本
                fs.writeFileSync(path.join(harness.tmpDir, "docs", "architecture", "03-API接口规格文档.md"), "# 接口概览\n\n追踪: DD-A001 | 上游: ARCH-001\n\n用户管理 API。\n\n## GET /api/users\n\n获取用户列表。\n\n## POST /api/users\n\n创建用户。\n", "utf-8");
                const clsRaw = await harness.callTool("sf_classify", { intent: "修复登录按钮 CSS，绑定 PHASE-001 SLICE-001 DD-T001 DD-A001 AC-001" });
                const cls = harness.parseResult(clsRaw);
                const taskId = cls.task_id;
                await harness.callTool("sf_expand", { task_id: taskId });
                await harness.setupPlanGate(taskId);
                const verRaw = await harness.callTool("sf_verify", {
                    task_id: taskId,
                    changed_files: ["docs/architecture/03-API接口规格文档.md"],
                });
                const ver = harness.parseResult(verRaw);
                const allFindings = (ver.design_artifact_findings ?? []);
                const apiFinding = allFindings.find((f) => f.code === "API_FIELD_TABLE_INCOMPLETE");
                if (!apiFinding) {
                    return { scenario_id: "release-scenario-design-pack-api-field-incomplete", status: "fail", error: `expected API_FIELD_TABLE_INCOMPLETE, got codes: ${allFindings.map((f) => f.code).join(",")}` };
                }
                const trace = await harness.collectTrace();
                return {
                    scenario_id: "release-scenario-design-pack-api-field-incomplete",
                    status: "pass",
                    evidence: `sf_classify→sf_expand→sf_verify API_FIELD_TABLE_INCOMPLETE found`,
                    production_trace: trace,
                };
            }
            finally {
                harness.cleanup();
            }
        },
    },
    // 场景 13: SQL 权威缺失 → sf_classify→sf_expand→sf_verify→SQL_AUTHORITY_MISSING
    {
        scenario_id: "release-scenario-design-pack-sql-authority-missing",
        test_files: ["tests/engine/design_artifact_pack.test.ts"],
        production_entrypoint: "sf_classify → sf_expand → sf_verify → design_artifact_pack",
        expected_outcome: "完整 fixture 但无 SQL 文件 → sf_verify findings 包含 SQL_AUTHORITY_MISSING",
        runner: async () => {
            const { createToolHarness } = await import("../release_tool_harness.js");
            const harness = await createToolHarness();
            try {
                createCompleteDesignFixture(harness.tmpDir);
                // 删除 SQL 文件触发 SQL_AUTHORITY_MISSING
                fs.rmSync(path.join(harness.tmpDir, "db", "migrations", "001-init.sql"));
                const clsRaw = await harness.callTool("sf_classify", { intent: "修复登录按钮 CSS，绑定 PHASE-001 SLICE-001 DD-T001 DD-A001 AC-001" });
                const cls = harness.parseResult(clsRaw);
                const taskId = cls.task_id;
                await harness.callTool("sf_expand", { task_id: taskId });
                await harness.setupPlanGate(taskId);
                const verRaw = await harness.callTool("sf_verify", {
                    task_id: taskId,
                    changed_files: ["docs/architecture/02-数据库设计文档.md"],
                });
                const ver = harness.parseResult(verRaw);
                const allFindings = (ver.design_artifact_findings ?? []);
                const sqlFinding = allFindings.find((f) => f.code === "SQL_AUTHORITY_MISSING");
                if (!sqlFinding) {
                    return { scenario_id: "release-scenario-design-pack-sql-authority-missing", status: "fail", error: `expected SQL_AUTHORITY_MISSING, got codes: ${allFindings.map((f) => f.code).join(",")}` };
                }
                const trace = await harness.collectTrace();
                return {
                    scenario_id: "release-scenario-design-pack-sql-authority-missing",
                    status: "pass",
                    evidence: `sf_classify→sf_expand→sf_verify SQL_AUTHORITY_MISSING found`,
                    production_trace: trace,
                };
            }
            finally {
                harness.cleanup();
            }
        },
    },
    // 场景 14: PostgreSQL 部分唯一约束非法 → sf_classify→sf_expand→sf_verify→POSTGRES_PARTIAL_UNIQUE_INVALID
    {
        scenario_id: "release-scenario-design-pack-postgres-partial-unique",
        test_files: ["tests/engine/design_artifact_pack.test.ts"],
        production_entrypoint: "sf_classify → sf_expand → sf_verify → design_artifact_pack",
        expected_outcome: "SQL 含 CONSTRAINT ... UNIQUE (...) WHERE → sf_verify findings 包含 POSTGRES_PARTIAL_UNIQUE_INVALID",
        runner: async () => {
            const { createToolHarness } = await import("../release_tool_harness.js");
            const harness = await createToolHarness();
            try {
                createCompleteDesignFixture(harness.tmpDir);
                // 替换 SQL 为含非法约束的版本
                fs.writeFileSync(path.join(harness.tmpDir, "db", "migrations", "001-init.sql"), "CREATE TABLE users (id UUID PRIMARY KEY, email TEXT NOT NULL, CONSTRAINT uq_users_email UNIQUE (email) WHERE active = true);\n", "utf-8");
                const clsRaw = await harness.callTool("sf_classify", { intent: "修复登录按钮 CSS，绑定 PHASE-001 SLICE-001 DD-T001 DD-A001 AC-001" });
                const cls = harness.parseResult(clsRaw);
                const taskId = cls.task_id;
                await harness.callTool("sf_expand", { task_id: taskId });
                await harness.setupPlanGate(taskId);
                const verRaw = await harness.callTool("sf_verify", {
                    task_id: taskId,
                    changed_files: ["db/migrations/001-init.sql"],
                });
                const ver = harness.parseResult(verRaw);
                const allFindings = (ver.design_artifact_findings ?? []);
                const pgFinding = allFindings.find((f) => f.code === "POSTGRES_PARTIAL_UNIQUE_INVALID");
                if (!pgFinding) {
                    return { scenario_id: "release-scenario-design-pack-postgres-partial-unique", status: "fail", error: `expected POSTGRES_PARTIAL_UNIQUE_INVALID, got codes: ${allFindings.map((f) => f.code).join(",")}` };
                }
                const trace = await harness.collectTrace();
                return {
                    scenario_id: "release-scenario-design-pack-postgres-partial-unique",
                    status: "pass",
                    evidence: `sf_classify→sf_expand→sf_verify POSTGRES_PARTIAL_UNIQUE_INVALID found`,
                    production_trace: trace,
                };
            }
            finally {
                harness.cleanup();
            }
        },
    },
    // 场景 15: 跨文档响应码冲突 → sf_classify→sf_expand→sf_verify→CROSS_DOC_RESPONSE_CODE_CONFLICT
    {
        scenario_id: "release-scenario-design-pack-cross-doc-conflict",
        test_files: ["tests/engine/design_artifact_pack.test.ts"],
        production_entrypoint: "sf_classify → sf_expand → sf_verify → design_artifact_pack",
        expected_outcome: "架构文档含 code:200 但 API 文档含 code:0 → sf_verify findings 包含 CROSS_DOC_RESPONSE_CODE_CONFLICT",
        runner: async () => {
            const { createToolHarness } = await import("../release_tool_harness.js");
            const harness = await createToolHarness();
            try {
                createCompleteDesignFixture(harness.tmpDir);
                // 替换架构文档: 含 "code": 200
                fs.writeFileSync(path.join(harness.tmpDir, "docs", "architecture", "01-架构设计文档.md"), "# 架构设计\n\n## 来源追踪\n\nREQ-001、PROTO-001。\n\n## 系统概述\n\n微服务。统一响应格式: {\"code\": 200, \"data\": {}}。\n\n## 架构决策\n\nARCH-001 微服务\n\n## 技术选型\n\nTS\n\n## 模块设计\n\n模块\n", "utf-8");
                // 替换 API 文档: 含 "code": 0
                fs.writeFileSync(path.join(harness.tmpDir, "docs", "architecture", "03-API接口规格文档.md"), "# 接口概览\n\n追踪: DD-A001 | 上游: ARCH-001\n\n响应格式: {\"code\": 0, \"msg\": \"\", \"data\": {}}。\n\n## GET /api/users\n\n### 请求字段表\n\n| 请求字段名 | 类型 | 必填 | 来源 | 说明 | 示例 | 校验规则 | 错误语义 |\n|--------|------|------|------|------|------|----------|----------|\n| page | int | 否 | query | 页码 | 1 | >=1 | 页码无效 |\n\n### 响应字段表\n\n| 响应字段名 | 类型 | 必填 | 来源 | 说明 | 示例 | 校验规则 | 错误语义 |\n|--------|------|------|------|------|------|----------|----------|\n| id | string | 是 | db | 用户ID | usr-1 | uuid | 用户不存在 |\n", "utf-8");
                const clsRaw = await harness.callTool("sf_classify", { intent: "修复登录按钮 CSS，绑定 PHASE-001 SLICE-001 DD-T001 DD-A001 AC-001" });
                const cls = harness.parseResult(clsRaw);
                const taskId = cls.task_id;
                await harness.callTool("sf_expand", { task_id: taskId });
                await harness.setupPlanGate(taskId);
                const verRaw = await harness.callTool("sf_verify", {
                    task_id: taskId,
                    changed_files: ["docs/architecture/01-架构设计文档.md", "docs/architecture/03-API接口规格文档.md"],
                });
                const ver = harness.parseResult(verRaw);
                const allFindings = (ver.design_artifact_findings ?? []);
                const crossDocFinding = allFindings.find((f) => f.code === "CROSS_DOC_RESPONSE_CODE_CONFLICT");
                if (!crossDocFinding) {
                    return { scenario_id: "release-scenario-design-pack-cross-doc-conflict", status: "fail", error: `expected CROSS_DOC_RESPONSE_CODE_CONFLICT, got codes: ${allFindings.map((f) => f.code).join(",")}` };
                }
                const trace = await harness.collectTrace();
                return {
                    scenario_id: "release-scenario-design-pack-cross-doc-conflict",
                    status: "pass",
                    evidence: `sf_classify→sf_expand→sf_verify CROSS_DOC_RESPONSE_CODE_CONFLICT found`,
                    production_trace: trace,
                };
            }
            finally {
                harness.cleanup();
            }
        },
    },
    // 场景 16: 低风险不触发设计产物包
    {
        scenario_id: "release-scenario-design-pack-low-risk-skip",
        test_files: ["tests/adapters/architecture_design_workshop_mainpath.test.ts"],
        production_entrypoint: "sf_classify",
        expected_outcome: "低风险 CSS 修复 route 非 artifact_generation，不触发设计产物包",
        runner: async () => {
            const { classify } = await import("../../pipeline/classifier.js");
            const result = classify({ intent: "修复 CSS 样式问题" });
            const route = result.route_decision?.route ?? "";
            if (route === "artifact_generation")
                return { scenario_id: "release-scenario-design-pack-low-risk-skip", status: "fail", error: `CSS fix should not be artifact_generation, got ${route}` };
            return {
                scenario_id: "release-scenario-design-pack-low-risk-skip",
                status: "pass",
                evidence: `route=${route} risk=${result.risk}`,
                production_trace: { tool_entrypoint: "sf_classify", diagnostic_codes: [`route:${route}`, `risk:${result.risk}`], gates_consumed: ["router"] },
            };
        },
    },
];
//# sourceMappingURL=scenarios_design_pack.js.map