@enhance-eng/sdk 0.1.0-dev.sha-d9190c779d58 → 0.1.160

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,9 @@
1
- import { o as CaptureRunDescriptor } from "../capture-files.lib-BpwdV8DQ.mjs";
2
- import { n as CaptureConnectionOptions } from "../environment.config-CA-3pMfN.mjs";
1
+ import { p as CaptureRunDescriptor } from "../index-CDKItTQW.mjs";
2
+ import { t as BeginCaptureRun } from "../capture-client.client-B5R1OWuh.mjs";
3
+ import { n as CaptureConnectionOptions } from "../environment.config-30cIR7iz.mjs";
3
4
  import { Reporter } from "@playwright/test/reporter";
4
5
  //#region src/playwright/capture-reporter.lib.d.ts
5
- interface EnhanceReporterOptions extends CaptureConnectionOptions {
6
- readonly canvas: string;
6
+ interface EnhanceReporterOptions extends CaptureConnectionOptions, Omit<BeginCaptureRun, 'runKey'> {
7
7
  readonly source?: string;
8
8
  readonly runKey?: string;
9
9
  readonly directory?: string;
@@ -1,6 +1,6 @@
1
- import { p as captureArtifactSchema, r as writeCaptureJson } from "../capture-files.lib-BA_KBz9W.mjs";
2
- import { t as createEnhance } from "../environment.config-DW6wFq6z.mjs";
3
- import { n as setCaptureDirectory } from "../environment.config-D898JKZB.mjs";
1
+ import { B as captureLeftOutSchema, C as isCaptureError, D as fitCaptureNames, O as leftOutCapture, P as CAPTURE_LEFT_OUT_REASONS, d as captureDiagnostics, g as writeCaptureJson, z as captureArtifactSchema } from "../stall-watch.lib-qRMMj0yg.mjs";
2
+ import { t as createEnhance } from "../environment.config-BorTlECV.mjs";
3
+ import { i as setCaptureDirectory } from "../capture-context.lib-Brfs-7h6.mjs";
4
4
  import process from "node:process";
5
5
  import { mkdir, readFile } from "node:fs/promises";
6
6
  import nodePath from "node:path";
@@ -12,36 +12,90 @@ const initialize = async (session) => {
12
12
  try {
13
13
  current.client = createEnhance(session.options);
14
14
  if (session.options.run === void 0) current.run = (await current.client.captures.begin({
15
- canvas: session.options.canvas,
15
+ ...session.options,
16
16
  ...session.options.source !== void 0 && { source: session.options.source },
17
17
  runKey: session.options.runKey ?? globalThis.crypto.randomUUID(),
18
- participants: [session.participant],
18
+ participants: session.participants,
19
19
  directory: session.directory
20
20
  })).descriptor;
21
21
  else current.run = session.options.run;
22
- if (!current.run.participants.includes(session.participant) || current.run.canvasId !== session.options.canvas) throw new Error("Enhance participant or canvas differs from the reserved run.");
22
+ if (!current.run.participants.includes(session.participant) || session.options.canvas !== void 0 && current.run.canvasId !== session.options.canvas) throw new Error("Enhance participant or canvas differs from the reserved run.");
23
23
  await writeCaptureJson(nodePath.join(session.directory, "ready.json"), current.run);
24
24
  } catch (error) {
25
25
  current.failure = errorOf(error);
26
26
  await writeCaptureJson(nodePath.join(session.directory, "ready.json"), { failed: true });
27
27
  }
28
28
  };
29
- const recordAttempt = async (participant, test, result) => {
30
- const captures = [];
31
- for (const attachment of result.attachments) {
32
- if (attachment.name !== "enhance-capture") continue;
33
- const bytes = attachment.body ?? (attachment.path === void 0 ? void 0 : await readFile(attachment.path));
34
- if (bytes === void 0) throw new Error("An Enhance capture attachment is missing.");
35
- const raw = JSON.parse(bytes.toString("utf8"));
36
- captures.push(captureArtifactSchema.parse(raw));
29
+ const attachmentText = async (attachment) => {
30
+ try {
31
+ return (attachment.body ?? await readFile(attachment.path ?? "")).toString("utf8");
32
+ } catch (error) {
33
+ captureDiagnostics.publish({
34
+ stage: "attachment-read",
35
+ error
36
+ });
37
+ return null;
37
38
  }
39
+ };
40
+ const parsedJson = (text) => {
41
+ try {
42
+ return { value: JSON.parse(text) };
43
+ } catch {
44
+ return null;
45
+ }
46
+ };
47
+ /** One capture, or the plain reason it was left out: its stored copy cannot be read or parsed. */
48
+ const readCapture = async (attachment, testId) => {
49
+ const text = await attachmentText(attachment);
50
+ if (text === null) return {
51
+ testId,
52
+ message: CAPTURE_LEFT_OUT_REASONS.unreadable
53
+ };
54
+ const json = parsedJson(text);
55
+ if (json === null) return {
56
+ testId,
57
+ message: CAPTURE_LEFT_OUT_REASONS.notACapture
58
+ };
59
+ const capture = fitCaptureNames(json.value);
60
+ const parsed = captureArtifactSchema.safeParse(capture);
61
+ if (parsed.success) return parsed.data;
62
+ captureDiagnostics.publish({
63
+ stage: "format-check",
64
+ issues: parsed.error.issues
65
+ });
66
+ return leftOutCapture(capture, testId, parsed.error.issues);
67
+ };
68
+ /** A capture the step itself left out, as it recorded it. */
69
+ const readLeftOut = async (attachment, testId) => {
70
+ const text = await attachmentText(attachment);
71
+ const parsed = captureLeftOutSchema.safeParse(text === null ? null : parsedJson(text)?.value);
72
+ return parsed.success ? {
73
+ ...parsed.data,
74
+ testId
75
+ } : {
76
+ testId,
77
+ message: CAPTURE_LEFT_OUT_REASONS.unreadable
78
+ };
79
+ };
80
+ const recordAttempt = async (session, test, result) => {
81
+ const captures = [];
82
+ for (const attachment of result.attachments) if (attachment.name === "enhance-capture") {
83
+ const read = await readCapture(attachment, test.id);
84
+ if ("format" in read) captures.push(read);
85
+ else session.leftOut.push(read);
86
+ } else if (attachment.name === "enhance-capture-left-out") session.leftOut.push(await readLeftOut(attachment, test.id));
38
87
  const project = test.parent.project()?.name;
39
88
  return {
40
- participant,
89
+ participant: session.participant,
41
90
  testId: test.id,
42
91
  project: project === void 0 || project === "" ? "default" : project,
43
92
  retry: result.retry,
44
93
  status: result.status,
94
+ title: test.title,
95
+ location: {
96
+ file: nodePath.relative(session.rootDir, test.location.file),
97
+ line: test.location.line
98
+ },
45
99
  captures
46
100
  };
47
101
  };
@@ -54,6 +108,7 @@ const finish = async (session, result, attempts) => {
54
108
  run: session.run,
55
109
  ordering: "partial",
56
110
  attempts,
111
+ ...session.leftOut.length > 0 && { leftOut: session.leftOut },
57
112
  participants: [{
58
113
  id: session.participant,
59
114
  status: result.status === "passed" ? "passed" : result.status === "interrupted" ? "interrupted" : "failed"
@@ -61,11 +116,12 @@ const finish = async (session, result, attempts) => {
61
116
  };
62
117
  const manifestPath = nodePath.join(session.directory, "manifest.json");
63
118
  await writeCaptureJson(manifestPath, manifest);
64
- if (result.status !== "passed") return;
65
- if (session.options.publish !== false && session.run.participants.length === 1) {
66
- const receipt = await session.client.captures.publishManifest(manifestPath);
67
- process.stdout.write(`Enhance: ${receipt.canvasUrl}\n`);
68
- } else process.stdout.write(`Enhance captures: ${manifestPath}\n`);
119
+ if (session.options.publish !== false && session.run.participants.length === 1) await session.client.captures.publishManifest(manifestPath, { write: (text) => process.stdout.write(text) });
120
+ else process.stdout.write(`Enhance captures: ${manifestPath}\n`);
121
+ };
122
+ const writeFailedReadiness = async (directory) => {
123
+ await mkdir(directory, { recursive: true });
124
+ await writeCaptureJson(nodePath.join(directory, "ready.json"), { failed: true });
69
125
  };
70
126
  const createCaptureReporter = (options) => {
71
127
  const directory = nodePath.resolve(options.directory ?? nodePath.join(".enhance", "captures"), globalThis.crypto.randomUUID());
@@ -73,22 +129,33 @@ const createCaptureReporter = (options) => {
73
129
  const session = {
74
130
  directory,
75
131
  participant: options.participant ?? "local",
132
+ participants: options.participants ?? [options.participant ?? "local"],
76
133
  options,
77
134
  run: null,
78
135
  client: null,
79
- failure: null
136
+ failure: null,
137
+ leftOut: [],
138
+ rootDir: process.cwd()
80
139
  };
81
140
  let ready = Promise.resolve();
82
141
  const records = [];
83
142
  return {
84
- onBegin: () => {
143
+ onBegin: (config) => {
144
+ session.rootDir = config?.rootDir ?? session.rootDir;
145
+ const shard = config?.shard ?? null;
146
+ if (shard !== null && options.run === void 0 && options.participants === void 0) {
147
+ session.participant = `shard-${String(shard.current)}`;
148
+ session.participants = Array.from({ length: shard.total }, (_, index) => `shard-${String(index + 1)}`);
149
+ if (options.runKey === void 0) {
150
+ session.failure = /* @__PURE__ */ new Error("Sharded captures require one shared runKey for the execution.");
151
+ ready = writeFailedReadiness(directory);
152
+ return;
153
+ }
154
+ }
85
155
  ready = initialize(session);
86
156
  },
87
157
  onTestEnd: (test, result) => {
88
- records.push(recordAttempt(session.participant, test, result));
89
- },
90
- onError: (error) => {
91
- session.failure = new Error(error.message ?? "The test runner failed.");
158
+ records.push(recordAttempt(session, test, result));
92
159
  },
93
160
  onEnd: async (result) => {
94
161
  try {
@@ -96,6 +163,10 @@ const createCaptureReporter = (options) => {
96
163
  await finish(session, result, await Promise.all(records));
97
164
  return;
98
165
  } catch (error) {
166
+ if (isCaptureError(error) && error.code === "pending") {
167
+ process.stdout.write(`Enhance: ${error.message}\n`);
168
+ return;
169
+ }
99
170
  process.stderr.write(`Enhance: ${errorOf(error).message}\nCapture files: ${directory}\n`);
100
171
  return { status: "failed" };
101
172
  }