92 lines
5.8 KiB
TypeScript
92 lines
5.8 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { bjDay, buildUsageStats, outcomeOf, parseClientHeader, taskChannel } from "../src/openapi_usage_core";
|
|
|
|
const DAY = 86_400_000;
|
|
const NOW = Date.UTC(2026, 9, 20, 4, 0, 0); // 北京时间 10-20 12:00
|
|
|
|
describe("客户端自报", () => {
|
|
test("版本和 Agent;不认识的 Agent 记 other,缺省为 null", () => {
|
|
expect(parseClientHeader("streamline-cli/0.2.0; agent=claude-code")).toEqual({ version: "0.2.0", agent: "claude-code" });
|
|
expect(parseClientHeader("streamline-cli/1.0.0-beta.1; agent=windsurf")).toEqual({ version: "1.0.0-beta.1", agent: "other" });
|
|
expect(parseClientHeader(null)).toEqual({ version: null, agent: null });
|
|
expect(parseClientHeader("curl/8.0")).toEqual({ version: null, agent: null });
|
|
});
|
|
});
|
|
|
|
describe("口径", () => {
|
|
test("北京时间切日", () => {
|
|
expect(bjDay(Date.UTC(2026, 9, 19, 16, 30))).toBe("2026-10-20");
|
|
expect(bjDay(Date.UTC(2026, 9, 19, 15, 59))).toBe("2026-10-19");
|
|
});
|
|
test("入口:Key 优先于 created_channel;其余算网页", () => {
|
|
expect(taskChannel({ createdViaKeyId: "opk_1", createdChannel: null })).toBe("openapi");
|
|
expect(taskChannel({ createdViaKeyId: null, createdChannel: "cli" })).toBe("cli");
|
|
expect(taskChannel({ createdViaKeyId: null, createdChannel: null })).toBe("web");
|
|
});
|
|
test("状态分段:manual_review 算已修复,info_required 算评估中", () => {
|
|
expect(outcomeOf("manual_review")).toBe("fixed");
|
|
expect(outcomeOf("info_required")).toBe("evaluating");
|
|
expect(outcomeOf("unreproducible")).toBe("notFixed");
|
|
expect(outcomeOf("fixing")).toBe("running");
|
|
expect(outcomeOf("archived")).toBe("closed");
|
|
});
|
|
});
|
|
|
|
test("聚合:占比与环比、活跃只认成功调用、窗口外不计", () => {
|
|
const t = (id: string, ago: number, extra: Partial<{ createdViaKeyId: string; createdChannel: string; creatorEmail: string; status: string }> = {}) => ({
|
|
id, createdAt: NOW - ago, createdViaKeyId: extra.createdViaKeyId ?? null, createdChannel: extra.createdChannel ?? null,
|
|
creatorEmail: extra.creatorEmail ?? null, status: extra.status ?? "initializing",
|
|
});
|
|
const stats = buildUsageStats({
|
|
now: NOW, days: 7,
|
|
tasks: [
|
|
t("w1", 1 * DAY), t("w2", 2 * DAY),
|
|
t("k1", 1 * DAY, { createdViaKeyId: "opk_a", status: "fix_succeeded" }),
|
|
t("c1", 3 * DAY, { createdChannel: "cli", creatorEmail: "alice@bytedance.com", status: "fixing" }),
|
|
t("h1", 2 * DAY, { createdChannel: "cli", creatorEmail: "alice@bytedance.com", status: "fix_succeeded" }),
|
|
// 上一个窗口:2 单都是网页 → 环比基线 0%
|
|
t("p1", 8 * DAY), t("p2", 9 * DAY),
|
|
// 再往前的不进任何窗口
|
|
t("old", 30 * DAY, { createdViaKeyId: "opk_a" }),
|
|
],
|
|
events: [
|
|
{ at: NOW - DAY, channel: "openapi", actor: "opk_a", route: "POST /openapi/v1/tasks", status: 200, agent: null, taskId: "k1" },
|
|
{ at: NOW - DAY, channel: "openapi", actor: "opk_b", route: "POST /openapi/v1/tasks", status: 400, agent: null, taskId: null },
|
|
{ at: NOW - 3 * DAY, channel: "cli", actor: "alice@bytedance.com", route: "POST /cli/v1/tasks", status: 200, agent: "claude-code", taskId: "c1" },
|
|
{ at: NOW - 2 * DAY, channel: "cli", actor: "alice@bytedance.com", route: "POST /cli/v1/handoff", status: 200, agent: "claude-code", taskId: "h1" },
|
|
{ at: NOW - 2 * DAY, channel: "cli", actor: "alice@bytedance.com", route: "POST /cli/v1/tasks/:taskId/execute", status: 200, agent: "cursor", taskId: "c1" },
|
|
{ at: NOW - 20 * DAY, channel: "cli", actor: "bob@bytedance.com", route: "GET /cli/v1/tasks", status: 200, agent: "codex", taskId: null },
|
|
],
|
|
keys: [
|
|
{ id: "opk_a", label: "Meego 同步", source: "meego", createdBy: "x@bytedance.com", revoked: false, lastUsedAt: null },
|
|
{ id: "opk_b", label: "坏掉的", source: "y", createdBy: "y@bytedance.com", revoked: false, lastUsedAt: null },
|
|
{ id: "opk_c", label: "已吊销", source: "z", createdBy: "z@bytedance.com", revoked: true, lastUsedAt: null },
|
|
],
|
|
sessions: [
|
|
{ email: "alice@bytedance.com", createdAt: NOW - 3 * DAY, lastUsedAt: NOW - DAY, expiresAt: NOW + 20 * DAY },
|
|
{ email: "bob@bytedance.com", createdAt: NOW - 25 * DAY, lastUsedAt: null, expiresAt: NOW + 5 * DAY },
|
|
{ email: "carol@bytedance.com", createdAt: null, lastUsedAt: null, expiresAt: NOW - 1 },
|
|
],
|
|
});
|
|
expect(stats.summary.tasks).toEqual({ web: 2, openapi: 1, cli: 2, total: 5 });
|
|
expect(stats.summary.nonWebShare).toBe(0.6);
|
|
expect(stats.summary.prevNonWebShare).toBe(0);
|
|
expect(stats.summary.activeKeys).toBe(1); // opk_b 只有失败调用
|
|
expect(stats.summary.totalKeys).toBe(2);
|
|
expect(stats.summary.activeCliUsers).toBe(1); // bob 的调用在窗口外
|
|
expect(stats.summary.authorizedCliUsers).toBe(2); // carol 已过期
|
|
expect(stats.summary.newCliUsers).toBe(1);
|
|
expect(stats.summary.calls).toEqual({ total: 5, openapi: 2, cli: 3, errors: 1 });
|
|
expect(stats.daily).toHaveLength(7);
|
|
expect(stats.daily.at(-1)!.date).toBe("2026-10-20");
|
|
const alice = stats.actors.find((a) => a.id === "alice@bytedance.com")!;
|
|
expect(alice).toMatchObject({ channel: "cli", name: "alice", tasks: 2, executes: 1, calls: 3, agents: ["claude-code", "cursor"] });
|
|
const keyA = stats.actors.find((a) => a.id === "opk_a")!;
|
|
expect(keyA).toMatchObject({ name: "Meego 同步", owner: "x@bytedance.com", tasks: 1 });
|
|
expect(stats.outcomes.find((o) => o.channel === "openapi")).toMatchObject({ total: 1, fixed: 1 });
|
|
expect(stats.outcomes.find((o) => o.channel === "cli")).toMatchObject({ total: 2, running: 1, fixed: 1 });
|
|
// 接力建的单是 Skill 的子集,单独一行,用来对比带线索和不带线索的修复率。
|
|
expect(stats.outcomes.find((o) => o.channel === "handoff")).toMatchObject({ total: 1, fixed: 1 });
|
|
expect(stats.agents.map((a) => a.agent).sort()).toEqual(["claude-code", "cursor"]);
|
|
});
|