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

181 lines
8.1 KiB
TypeScript

// MUST be first: sets DATABASE_URL before db_client is evaluated.
// feature_execution.ts imports ./db transitively, and db_client throws at import
// time on a missing DATABASE_URL — an assignment at the top of THIS file would
// run too late, because ES imports hoist.
import "./setup_env";
import { describe, expect, it } from "bun:test";
import { featureExecuteGate, featureExecuteReasonMessage } from "../src/feature_execution";
import { openApiFeatureView } from "../src/openapi_feature_view";
import type { Feature, FeatureStatus } from "../src/types";
type GateInput = Pick<Feature, "status" | "executionPlan" | "createdViaKeyId">;
const PLAN = "## 实现方案\n1. 在设置页加一个开关";
function feature(over: Partial<GateInput> = {}): GateInput {
return { status: "pending_confirmation", executionPlan: PLAN, createdViaKeyId: null, ...over };
}
/** Everything except the one status that may be promoted (需求的状态机,不是 bug 的). */
const NON_PROMOTABLE: FeatureStatus[] = [
"initializing", "info_required", "awaiting_implementation", "implementing",
"manual_review", "completed", "implementation_failed", "archived", "cancelled",
];
describe("featureExecuteGate", () => {
it("rejects a missing feature", () => {
expect(featureExecuteGate(null, {})).toEqual({ ok: false, reason: "not_found" });
});
it("promotes a pending_confirmation feature using the stored plan", () => {
expect(featureExecuteGate(feature(), {})).toEqual({ ok: true, finalPlan: PLAN });
});
it("trims the stored plan", () => {
expect(featureExecuteGate(feature({ executionPlan: ` ${PLAN} ` }), {})).toEqual({ ok: true, finalPlan: PLAN });
});
describe("status gate", () => {
for (const status of NON_PROMOTABLE) {
it(`rejects ${status} and reports it back`, () => {
expect(featureExecuteGate(feature({ status }), {})).toEqual({ ok: false, reason: "wrong_status", status });
});
}
});
describe("ownership gate", () => {
it("passes when the key created the feature", () => {
const f = feature({ createdViaKeyId: "opk_aaa" });
expect(featureExecuteGate(f, { requireCreatedViaKeyId: "opk_aaa" })).toEqual({ ok: true, finalPlan: PLAN });
});
it("rejects a feature created by a different key", () => {
const f = feature({ createdViaKeyId: "opk_aaa" });
expect(featureExecuteGate(f, { requireCreatedViaKeyId: "opk_bbb" })).toEqual({ ok: false, reason: "not_owner" });
});
it("rejects a panel-created feature (no open-platform provenance)", () => {
expect(featureExecuteGate(feature(), { requireCreatedViaKeyId: "opk_aaa" })).toEqual({ ok: false, reason: "not_owner" });
});
// The leak guard, same as the bug path: ownership is checked BEFORE status, so a
// key that does not own the feature cannot read its lifecycle from the 409 text.
it("reports not_owner — NOT wrong_status — for a foreign feature in a bad state", () => {
const f = feature({ status: "implementing", createdViaKeyId: "opk_aaa" });
expect(featureExecuteGate(f, { requireCreatedViaKeyId: "opk_bbb" })).toEqual({ ok: false, reason: "not_owner" });
});
it("skips the ownership check entirely for the console path", () => {
const f = feature({ createdViaKeyId: "opk_aaa" });
expect(featureExecuteGate(f, {})).toEqual({ ok: true, finalPlan: PLAN });
});
});
describe("plan gate", () => {
for (const [name, executionPlan] of [["null", null], ["empty", ""], ["blank", " "]] as const) {
it(`rejects a ${name} stored plan`, () => {
expect(featureExecuteGate(feature({ executionPlan }), {})).toEqual({ ok: false, reason: "empty_execution_plan" });
});
}
it("prefers an explicitly passed plan over the stored one", () => {
expect(featureExecuteGate(feature(), { plan: "edited" })).toEqual({ ok: true, finalPlan: "edited" });
});
// `??` not `||`: an explicit blank is a rejection, not "fall back to stored".
it("rejects an explicitly blank plan instead of falling back", () => {
expect(featureExecuteGate(feature(), { plan: " " })).toEqual({ ok: false, reason: "empty_execution_plan" });
});
});
});
describe("featureExecuteReasonMessage", () => {
it("tells the caller to wait while the evaluator is still drafting", () => {
expect(featureExecuteReasonMessage("wrong_status", "initializing")).toContain("稍后重试");
});
it("does not offer a retry when the feature needs human input", () => {
expect(featureExecuteReasonMessage("wrong_status", "info_required")).toContain("重试不会好转");
// 和 bug 同一个分法:平台出错时不让人去补信息。
const infra = featureExecuteReasonMessage("wrong_status", "info_required", "评估超时,已自动终止");
expect(infra).toContain("平台侧出错");
expect(infra).not.toContain("重试不会好转");
});
it("says no retry is needed once the feature is already queued or running", () => {
for (const status of ["awaiting_implementation", "implementing"] as const) {
expect(featureExecuteReasonMessage("wrong_status", status)).toContain("无需重复调用");
}
});
it("treats terminal states as non-promotable", () => {
for (const status of ["completed", "implementation_failed", "archived", "cancelled"] as const) {
expect(featureExecuteReasonMessage("wrong_status", status)).toContain("只有待确认状态可以执行");
}
});
});
describe("openApiFeatureView", () => {
const base = {
id: "feat-1",
title: "一句话需求",
platform: "ios",
status: "pending_confirmation",
createdAt: 1_700_000_000_000,
statusChangedAt: 1_700_000_100_000,
finishedAt: null,
mrUrl: null,
externalId: "PRD-42",
productProfile: { profileId: "tiktok", profileName: "TikTok" },
} as unknown as Feature;
it("exposes the pulled fields under featureId, mirroring the task view naming", () => {
const view = openApiFeatureView(base);
expect(view).toMatchObject({
featureId: "feat-1",
externalId: "PRD-42",
title: "一句话需求",
platform: "ios",
status: "pending_confirmation",
statusLabel: "待确认",
productProfileId: "tiktok",
productProfileName: "TikTok",
baseRef: null,
createdAt: 1_700_000_000_000,
statusChangedAt: 1_700_000_100_000,
finishedAt: null,
mrUrl: null,
});
});
it("echoes the custom branch the caller created it on (same field name as the task view)", () => {
const view = openApiFeatureView({ ...base, baseRef: "notes/feature/x" } as unknown as Feature);
expect(view.baseRef).toBe("notes/feature/x");
});
it("links to the canonical feature deep link (?feature_id is only the legacy form)", () => {
expect(openApiFeatureView(base).panelUrl).toContain("/feature?id=feat-1");
});
it("carries infoRequired / needsInput with the same semantics as the task view", () => {
const plan = "1. 改排序\n<<<<<<< NEEDS_INPUT: 空态文案用哪句? @OPTION 默认 | 工单 | 暂无收藏 >>>>>>>";
expect(openApiFeatureView({ ...base, executionPlan: plan } as unknown as Feature)).toMatchObject({
infoRequired: null, needsInput: ["空态文案用哪句?"],
});
const needsInfo = openApiFeatureView({ ...base, status: "info_required", failureReason: "没说是哪个入口", executionPlan: plan } as unknown as Feature);
expect(needsInfo.infoRequired).toEqual({ kind: "needs_info", reason: "没说是哪个入口" });
expect(needsInfo.needsInput).toEqual(["空态文案用哪句?"]);
const infra = openApiFeatureView({ ...base, status: "info_required", failureReason: "评估超时,已自动终止", executionPlan: plan } as unknown as Feature);
expect(infra.infoRequired?.kind).toBe("platform_error");
expect(infra.needsInput).toEqual([]);
expect(openApiFeatureView({ ...base, status: "implementing", executionPlan: plan } as unknown as Feature).needsInput).toEqual([]);
});
it("reports a null product snapshot when the caller omitted productProfileId", () => {
const view = openApiFeatureView({ ...base, productProfile: null } as unknown as Feature);
expect(view.productProfileId).toBeNull();
expect(view.productProfileName).toBeNull();
});
});