@testpulse.run/reporter 0.1.1 → 0.1.3

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.
package/README.md CHANGED
@@ -35,4 +35,4 @@ test("streams automatically when page is used", async ({ page }) => {
35
35
  });
36
36
  ```
37
37
 
38
- When `liveStreamingEnabled` is enabled on the reporter, the fixture automatically starts and stops live capture for tests that use the `page` fixture. The package bundles `ffmpeg` automatically. To override the encoder path, set `TESTPULSE_FFMPEG_PATH`.
38
+ Live streaming is enabled by default on the reporter, and the fixture automatically starts and stops live capture for tests that use the `page` fixture. Set `liveStreamingEnabled: false` to opt out. The package bundles `ffmpeg` automatically. To override the encoder path, set `TESTPULSE_FFMPEG_PATH`.
@@ -1,6 +1,6 @@
1
1
  import { resolveTestPulseEndpoint } from "../shared/endpoint.js";
2
2
  export function resolveTestPulseFixturesOptions(options = {}, environment = process.env) {
3
- const enabled = options.enabled ?? environment.TESTPULSE_ENABLE_LIVE_STREAMING === "1";
3
+ const enabled = options.enabled ?? parseOptionalBoolean(environment.TESTPULSE_ENABLE_LIVE_STREAMING) ?? true;
4
4
  const swallowErrors = options.swallowErrors ?? true;
5
5
  return {
6
6
  enabled,
@@ -16,6 +16,19 @@ export function resolveTestPulseFixturesOptions(options = {}, environment = proc
16
16
  }
17
17
  };
18
18
  }
19
+ function parseOptionalBoolean(value) {
20
+ const normalized = value?.trim().toLowerCase();
21
+ if (!normalized) {
22
+ return undefined;
23
+ }
24
+ if (normalized === "1" || normalized === "true") {
25
+ return true;
26
+ }
27
+ if (normalized === "0" || normalized === "false") {
28
+ return false;
29
+ }
30
+ return undefined;
31
+ }
19
32
  function trim(value) {
20
33
  const trimmed = value?.trim();
21
34
  return trimmed ? trimmed : undefined;
@@ -1,4 +1,4 @@
1
- import { readdir, readFile } from "node:fs/promises";
1
+ import { access, readdir, readFile } from "node:fs/promises";
2
2
  import { getLiveCaptureStateDirectory, getLiveCaptureStatePath } from "./LiveSessionState.js";
3
3
  export async function resolveLiveCaptureSession(options, now = () => Date.now(), wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))) {
4
4
  if (options.endpoint && options.projectId && options.apiKey && options.runId && options.streamPublisherToken) {
@@ -11,6 +11,12 @@ export async function resolveLiveCaptureSession(options, now = () => Date.now(),
11
11
  createdAt: new Date(now()).toISOString()
12
12
  };
13
13
  }
14
+ if (!await shouldWaitForState(options)) {
15
+ const path = options.sessionKey
16
+ ? getLiveCaptureStatePath(options.sessionKey, options.stateDirectory)
17
+ : getLiveCaptureStateDirectory(options.stateDirectory);
18
+ throw new Error(`TestPulse live streaming session state was not found at ${path}.`);
19
+ }
14
20
  const deadline = now() + 30_000;
15
21
  while (now() < deadline) {
16
22
  const session = options.sessionKey
@@ -26,6 +32,20 @@ export async function resolveLiveCaptureSession(options, now = () => Date.now(),
26
32
  : getLiveCaptureStateDirectory(options.stateDirectory);
27
33
  throw new Error(`TestPulse live streaming session state was not found at ${path}.`);
28
34
  }
35
+ async function shouldWaitForState(options) {
36
+ if (options.sessionKey) {
37
+ return true;
38
+ }
39
+ try {
40
+ const directory = getLiveCaptureStateDirectory(options.stateDirectory);
41
+ await access(directory);
42
+ const entries = await readdir(directory);
43
+ return entries.some((entry) => entry.endsWith(".json"));
44
+ }
45
+ catch {
46
+ return false;
47
+ }
48
+ }
29
49
  async function tryReadLatestStateFile(stateDirectory, now) {
30
50
  try {
31
51
  const directory = getLiveCaptureStateDirectory(stateDirectory);
@@ -16,7 +16,8 @@ export default class TestPulseReporter {
16
16
  this.options = options;
17
17
  this.resolvedOptions = {
18
18
  ...options,
19
- endpoint: resolveTestPulseEndpoint(options.endpoint)
19
+ endpoint: resolveTestPulseEndpoint(options.endpoint),
20
+ liveStreamingEnabled: options.liveStreamingEnabled ?? true
20
21
  };
21
22
  this.runClient = new RunClient(this.resolvedOptions);
22
23
  this.artifactUploader = new ArtifactUploader(this.eventQueue, () => this.ensureRunStarted(), this.runClient);
@@ -74,7 +75,7 @@ export default class TestPulseReporter {
74
75
  await this.runClient.completeRun(runId, result.status === "failed" ? "Failed" : "Completed");
75
76
  }
76
77
  finally {
77
- if (this.options.liveStreamingEnabled) {
78
+ if (this.resolvedOptions.liveStreamingEnabled) {
78
79
  await deleteLiveCaptureState(this.options.liveStreamSessionKey, this.options.liveCaptureStateDirectory);
79
80
  }
80
81
  }
@@ -107,7 +108,7 @@ export default class TestPulseReporter {
107
108
  const metadata = await this.metadataPromise;
108
109
  const payload = await this.runClient.startRun(metadata);
109
110
  this.runId = payload.runId;
110
- if (this.options.liveStreamingEnabled) {
111
+ if (this.resolvedOptions.liveStreamingEnabled) {
111
112
  if (!payload.streamPublisherToken) {
112
113
  throw new Error("TestPulse live streaming token was not returned by the backend.");
113
114
  }
@@ -21,6 +21,7 @@ export type StartRunResponse = {
21
21
  runId: string;
22
22
  streamPublisherToken?: string;
23
23
  };
24
- export interface ResolvedTestPulseReporterOptions extends Omit<TestPulseReporterOptions, "endpoint"> {
24
+ export interface ResolvedTestPulseReporterOptions extends Omit<TestPulseReporterOptions, "endpoint" | "liveStreamingEnabled"> {
25
25
  endpoint: string;
26
+ liveStreamingEnabled: boolean;
26
27
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testpulse.run/reporter",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Playwright reporter for streaming TestPulse runs and optional live browser capture",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",