telemetryFieldContract.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import assert from "node:assert/strict";
  2. import { execFile } from "node:child_process";
  3. import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
  4. import { devNull, tmpdir } from "node:os";
  5. import { dirname, join } from "node:path";
  6. import { promisify } from "node:util";
  7. import { afterEach, test } from "vitest";
  8. import { auditTelemetryFields } from "../scripts/lib/telemetry-field-contract.mjs";
  9. const exec = promisify(execFile);
  10. const roots: string[] = [];
  11. afterEach(async () => {
  12. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  13. });
  14. async function fixture() {
  15. const root = await mkdtemp(join(tmpdir(), "telemetry-fields-"));
  16. roots.push(root);
  17. const git = (...args: string[]) =>
  18. exec("git", args, {
  19. cwd: root,
  20. env: {
  21. ...process.env,
  22. GIT_CONFIG_NOSYSTEM: "1",
  23. GIT_CONFIG_GLOBAL: devNull,
  24. GIT_AUTHOR_NAME: "Test",
  25. GIT_AUTHOR_EMAIL: "test@example.com",
  26. GIT_COMMITTER_NAME: "Test",
  27. GIT_COMMITTER_EMAIL: "test@example.com",
  28. },
  29. });
  30. const save = async (path: string, content: string) => {
  31. await mkdir(dirname(join(root, path)), { recursive: true });
  32. await writeFile(join(root, path), content);
  33. };
  34. await git("init", "-b", "test");
  35. const contractPath = join(root, "fields.json");
  36. await save(
  37. "fields.json",
  38. JSON.stringify({
  39. version: 1,
  40. events: { agent_step: { required: ["status"], optional: ["provider_name"], additions: {} } },
  41. }),
  42. );
  43. await git("add", ".");
  44. await git("commit", "-m", "test: baseline");
  45. const records = [
  46. { element_name: "agent_step", event_extra_detail: { status: "success", ui_mode: "code" } },
  47. { element_name: "agent_step", event_extra_detail: { status: "success", ui_mode: "work" } },
  48. ];
  49. const run = (options = {}) =>
  50. auditTelemetryFields({
  51. repoRoot: root,
  52. contractPath,
  53. artifactDir: join(root, "artifacts"),
  54. records,
  55. ...options,
  56. });
  57. const introduce = async (document = "`agent_step` 增加 `ui_mode`,表示发送时的模式。") => {
  58. await save("packages/ui/src/telemetry.ts", 'export const detail = { ui_mode: "code" };\n');
  59. await save("docs/monitoring/events.md", document + "\n");
  60. await git("add", "packages", "docs");
  61. await git("commit", "-m", "feat: record interface mode");
  62. return (await git("rev-parse", "HEAD")).stdout.trim();
  63. };
  64. return { root, git, save, run, introduce, contractPath, records };
  65. }
  66. test("新增字段通过并去重告知;没有依据不更新,报告不包含字段值", async () => {
  67. const f = await fixture();
  68. const before = await readFile(f.contractPath, "utf8");
  69. const result = await f.run();
  70. assert.deepEqual(result.errors, []);
  71. assert.equal(result.additions.length, 1);
  72. assert.equal(result.additions[0].status, "unverified");
  73. assert.equal(await readFile(f.contractPath, "utf8"), before);
  74. const report = await readFile(join(f.root, "artifacts/telemetry-field-audit.json"), "utf8");
  75. assert.ok(report.includes("ui_mode"));
  76. assert.ok(!report.includes('"success"'));
  77. });
  78. test("同提交实现与对应事件 spec 自动归并,保留必填清单与依据,下次不重复告知", async () => {
  79. const f = await fixture();
  80. const commit = await f.introduce();
  81. const result = await f.run();
  82. assert.equal(result.additions[0].status, "merged");
  83. const saved = JSON.parse(await readFile(f.contractPath, "utf8"));
  84. assert.deepEqual(saved.events.agent_step.required, ["status"]);
  85. assert.equal(saved.events.agent_step.additions.ui_mode.commit, commit);
  86. assert.equal(saved.events.agent_step.additions.ui_mode.document, "docs/monitoring/events.md");
  87. assert.deepEqual((await f.run()).additions, []);
  88. // 自动归并是认可扩展,不把一次观察提升为每次必填。
  89. assert.deepEqual(
  90. (
  91. await f.run({
  92. records: [{ element_name: "agent_step", event_extra_detail: { status: "success" } }],
  93. })
  94. ).errors,
  95. [],
  96. );
  97. });
  98. test("已有必填字段缺失、改名或任意字段类型错误仍失败,失败时不归并", async () => {
  99. const f = await fixture();
  100. await f.introduce();
  101. for (const detail of [
  102. { state: "success", ui_mode: "code" },
  103. { status: null, ui_mode: "code" },
  104. { status: "success", ui_mode: 1 },
  105. { status: "success", provider_name: false, ui_mode: "code" },
  106. ]) {
  107. const result = await f.run({
  108. records: [{ element_name: "agent_step", event_extra_detail: detail }],
  109. });
  110. assert.ok(result.errors.length > 0);
  111. assert.ok(!result.additions.some((item) => item.status === "merged"));
  112. }
  113. assert.deepEqual(
  114. JSON.parse(await readFile(f.contractPath, "utf8")).events.agent_step.additions,
  115. {},
  116. );
  117. });
  118. test("无关事件 spec 不构成依据", async () => {
  119. const f = await fixture();
  120. await f.introduce("`app_launch` 增加 `ui_mode`。");
  121. assert.equal((await f.run()).additions[0].status, "unverified");
  122. });
  123. test("只有测试里出现字段不能证明生产实现", async () => {
  124. const f = await fixture();
  125. await f.save("packages/ui/test/telemetry.test.ts", 'const detail = { ui_mode: "code" };');
  126. await f.save("docs/monitoring/events.md", "`agent_step` 增加 `ui_mode`。");
  127. await f.git("add", ".");
  128. await f.git("commit", "-m", "test: fixture only");
  129. assert.equal((await f.run()).additions[0].status, "unverified");
  130. });
  131. test("生产文件里的注释或普通字符串不构成字段赋值证据", async () => {
  132. const f = await fixture();
  133. await f.save(
  134. "packages/ui/src/telemetry.ts",
  135. '// ui_mode: "code"\nexport const note = "ui_mode: code";',
  136. );
  137. await f.save("docs/monitoring/events.md", "`agent_step` 增加 `ui_mode`。");
  138. await f.git("add", ".");
  139. await f.git("commit", "-m", "docs: describe a future field");
  140. assert.equal((await f.run()).additions[0].status, "unverified");
  141. });
  142. test("其他分支和已经撤销的来源不得归并", async () => {
  143. const f = await fixture();
  144. await f.git("switch", "-c", "other");
  145. const commit = await f.introduce();
  146. await f.git("switch", "test");
  147. assert.equal((await f.run()).additions[0].status, "unverified");
  148. await f.git("cherry-pick", commit);
  149. await f.git("revert", "--no-edit", "HEAD");
  150. assert.equal((await f.run()).additions[0].status, "unverified");
  151. });
  152. test("只读模式报告已验证依据,不改合同", async () => {
  153. const f = await fixture();
  154. await f.introduce();
  155. assert.equal((await f.run({ sync: false })).additions[0].status, "verified");
  156. assert.deepEqual(
  157. JSON.parse(await readFile(f.contractPath, "utf8")).events.agent_step.additions,
  158. {},
  159. );
  160. });
  161. test("并发持锁时不覆盖,Git 不可用时仍允许新增并告知", async () => {
  162. const f = await fixture();
  163. await f.introduce();
  164. await writeFile(f.contractPath + ".lock", "another run");
  165. assert.equal((await f.run()).additions[0].status, "deferred");
  166. await rm(join(f.root, ".git"), { recursive: true, force: true });
  167. assert.equal((await f.run()).additions[0].status, "unverified");
  168. });