@enhance-eng/sdk 0.1.0-dev.sha-3f7dd0acb078

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.
@@ -0,0 +1,19 @@
1
+ import { o as CaptureRunDescriptor } from "../index-PIb9iIwo.mjs";
2
+ import { Reporter } from "@playwright/test/reporter";
3
+ //#region src/playwright/capture-reporter.lib.d.ts
4
+ interface EnhanceReporterOptions {
5
+ readonly canvas: string;
6
+ readonly source?: string;
7
+ readonly apiUrl?: string;
8
+ readonly token?: string;
9
+ readonly runKey?: string;
10
+ readonly directory?: string;
11
+ readonly run?: CaptureRunDescriptor;
12
+ readonly participant?: string;
13
+ readonly publish?: boolean;
14
+ }
15
+ //#endregion
16
+ //#region src/playwright/reporter.adapter.d.ts
17
+ declare const ReporterAdapter: (options: EnhanceReporterOptions) => Reporter;
18
+ //#endregion
19
+ export { type EnhanceReporterOptions, ReporterAdapter as default };
@@ -0,0 +1,114 @@
1
+ import { p as captureArtifactSchema, r as writeCaptureJson } from "../capture-files.lib-BA_KBz9W.mjs";
2
+ import { t as createEnhance } from "../capture-client.client-BDphetFj.mjs";
3
+ import { n as setCaptureDirectory, t as captureEnvironment } from "../environment.config-BgNT6S_H.mjs";
4
+ import { mkdir, readFile } from "node:fs/promises";
5
+ import nodePath from "node:path";
6
+ import process from "node:process";
7
+ //#region src/playwright/capture-reporter.lib.ts
8
+ const errorOf = (error) => error instanceof Error ? error : /* @__PURE__ */ new Error("Enhance reporter could not complete the capture run.");
9
+ const initialize = async (session) => {
10
+ const current = session;
11
+ await mkdir(session.directory, { recursive: true });
12
+ try {
13
+ current.client = createEnhance({
14
+ apiUrl: session.options.apiUrl ?? captureEnvironment().apiUrl,
15
+ token: session.options.token ?? captureEnvironment().token
16
+ });
17
+ if (session.options.run === void 0) current.run = (await current.client.captures.begin({
18
+ canvas: session.options.canvas,
19
+ ...session.options.source !== void 0 && { source: session.options.source },
20
+ runKey: session.options.runKey ?? globalThis.crypto.randomUUID(),
21
+ participants: [session.participant],
22
+ directory: session.directory
23
+ })).descriptor;
24
+ else current.run = session.options.run;
25
+ 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.");
26
+ await writeCaptureJson(nodePath.join(session.directory, "ready.json"), current.run);
27
+ } catch (error) {
28
+ current.failure = errorOf(error);
29
+ await writeCaptureJson(nodePath.join(session.directory, "ready.json"), { failed: true });
30
+ }
31
+ };
32
+ const recordAttempt = async (participant, test, result) => {
33
+ const captures = [];
34
+ for (const attachment of result.attachments) {
35
+ if (attachment.name !== "enhance-capture") continue;
36
+ const bytes = attachment.body ?? (attachment.path === void 0 ? void 0 : await readFile(attachment.path));
37
+ if (bytes === void 0) throw new Error("An Enhance capture attachment is missing.");
38
+ const raw = JSON.parse(bytes.toString("utf8"));
39
+ captures.push(captureArtifactSchema.parse(raw));
40
+ }
41
+ const project = test.parent.project()?.name;
42
+ return {
43
+ participant,
44
+ testId: test.id,
45
+ project: project === void 0 || project === "" ? "default" : project,
46
+ retry: result.retry,
47
+ status: result.status,
48
+ captures
49
+ };
50
+ };
51
+ const finish = async (session, result, attempts) => {
52
+ if (session.failure !== null) throw session.failure;
53
+ if (session.run === null || session.client === null) throw new Error("Enhance run initialization did not complete.");
54
+ const manifest = {
55
+ format: "enhance-capture-run",
56
+ version: 1,
57
+ run: session.run,
58
+ ordering: "partial",
59
+ attempts,
60
+ participants: [{
61
+ id: session.participant,
62
+ status: result.status === "passed" ? "passed" : result.status === "interrupted" ? "interrupted" : "failed"
63
+ }]
64
+ };
65
+ const manifestPath = nodePath.join(session.directory, "manifest.json");
66
+ await writeCaptureJson(manifestPath, manifest);
67
+ if (result.status !== "passed") return;
68
+ if (session.options.publish !== false && session.run.participants.length === 1) {
69
+ const receipt = await session.client.captures.publishManifest(manifestPath);
70
+ process.stdout.write(`Enhance: ${receipt.canvasUrl}\n`);
71
+ } else process.stdout.write(`Enhance captures: ${manifestPath}\n`);
72
+ };
73
+ const createCaptureReporter = (options) => {
74
+ const directory = nodePath.resolve(options.directory ?? nodePath.join(".enhance", "captures"), globalThis.crypto.randomUUID());
75
+ setCaptureDirectory(directory);
76
+ const session = {
77
+ directory,
78
+ participant: options.participant ?? "local",
79
+ options,
80
+ run: null,
81
+ client: null,
82
+ failure: null
83
+ };
84
+ let ready = Promise.resolve();
85
+ const records = [];
86
+ return {
87
+ onBegin: () => {
88
+ ready = initialize(session);
89
+ },
90
+ onTestEnd: (test, result) => {
91
+ records.push(recordAttempt(session.participant, test, result));
92
+ },
93
+ onError: (error) => {
94
+ session.failure = new Error(error.message ?? "The test runner failed.");
95
+ },
96
+ onEnd: async (result) => {
97
+ try {
98
+ await ready;
99
+ await finish(session, result, await Promise.all(records));
100
+ return;
101
+ } catch (error) {
102
+ process.stderr.write(`Enhance: ${errorOf(error).message}\nCapture files: ${directory}\n`);
103
+ return { status: "failed" };
104
+ }
105
+ }
106
+ };
107
+ };
108
+ //#endregion
109
+ //#region src/playwright/reporter.adapter.ts
110
+ const ReporterAdapter = function(options) {
111
+ return createCaptureReporter(options);
112
+ };
113
+ //#endregion
114
+ export { ReporterAdapter as default };
package/package.json ADDED
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@enhance-eng/sdk",
3
+ "version": "0.1.0-dev.sha-3f7dd0acb078",
4
+ "description": "Capture editable customer journeys in Enhance from your tests.",
5
+ "homepage": "https://enhancelabs.ai",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/enhance-labs-builds/enhance3.git",
9
+ "directory": "packages/sdk"
10
+ },
11
+ "license": "UNLICENSED",
12
+ "sideEffects": false,
13
+ "type": "module",
14
+ "imports": {
15
+ "#capture-kernel": "./dist/browser/kernel.iife.js"
16
+ },
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.mts",
20
+ "import": "./dist/index.mjs"
21
+ },
22
+ "./playwright": {
23
+ "types": "./dist/playwright/index.d.mts",
24
+ "import": "./dist/playwright/index.mjs"
25
+ },
26
+ "./playwright/reporter": {
27
+ "types": "./dist/playwright/reporter.d.mts",
28
+ "import": "./dist/playwright/reporter.mjs",
29
+ "default": "./dist/playwright/reporter.mjs"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "README.md"
35
+ ],
36
+ "dependencies": {
37
+ "zod": "^4.4.3"
38
+ },
39
+ "devDependencies": {
40
+ "@playwright/test": "^1.62.0",
41
+ "tsdown": "^0.22.14",
42
+ "typescript": "^5.9.3",
43
+ "vitest": "^4.1.10",
44
+ "@enhance3/capture-browser": "0.0.0",
45
+ "@enhance3/contract": "0.0.0",
46
+ "@enhance3/tsconfig": "0.0.0"
47
+ },
48
+ "peerDependencies": {
49
+ "@playwright/test": ">=1.62.0 <2"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "@playwright/test": {
53
+ "optional": true
54
+ }
55
+ },
56
+ "engines": {
57
+ "node": ">=24"
58
+ },
59
+ "publishConfig": {
60
+ "access": "public",
61
+ "registry": "https://registry.npmjs.org"
62
+ },
63
+ "scripts": {
64
+ "build": "tsdown",
65
+ "test": "vitest run",
66
+ "typecheck": "tsc --noEmit"
67
+ }
68
+ }