/**
 * 发布问题场景注册表 — 问题六十八: 代码可维护性与可观测性契约场景
 */
import { errorMessage } from "../../core/helpers.js";
export const CODE_OBSERVABILITY_SCENARIOS = [
    {
        scenario_id: "release-scenario-code-observability-payment-status-no-log",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → sf_review → reviewMissingLogs → sf_deliver → evaluateDeliveryBlock",
        expected_outcome: "支付/账单状态更新缺少业务日志 → hard_fail 阻断交付",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const findings = mod.reviewMissingLogs({
                "PaymentService.java": `public void refund(String orderId, BigDecimal amount) {
  orderRepository.save(order);
  paymentGateway.refund(orderId, amount);
}`,
            });
            const blocked = mod.hasBlockingObservabilityFindings(findings);
            return {
                scenario_id: "release-scenario-code-observability-payment-status-no-log",
                status: blocked ? "pass" : "fail",
                evidence: blocked ? "hard_fail: 支付写操作无日志被检测到" : "未检测到支付写操作无日志",
                production_trace: { tool_entrypoint: "reviewMissingLogs", diagnostic_codes: [blocked ? "hard_fail" : "missed"], gates_consumed: ["sf_review", "engine_contract"] },
            };
        },
    },
    {
        scenario_id: "release-scenario-code-observability-permission-denied-no-log",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → sf_review → reviewMissingLogs → sf_deliver → evaluateDeliveryBlock",
        expected_outcome: "权限拒绝无安全日志 → hard_fail 阻断",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const findings = mod.reviewMissingLogs({
                "AuthController.java": `public void checkPermission(String userId, String resource) {
  if (!roleService.hasAccess(userId, resource)) {
    throw new AccessDeniedException("forbidden");
  }
}`,
            });
            const blocked = mod.hasBlockingObservabilityFindings(findings);
            return {
                scenario_id: "release-scenario-code-observability-permission-denied-no-log",
                status: blocked ? "pass" : "fail",
                evidence: blocked ? "hard_fail: 权限拒绝无日志被检测到" : "未检测到权限拒绝无日志",
                production_trace: { tool_entrypoint: "reviewMissingLogs", diagnostic_codes: [blocked ? "hard_fail" : "missed"], gates_consumed: ["sf_review", "engine_contract"] },
            };
        },
    },
    {
        scenario_id: "release-scenario-code-observability-catch-swallow",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → sf_review → reviewMissingLogs → sf_deliver → evaluateDeliveryBlock",
        expected_outcome: "catch 后吞异常无日志 → hard_fail 阻断",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const findings = mod.reviewMissingLogs({
                "OrderService.ts": `async processOrder(orderId: string) {
  try {
    await this.client.submit(orderId);
  } catch (e: unknown) {
    return false;
  }
}`,
            });
            const blocked = mod.hasBlockingObservabilityFindings(findings);
            return {
                scenario_id: "release-scenario-code-observability-catch-swallow",
                status: blocked ? "pass" : "fail",
                evidence: blocked ? "hard_fail: catch 吞异常被检测到" : "未检测到 catch 吞异常",
                production_trace: { tool_entrypoint: "reviewMissingLogs", diagnostic_codes: [blocked ? "hard_fail" : "missed"], gates_consumed: ["sf_review", "engine_contract"] },
            };
        },
    },
    {
        scenario_id: "release-scenario-code-observability-sensitive-log",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → sf_review → detectSensitiveLogs → sf_deliver → evaluateDeliveryBlock",
        expected_outcome: "日志输出 token/password/手机号全量 → hard_fail 阻断",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const findings = mod.detectSensitiveLogs({
                "UserService.ts": `function login(user) {
  logger.info("user login", { token: "eyJhbGciOiJIUzI1NiJ9.xxxxx", password: user.password, phone: "13812345678" });
}`,
            });
            const blocked = mod.hasBlockingObservabilityFindings(findings);
            return {
                scenario_id: "release-scenario-code-observability-sensitive-log",
                status: blocked ? "pass" : "fail",
                evidence: blocked ? `hard_fail: 敏感信息泄漏被检测到 (${findings.length} findings)` : "未检测到敏感信息泄漏",
                production_trace: { tool_entrypoint: "detectSensitiveLogs", diagnostic_codes: [blocked ? "hard_fail" : "missed"], gates_consumed: ["sf_review", "engine_contract"] },
            };
        },
    },
    {
        scenario_id: "release-scenario-code-observability-complex-rule-no-comment",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → sf_review → reviewCommentQuality → sf_deliver",
        expected_outcome: "复杂金额计算无注释 → warning",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const findings = mod.reviewCommentQuality({
                "BillingService.java": `// 金额计算逻辑
public BigDecimal calculateSubsidy(Order order) {
  BigDecimal base = order.getAmount().multiply(new BigDecimal("0.8"));
  BigDecimal discount = base.subtract(order.getDeducted());
  return discount.max(BigDecimal.ZERO);
}`,
            });
            findings.some(f => f.category === "missing_comment_complex");
            // 上面有注释不会触发缺失检测；下面测试无注释的负例：
            const findingsNoComment = mod.reviewCommentQuality({
                "金额计算/BillingService.java": `public BigDecimal calculateSubsidy(Order order) {
  BigDecimal base = order.getAmount().multiply(new BigDecimal("0.8"));
  BigDecimal discount = base.subtract(order.getDeducted());
  return discount.max(BigDecimal.ZERO);
}`,
            });
            const detected = findingsNoComment.some(f => f.category === "missing_comment_complex");
            return {
                scenario_id: "release-scenario-code-observability-complex-rule-no-comment",
                status: detected ? "pass" : "fail",
                evidence: detected ? "warning: 无注释复杂逻辑被检测到" : "未检测到无注释复杂逻辑",
                production_trace: { tool_entrypoint: "reviewCommentQuality", diagnostic_codes: [detected ? "warning" : "missed"], gates_consumed: ["sf_review", "engine_contract"] },
            };
        },
    },
    {
        scenario_id: "release-scenario-code-observability-low-risk-skip",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_classify → sf_expand (低风险) → 不触发可观测性门禁",
        expected_outcome: "简单文案修改不触发代码可维护性/可观测性契约",
        runner: async () => {
            const { createToolHarness } = await import("../release_tool_harness.js");
            const harness = await createToolHarness();
            try {
                const intent = "修复 README 文案错别字";
                const clsRaw = await harness.callTool("sf_classify", { intent });
                const cls = harness.parseResult(clsRaw);
                const taskId = cls.task_id;
                if (!taskId)
                    return { scenario_id: "release-scenario-code-observability-low-risk-skip", status: "fail", error: "sf_classify 未返回 task_id" };
                const expRaw = await harness.callTool("sf_expand", { task_id: taskId });
                const exp = harness.parseResult(expRaw);
                // 低风险任务不应触发 SF-OBS-6801 诊断码
                const notBlocked = exp.diagnostic_code !== "SF-OBS-6801";
                return {
                    scenario_id: "release-scenario-code-observability-low-risk-skip",
                    status: notBlocked ? "pass" : "fail",
                    evidence: notBlocked ? "低风险任务正确跳过可观测性门禁" : "低风险任务被错误触发可观测性门禁",
                    production_trace: { tool_entrypoint: "sf_classify+sf_expand", diagnostic_codes: [exp.status ?? "ok", exp.diagnostic_code ?? "none"], gates_consumed: ["sf_classify", "sf_expand"] },
                };
            }
            catch (e) {
                return { scenario_id: "release-scenario-code-observability-low-risk-skip", status: "fail", error: errorMessage(e) };
            }
            finally {
                await harness.cleanup();
            }
        },
    },
    {
        scenario_id: "release-scenario-code-observability-project-logger-detected",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → detectProjectLogger",
        expected_outcome: "Java/Spring/Node/NestJS 项目能正确检测 logger 类型",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const javaLogger = mod.detectProjectLogger({
                "Application.java": "import org.slf4j.LoggerFactory; private static final Logger log = LoggerFactory.getLogger(App.class);",
            });
            const pinoLogger = mod.detectProjectLogger({
                "index.ts": "import pino from 'pino'; const logger = pino();",
            });
            const nestjsLogger = mod.detectProjectLogger({
                "app.service.ts": "import { Logger } from '@nestjs/common';",
            });
            const ok = javaLogger.type === "slf4j" && pinoLogger.type === "pino" && nestjsLogger.type === "nestjs_logger";
            return {
                scenario_id: "release-scenario-code-observability-project-logger-detected",
                status: ok ? "pass" : "fail",
                evidence: ok ? `检测: ${javaLogger.type}, ${pinoLogger.type}, ${nestjsLogger.type}` : `检测失败: ${javaLogger.type}, ${pinoLogger.type}, ${nestjsLogger.type}`,
                production_trace: { tool_entrypoint: "detectProjectLogger", diagnostic_codes: [javaLogger.type, pinoLogger.type, nestjsLogger.type], gates_consumed: ["sf_expand", "engine_contract"] },
            };
        },
    },
    {
        scenario_id: "release-scenario-code-observability-migration-audit-log",
        test_files: ["tests/engine/code_maintainability_observability_contract.test.ts"],
        production_entrypoint: "sf_expand → CodeObservabilityGate → sf_review → reviewMissingLogs → sf_deliver",
        expected_outcome: "迁移脚本/数据修复无审计日志 → hard_fail",
        runner: async () => {
            const mod = await import("../../contracts/code_maintainability_observability_contract.js");
            const findings = mod.reviewMissingLogs({
                "scripts/data_fix_order_status.ts": `async function migrateOrderStatus() {
  const orders = await db.query("SELECT * FROM orders WHERE status IS NULL");
  for (const order of orders) {
    await db.update("UPDATE orders SET status = 'pending' WHERE id = ?", [order.id]);
  }
}`,
            });
            const blocked = mod.hasBlockingObservabilityFindings(findings);
            return {
                scenario_id: "release-scenario-code-observability-migration-audit-log",
                status: blocked ? "pass" : "fail",
                evidence: blocked ? "hard_fail: 迁移脚本无审计日志被检测到" : "未检测到迁移脚本无审计日志",
                production_trace: { tool_entrypoint: "reviewMissingLogs", diagnostic_codes: [blocked ? "hard_fail" : "missed"], gates_consumed: ["sf_review", "engine_contract"] },
            };
        },
    },
];
//# sourceMappingURL=scenarios_code_observability.js.map