sidebar-rich-content-fixture.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { createHash } from "node:crypto";
  2. import { readFile, writeFile } from "node:fs/promises";
  3. import { join, resolve } from "node:path";
  4. import { userInfo } from "node:os";
  5. import { DatabaseSync } from "node:sqlite";
  6. import { z } from "zod";
  7. import { seedProviderReadinessHistoryFixture } from "#e2e/helpers/provider-readiness-history-fixture";
  8. export const SIDEBAR_RICH_SESSION_ID = "sess_e2e_sidebar_rich_content";
  9. const rowSchema = z.object({
  10. id: z.string(),
  11. time_created: z.number(),
  12. time_updated: z.number(),
  13. data: z.string(),
  14. });
  15. const snapshotSchema = z.object({
  16. message: z.array(rowSchema),
  17. part: z.array(rowSchema.extend({ message_id: z.string() })),
  18. });
  19. /** 只向尚未启动 Host 的隔离 HOME 播种;真实会话正文只读且不进入运行报告。 */
  20. export async function seedSidebarRichContentFixture(options: {
  21. cliEntryPoint: string;
  22. e2eHomeDir: string;
  23. workspacePath: string;
  24. }) {
  25. if (resolve(options.e2eHomeDir) === resolve(userInfo().homedir)) {
  26. throw new Error("侧栏性能夹具必须使用隔离 HOME");
  27. }
  28. const syntheticText = Array.from(
  29. { length: 7 },
  30. (_, index) =>
  31. `### Section ${index + 1}\n\n${"A long conversation with responsive paragraphs. 中文段落随侧栏宽度重新换行。".repeat(12)}\n\n| Item | Description | Status |\n| --- | --- | --- |\n${Array.from({ length: 24 }, (_, row) => `| ${index}-${row} | A detailed value for a wide Markdown table, with retained horizontal scrolling | completed |`).join("\n")}`,
  32. ).join("\n\n");
  33. await seedProviderReadinessHistoryFixture({
  34. ...options,
  35. history: {
  36. sessionId: SIDEBAR_RICH_SESSION_ID,
  37. title: "E2E_SIDEBAR_RICH_CONTENT",
  38. userText: "E2E_SIDEBAR_RICH_CONTENT Inspect this completed conversation.",
  39. assistantText: syntheticText,
  40. modelId: "missing-model",
  41. runtimeProviderId: "missing-provider",
  42. taskProviderId: "glm",
  43. assistantInputTokens: 100,
  44. assistantOutputTokens: 100,
  45. },
  46. });
  47. const source = process.env.ZCODE_SIDEBAR_HISTORY_SNAPSHOT;
  48. let metadata = { source: "synthetic", sha256: "", messages: 2, parts: 2 };
  49. if (source) {
  50. const bytes = await readFile(source);
  51. const snapshot = snapshotSchema.parse(JSON.parse(bytes.toString("utf8")));
  52. const messageIds = new Set(snapshot.message.map((message) => message.id));
  53. if (
  54. snapshot.message.length < 4 ||
  55. snapshot.part.some((part) => !messageIds.has(part.message_id))
  56. ) {
  57. throw new Error("会话副本缺少完整消息或 part 所属消息");
  58. }
  59. // node:sqlite 只有同步 API;仅在测试进程、Host 启动前写入离线副本。
  60. const db = new DatabaseSync(join(options.e2eHomeDir, ".zcode/cli/db/db.sqlite"));
  61. try {
  62. db.exec("BEGIN IMMEDIATE");
  63. db.prepare("DELETE FROM part WHERE session_id = ?").run(SIDEBAR_RICH_SESSION_ID);
  64. db.prepare("DELETE FROM message WHERE session_id = ?").run(SIDEBAR_RICH_SESSION_ID);
  65. const messageInsert = db.prepare(
  66. "INSERT INTO message(id,session_id,time_created,time_updated,data) VALUES(?,?,?,?,?)",
  67. );
  68. for (const row of snapshot.message)
  69. messageInsert.run(
  70. row.id,
  71. SIDEBAR_RICH_SESSION_ID,
  72. row.time_created,
  73. row.time_updated,
  74. row.data,
  75. );
  76. const partInsert = db.prepare(
  77. "INSERT INTO part(id,message_id,session_id,time_created,time_updated,data) VALUES(?,?,?,?,?,?)",
  78. );
  79. for (const row of snapshot.part)
  80. partInsert.run(
  81. row.id,
  82. row.message_id,
  83. SIDEBAR_RICH_SESSION_ID,
  84. row.time_created,
  85. row.time_updated,
  86. row.data,
  87. );
  88. db.exec("COMMIT");
  89. } finally {
  90. db.close();
  91. }
  92. metadata = {
  93. source: "recorded-session",
  94. sha256: createHash("sha256").update(bytes).digest("hex"),
  95. messages: snapshot.message.length,
  96. parts: snapshot.part.length,
  97. };
  98. }
  99. await writeFile(
  100. join(options.e2eHomeDir, "sidebar-rich-fixture.json"),
  101. JSON.stringify(metadata, null, 2),
  102. );
  103. }