Files
streamline/control-web/test/server_scope_gate.test.ts
T
2026-11-16 17:26:44 -08:00

81 lines
4.0 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import { readFileSync } from "node:fs";
import path from "node:path";
import { serverScopeNeedsDecision } from "@bbbug/shared/server_prompt";
/**
* 面板的挂载条件 —— 钉住两条互相拉扯的纪律。
*
* 一、**纯客户端需求看不到任何服务端东西。**线上绝大多数是纯客户端的。
* 二、**入口交给人。**端上 endpoint → PSM 的反查实测唯一命中率只有 4%
* (120 条真实 endpoint 随机抽样),所以「评估器查到了 PSM」是少数情况。
* 如果只在 psms 非空时给入口,96% 的情况下人在闸上根本没有地方补这个值。
*
* 两条的交点是**方案里那一行的形态**:写「无」= 纯客户端,什么都不显示;
* 打 NEEDS_INPUT 哨兵 = 评估器明确说定不了,那正是该交给人的时候。
* 所以这里断言的重点从「psms 非空」挪到了 `serverScopeNeedsDecision` 的行为上 ——
* 那才是真正把两条纪律分开的判据。
*/
const REPO = path.resolve(import.meta.dir, "../..");
const drawer = readFileSync(path.join(REPO, "control-web/src/components/TaskDetailDrawer.tsx"), "utf8");
describe("服务端面板的挂载条件", () => {
test("挂载条件仍然以确认闸为前提", () => {
expect(drawer).toContain("isPendingConfirm &&");
expect(drawer).toContain("<ServerScopePanel");
});
test("两档触发:psms 非空,或方案里那一节还没定", () => {
const cond = drawer.slice(drawer.indexOf("{isPendingConfirm &&"), drawer.indexOf("<ServerScopePanel"));
expect(cond).toContain("item.psms?.length");
expect(cond).toContain("serverScopeNeedsDecision");
});
test("没有任何一处无条件渲染它", () => {
const uses = [...drawer.matchAll(/<ServerScopePanel/g)];
expect(uses.length).toBe(1);
});
});
describe("判据本身:什么时候该把入口交给人", () => {
const sec = (body: string) => `## 目标\n做点什么\n\n## 服务端范围\n${body}\n\n## 验证环境\n- 账号: 任意\n`;
test("写「无」→ 不给入口。这是纯客户端需求的出口,必须一个像素都不出现", () => {
const plan = sec('- 涉及 PSM: 无\n- 服务端改动: 无\n- 服务端验证方式: 无');
expect(serverScopeNeedsDecision(plan, "feature")).toBe(false);
expect(serverScopeNeedsDecision(plan, "bug")).toBe(false);
});
test("打哨兵 → 给入口。4% 的命中率下这是常态,不是例外", () => {
const plan = sec('- 涉及 PSM: <<<<<<< NEEDS_INPUT: 需要补充承载该接口的 PSM 名称 >>>>>>>\n- 服务端改动: 待定');
expect(serverScopeNeedsDecision(plan, "feature")).toBe(true);
});
test("整节缺失 → 不给入口。缺失不等于未定,不要替旧方案臆造一个待决状态", () => {
expect(serverScopeNeedsDecision("## 目标\n做点什么\n", "feature")).toBe(false);
expect(serverScopeNeedsDecision("", "feature")).toBe(false);
});
test("哨兵在别的小节里不算 —— 判据只看「服务端范围」这一节", () => {
// 方案里到处都可能有哨兵;把任何一个哨兵都当成「服务端待定」会让面板对着
// 一堆纯客户端需求弹出来,那就等于破了第一条纪律。
const plan = '## 目标\n<<<<<<< NEEDS_INPUT: 缺产品目标 >>>>>>>\n\n## 服务端范围\n- 涉及 PSM: 无\n\n## 验证环境\n- 账号: 任意\n';
expect(serverScopeNeedsDecision(plan, "feature")).toBe(false);
});
});
describe("凭据不回显", () => {
const panel = readFileSync(path.join(REPO, "control-web/src/components/ServerScopePanel.tsx"), "utf8");
test("输入框是 password 且不自动填充", () => {
expect(panel).toContain('type="password"');
expect(panel).toContain('autoComplete="off"');
});
test("只消费 has(有没有),不消费任何密文字段", () => {
// 服务端从不回密文;面板若试图读它,说明契约被破坏了。
expect(panel).not.toMatch(/\.values\b/);
expect(panel).toContain("meServerlineCreds");
});
});