run.mjs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/env node
  2. import { mkdir, mkdtemp, rm } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { dirname, join, resolve } from "node:path";
  5. import { fileURLToPath } from "node:url";
  6. import {
  7. DEFAULT_BASE_URL,
  8. DEFAULT_MODEL,
  9. DEFAULT_PROVIDER,
  10. FAKE_CASE_NAMES,
  11. REAL_CASE_NAMES,
  12. } from "./constants.mjs";
  13. import { parseArgs, resolveCaseNames } from "./args.mjs";
  14. import { writeJsonFile } from "./artifacts.mjs";
  15. import { formatError } from "./diagnostics.mjs";
  16. import { loadZCodeModules } from "./case-utils.mjs";
  17. import { getCaseDefinitions } from "./suite-cases.mjs";
  18. const scriptDir = dirname(fileURLToPath(import.meta.url));
  19. const repoRoot = resolve(scriptDir, "../..");
  20. const fixtureDir = join(scriptDir, "fixture", "repo");
  21. async function main() {
  22. const rawOptions = parseArgs(process.argv.slice(2));
  23. const selectedCaseNames = resolveCaseNames(rawOptions.cases);
  24. const options = {
  25. ...rawOptions,
  26. baseURL: rawOptions.baseURL ?? DEFAULT_BASE_URL,
  27. model: rawOptions.model ?? DEFAULT_MODEL,
  28. };
  29. const runRoot = await mkdtemp(join(tmpdir(), "zcode-compact-e2e-"));
  30. const artifactsRoot = options.artifactsDir ? resolve(options.artifactsDir) : runRoot;
  31. const resultPath = join(artifactsRoot, "result.json");
  32. const eventsIndexPath = join(artifactsRoot, "events.json");
  33. const captureIndexPath = join(artifactsRoot, "capture.json");
  34. let keepTmp = options.keepTmp;
  35. try {
  36. await mkdir(artifactsRoot, { recursive: true });
  37. const caseDefinitions = getCaseDefinitions(selectedCaseNames);
  38. if (options.dryRun) {
  39. await writeDryRun({
  40. artifactsRoot,
  41. caseDefinitions,
  42. resultPath,
  43. selectedCaseNames,
  44. options,
  45. });
  46. return;
  47. }
  48. if (caseDefinitions.some((caseDef) => caseDef.requiresApiKey) && !process.env.ZCODE_API_KEY) {
  49. keepTmp = true;
  50. throw new Error(
  51. "ZCODE_API_KEY is required for real-provider compact E2E cases. Use --case=fake to run only local scripted cases.",
  52. );
  53. }
  54. const modules = await loadZCodeModules(repoRoot);
  55. const caseResults = [];
  56. for (const caseDef of caseDefinitions) {
  57. const caseRoot = join(runRoot, "cases", caseDef.name);
  58. try {
  59. const caseResult = await caseDef.run({
  60. artifactsRoot,
  61. caseName: caseDef.name,
  62. caseRoot,
  63. fixtureDir,
  64. modules,
  65. options,
  66. repoRoot,
  67. });
  68. caseResults.push(caseResult);
  69. console.log(JSON.stringify({ case: caseDef.name, status: "passed" }));
  70. } catch (error) {
  71. keepTmp = true;
  72. const failure = {
  73. case: caseDef.name,
  74. error: formatError(error),
  75. model: caseDef.model,
  76. status: "failed",
  77. };
  78. caseResults.push(failure);
  79. console.error(JSON.stringify(failure, null, 2));
  80. }
  81. }
  82. const suiteResult = buildSuiteResult(caseResults, {
  83. artifactsRoot,
  84. captureIndexPath,
  85. eventsIndexPath,
  86. model: `${DEFAULT_PROVIDER}/${options.model}`,
  87. resultPath,
  88. selectedCaseNames,
  89. });
  90. await writeJsonFile(resultPath, suiteResult);
  91. await writeJsonFile(eventsIndexPath, buildArtifactIndex(caseResults, "eventsPath"));
  92. await writeJsonFile(captureIndexPath, buildArtifactIndex(caseResults, "capturePath"));
  93. console.log(JSON.stringify(suiteResult, null, 2));
  94. if (suiteResult.status !== "passed") {
  95. process.exitCode = 1;
  96. }
  97. } catch (error) {
  98. keepTmp = true;
  99. const failure = {
  100. artifactsRoot,
  101. error: formatError(error),
  102. resultPath,
  103. status: "failed",
  104. };
  105. await mkdir(artifactsRoot, { recursive: true });
  106. await writeJsonFile(resultPath, failure);
  107. console.error("Compact E2E suite failed.");
  108. console.error(JSON.stringify(failure, null, 2));
  109. process.exitCode = 1;
  110. } finally {
  111. if (!keepTmp) {
  112. await rm(runRoot, { force: true, recursive: true });
  113. } else {
  114. console.error(`Temporary workspace kept at: ${runRoot}`);
  115. }
  116. }
  117. }
  118. async function writeDryRun(input) {
  119. const defaultModel = `${DEFAULT_PROVIDER}/${input.options.model}`;
  120. const reportedModel =
  121. input.caseDefinitions.length === 1
  122. ? (input.caseDefinitions[0]?.model ?? defaultModel)
  123. : defaultModel;
  124. const payload = {
  125. artifactsDir: input.artifactsRoot,
  126. baseURL: input.options.baseURL,
  127. cases: input.selectedCaseNames,
  128. dryRun: true,
  129. fakeCases: FAKE_CASE_NAMES,
  130. model: reportedModel,
  131. realCases: REAL_CASE_NAMES,
  132. resultPath: input.resultPath,
  133. status: "ok",
  134. };
  135. await writeJsonFile(input.resultPath, payload);
  136. console.log(JSON.stringify(payload, null, 2));
  137. }
  138. function buildSuiteResult(caseResults, input) {
  139. const failed = caseResults.filter((result) => result.status !== "passed");
  140. const reportedModel =
  141. caseResults.length === 1 ? (caseResults[0]?.model ?? input.model) : input.model;
  142. return {
  143. artifactsRoot: input.artifactsRoot,
  144. captureIndexPath: input.captureIndexPath,
  145. caseCount: caseResults.length,
  146. cases: caseResults.map((result) => ({
  147. capturedProviderRequestCount: result.capturedProviderRequestCount,
  148. case: result.case,
  149. capturePath: result.capturePath,
  150. eventsPath: result.eventsPath,
  151. model: result.model,
  152. resultPath: result.resultPath,
  153. status: result.status,
  154. traceId: result.traceId,
  155. })),
  156. eventsIndexPath: input.eventsIndexPath,
  157. failedCaseCount: failed.length,
  158. model: reportedModel,
  159. resultPath: input.resultPath,
  160. selectedCases: input.selectedCaseNames,
  161. status: failed.length === 0 ? "passed" : "failed",
  162. };
  163. }
  164. function buildArtifactIndex(caseResults, key) {
  165. return {
  166. cases: caseResults.map((result) => ({
  167. case: result.case,
  168. path: result[key],
  169. status: result.status,
  170. })),
  171. generatedAt: new Date().toISOString(),
  172. };
  173. }
  174. await main();