@testivai/witness-playwright 1.8.0 → 2.0.0

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
@@ -113,6 +113,6 @@ MIT
113
113
 
114
114
  ## Support
115
115
 
116
- - Documentation: https://github.com/mcbuddy/testivai-oss/tree/main/packages/playwright
117
- - Issues: https://github.com/mcbuddy/testivai-oss/issues
116
+ - Documentation: https://github.com/testivai/testivai-oss/tree/main/packages/playwright
117
+ - Issues: https://github.com/testivai/testivai-oss/issues
118
118
  - Website: https://testiv.ai
@@ -14,7 +14,7 @@
14
14
  * These functions are pure (no Playwright dependency) so they can be
15
15
  * exercised in plain Jest unit tests without a browser.
16
16
  */
17
- import { TestivAIConfig, TestivAIProjectConfig, IgnoreMode, IgnoreSelectorInput } from '../types';
17
+ import type { TestivAIConfig, TestivAIProjectConfig, IgnoreMode, IgnoreSelectorInput } from '../types';
18
18
  /** A fully-resolved ignore entry with an explicit mode. */
19
19
  export interface IgnoreRule {
20
20
  selector: string;
@@ -1,4 +1,4 @@
1
- import { TestivAIProjectConfig, TestivAIConfig } from '../types';
1
+ import type { TestivAIProjectConfig, TestivAIConfig } from '../types';
2
2
  /**
3
3
  * Load TestivAI configuration from file system
4
4
  * Supports both .ts and .js config files
@@ -7,23 +7,10 @@ import { TestivAIProjectConfig, TestivAIConfig } from '../types';
7
7
  */
8
8
  export declare function loadConfig(): Promise<TestivAIProjectConfig>;
9
9
  /**
10
- * Merge per-test configuration with project configuration
10
+ * Merge per-snapshot configuration with project configuration
11
11
  *
12
12
  * @param projectConfig The project-level configuration
13
- * @param testConfig Optional per-test configuration overrides
14
- * @returns TestivAIConfig The effective configuration for this test
13
+ * @param testConfig Optional per-snapshot configuration overrides
14
+ * @returns TestivAIConfig The effective configuration for this snapshot
15
15
  */
16
- export declare function mergeTestConfig(projectConfig: TestivAIProjectConfig, testConfig?: TestivAIConfig): TestivAIConfig;
17
- /**
18
- * Detect current environment (CI, development, production)
19
- *
20
- * @returns string The detected environment
21
- */
22
- export declare function detectEnvironment(): string;
23
- /**
24
- * Apply environment-specific overrides to configuration
25
- *
26
- * @param config The base configuration
27
- * @returns TestivAIProjectConfig Configuration with environment overrides applied
28
- */
29
- export declare function applyEnvironmentOverrides(config: TestivAIProjectConfig): TestivAIProjectConfig;
16
+ export declare function mergeTestConfig(_projectConfig: TestivAIProjectConfig, testConfig?: TestivAIConfig): TestivAIConfig;
@@ -35,23 +35,12 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.loadConfig = loadConfig;
37
37
  exports.mergeTestConfig = mergeTestConfig;
38
- exports.detectEnvironment = detectEnvironment;
39
- exports.applyEnvironmentOverrides = applyEnvironmentOverrides;
40
38
  const fs = __importStar(require("fs-extra"));
41
39
  const path = __importStar(require("path"));
42
40
  /**
43
41
  * Default configuration when no config file is found
44
42
  */
45
- const DEFAULT_CONFIG = {
46
- layout: {
47
- sensitivity: 2, // Balanced sensitivity (0-4 scale)
48
- tolerance: 1.0, // 1 pixel base tolerance
49
- },
50
- ai: {
51
- sensitivity: 2, // Balanced AI analysis (0-4 scale)
52
- confidence: 0.7, // 70% confidence required for AI_BUG
53
- }
54
- };
43
+ const DEFAULT_CONFIG = {};
55
44
  /**
56
45
  * Load TestivAI configuration from file system
57
46
  * Supports both .ts and .js config files
@@ -73,7 +62,7 @@ async function loadConfig() {
73
62
  configPath = jsConfigPath;
74
63
  }
75
64
  else {
76
- console.log('⚠️ No testivai.config.ts or testivai.config.js found, using defaults');
65
+ // No config file is the normal zero-config case — silently use defaults
77
66
  return DEFAULT_CONFIG;
78
67
  }
79
68
  // Load configuration based on file type
@@ -88,171 +77,40 @@ async function loadConfig() {
88
77
  configModule = await Promise.resolve(`${configPath}`).then(s => __importStar(require(s)));
89
78
  }
90
79
  const config = configModule.default || configModule;
91
- // Validate and merge with defaults
92
- return validateAndMergeConfig(config);
80
+ if (!config) {
81
+ return DEFAULT_CONFIG;
82
+ }
83
+ return {
84
+ ignoreSelectors: config.ignoreSelectors,
85
+ stabilize: config.stabilize,
86
+ };
93
87
  }
94
88
  catch (error) {
95
- console.warn('⚠️ Failed to load testivai config, using defaults:', error);
89
+ console.warn('Warning: failed to load testivai config, using defaults:', error);
96
90
  return DEFAULT_CONFIG;
97
91
  }
98
92
  }
99
93
  /**
100
- * Merge per-test configuration with project configuration
94
+ * Merge per-snapshot configuration with project configuration
101
95
  *
102
96
  * @param projectConfig The project-level configuration
103
- * @param testConfig Optional per-test configuration overrides
104
- * @returns TestivAIConfig The effective configuration for this test
97
+ * @param testConfig Optional per-snapshot configuration overrides
98
+ * @returns TestivAIConfig The effective configuration for this snapshot
105
99
  */
106
- function mergeTestConfig(projectConfig, testConfig) {
100
+ function mergeTestConfig(_projectConfig, testConfig) {
107
101
  if (!testConfig) {
108
- return {
109
- layout: projectConfig.layout,
110
- ai: projectConfig.ai,
111
- // @renamed: dom → structure (IP protection)
112
- structure: projectConfig.structure,
113
- performanceMetrics: projectConfig.performanceMetrics
114
- };
102
+ return {};
115
103
  }
116
104
  return {
117
- layout: {
118
- ...projectConfig.layout,
119
- ...testConfig.layout
120
- },
121
- ai: {
122
- ...projectConfig.ai,
123
- ...testConfig.ai
124
- },
125
- // @renamed: dom → structure (IP protection)
126
- structure: {
127
- ...projectConfig.structure,
128
- ...testConfig.structure
129
- },
130
- performanceMetrics: {
131
- ...projectConfig.performanceMetrics,
132
- ...testConfig.performanceMetrics
133
- },
134
- selectors: testConfig.selectors,
135
105
  useBrowserCapture: testConfig.useBrowserCapture,
136
106
  // Per-call capture options must survive the merge: without these,
137
107
  // witness(page, testInfo, name, { ignoreSelectors }) silently ignored
138
108
  // the selectors (masked for a long time by the diff engine's cumulated
139
109
  // threshold absorbing the tiny leaked pixels).
110
+ // Project-level ignoreSelectors and stabilize are read separately
111
+ // (collectIgnoreRules / resolveStabilize) so they are NOT merged here.
140
112
  ignoreSelectors: testConfig.ignoreSelectors,
141
113
  stabilize: testConfig.stabilize,
142
- mask: testConfig.mask
114
+ mask: testConfig.mask,
143
115
  };
144
116
  }
145
- /**
146
- * Detect current environment (CI, development, production)
147
- *
148
- * @returns string The detected environment
149
- */
150
- function detectEnvironment() {
151
- // Check for common CI environment variables
152
- if (process.env.CI || process.env.CONTINUOUS_INTEGRATION) {
153
- return 'ci';
154
- }
155
- // Check for production indicators
156
- if (process.env.NODE_ENV === 'production') {
157
- return 'production';
158
- }
159
- // Default to development
160
- return 'development';
161
- }
162
- /**
163
- * Apply environment-specific overrides to configuration
164
- *
165
- * @param config The base configuration
166
- * @returns TestivAIProjectConfig Configuration with environment overrides applied
167
- */
168
- function applyEnvironmentOverrides(config) {
169
- const environment = detectEnvironment();
170
- if (!config.environments) {
171
- return config;
172
- }
173
- // Type-safe environment override access
174
- let envOverrides;
175
- switch (environment) {
176
- case 'ci':
177
- envOverrides = config.environments.ci;
178
- break;
179
- case 'development':
180
- envOverrides = config.environments.development;
181
- break;
182
- case 'production':
183
- envOverrides = config.environments.production;
184
- break;
185
- }
186
- if (!envOverrides) {
187
- return config;
188
- }
189
- return {
190
- layout: {
191
- ...config.layout,
192
- ...envOverrides.layout
193
- },
194
- ai: {
195
- ...config.ai,
196
- ...envOverrides.ai
197
- },
198
- environments: config.environments // Keep the original environments config
199
- };
200
- }
201
- /**
202
- * Validate configuration values and merge with defaults
203
- *
204
- * @param config The configuration to validate
205
- * @returns TestivAIProjectConfig Validated configuration
206
- */
207
- function validateAndMergeConfig(config) {
208
- // Guard against null/undefined config
209
- if (!config) {
210
- console.warn('⚠️ Config is null or undefined, using defaults');
211
- return DEFAULT_CONFIG;
212
- }
213
- // Basic validation
214
- const validatedConfig = {
215
- // API configuration with production default
216
- apiKey: config.apiKey,
217
- apiUrl: config.apiUrl || 'https://core-api.testiv.ai',
218
- layout: {
219
- sensitivity: validateRange(config.layout?.sensitivity ?? DEFAULT_CONFIG.layout.sensitivity, 0, 4, 'layout.sensitivity', DEFAULT_CONFIG.layout.sensitivity),
220
- tolerance: validateRange(config.layout?.tolerance ?? DEFAULT_CONFIG.layout.tolerance, 0, 100, 'layout.tolerance', DEFAULT_CONFIG.layout.tolerance),
221
- selectorTolerances: config.layout?.selectorTolerances,
222
- useRelativeTolerance: config.layout?.useRelativeTolerance,
223
- relativeTolerance: config.layout?.relativeTolerance
224
- },
225
- ai: {
226
- sensitivity: validateRange(config.ai?.sensitivity ?? DEFAULT_CONFIG.ai.sensitivity, 0, 4, 'ai.sensitivity', DEFAULT_CONFIG.ai.sensitivity),
227
- confidence: validateRange(config.ai?.confidence ?? DEFAULT_CONFIG.ai.confidence, 0, 1, 'ai.confidence', DEFAULT_CONFIG.ai.confidence),
228
- enableReasoning: config.ai?.enableReasoning
229
- },
230
- // @renamed: dom → structure (IP protection)
231
- structure: config.structure, // Pass through structure analysis configuration as-is
232
- performanceMetrics: config.performanceMetrics, // Pass through performance metrics configuration as-is
233
- environments: config.environments
234
- };
235
- return applyEnvironmentOverrides(validatedConfig);
236
- }
237
- /**
238
- * Validate that a number is within the expected range
239
- *
240
- * @param value The value to validate
241
- * @param min Minimum allowed value
242
- * @param max Maximum allowed value
243
- * @param field Field name for error messages
244
- * @param defaultValue Default value to use when validation fails
245
- * @returns number The validated value
246
- */
247
- function validateRange(value, min, max, field, defaultValue) {
248
- const numValue = Number(value);
249
- if (isNaN(numValue)) {
250
- console.warn(`⚠️ Invalid ${field}: ${value}, using default: ${defaultValue}`);
251
- return defaultValue;
252
- }
253
- if (numValue < min || numValue > max) {
254
- console.warn(`⚠️ ${field} must be between ${min} and ${max}, got ${numValue}, using default: ${defaultValue}`);
255
- return defaultValue;
256
- }
257
- return numValue;
258
- }
@@ -14,7 +14,7 @@
14
14
  * 4. default true
15
15
  */
16
16
  import type { Page } from '@playwright/test';
17
- import { TestivAIConfig, TestivAIProjectConfig } from '../types';
17
+ import type { TestivAIConfig, TestivAIProjectConfig } from '../types';
18
18
  /**
19
19
  * CSS injected for the duration of the capture. Near-zero durations (not
20
20
  * `none`) let every animation/transition COMPLETE instantly at its final
@@ -33,3 +33,21 @@ export declare function resolveStabilize(projectRoot: string, projectConfig: Tes
33
33
  * a missing FontFaceSet API just means no wait.
34
34
  */
35
35
  export declare function waitForFonts(page: Page): Promise<void>;
36
+ /**
37
+ * Wait until the page has stopped changing: document complete, images
38
+ * finished, fonts loaded, and no DOM mutations for `quietMs`.
39
+ *
40
+ * Deliberately NOT `networkidle` — Playwright's own docs mark that DISCOURAGED
41
+ * for testing, and it is the wrong signal for a visual snapshot: a page with
42
+ * analytics beacons never goes quiet, while a network-idle page can still be
43
+ * animating. What matters is whether the rendered page settled.
44
+ *
45
+ * Always bounded; a page that never settles yields a capture, not a hang.
46
+ */
47
+ export declare function waitForSettled(page: {
48
+ evaluate: (expr: string) => Promise<unknown>;
49
+ }, quietMs?: number, timeoutMs?: number): Promise<void>;
50
+ /** Detach the settle observer so it does not linger on the page under test. */
51
+ export declare function stopSettleObserver(page: {
52
+ evaluate: (expr: string) => Promise<unknown>;
53
+ }): Promise<void>;
@@ -52,6 +52,9 @@ exports.STABILIZE_CSS = void 0;
52
52
  exports.readWitnessConfigStabilize = readWitnessConfigStabilize;
53
53
  exports.resolveStabilize = resolveStabilize;
54
54
  exports.waitForFonts = waitForFonts;
55
+ exports.waitForSettled = waitForSettled;
56
+ exports.stopSettleObserver = stopSettleObserver;
57
+ const witness_1 = require("@testivai/witness");
55
58
  const fs = __importStar(require("fs"));
56
59
  const path = __importStar(require("path"));
57
60
  /**
@@ -110,3 +113,49 @@ async function waitForFonts(page) {
110
113
  // Page navigated or evaluate failed — capture proceeds unstabilized fonts
111
114
  }
112
115
  }
116
+ /**
117
+ * Wait until the page has stopped changing: document complete, images
118
+ * finished, fonts loaded, and no DOM mutations for `quietMs`.
119
+ *
120
+ * Deliberately NOT `networkidle` — Playwright's own docs mark that DISCOURAGED
121
+ * for testing, and it is the wrong signal for a visual snapshot: a page with
122
+ * analytics beacons never goes quiet, while a network-idle page can still be
123
+ * animating. What matters is whether the rendered page settled.
124
+ *
125
+ * Always bounded; a page that never settles yields a capture, not a hang.
126
+ */
127
+ async function waitForSettled(page, quietMs = witness_1.DEFAULT_QUIET_MS, timeoutMs = witness_1.DEFAULT_SETTLE_TIMEOUT_MS) {
128
+ const expr = (0, witness_1.buildSettleProbeExpression)(quietMs);
129
+ const deadline = Date.now() + timeoutMs;
130
+ for (;;) {
131
+ let state;
132
+ try {
133
+ state = (await page.evaluate(expr));
134
+ }
135
+ catch {
136
+ return; // probe unavailable — never block the capture
137
+ }
138
+ // An unusable answer means we cannot probe; polling to the timeout would
139
+ // add the full wait to every capture.
140
+ if (!state || typeof state.settled !== 'boolean')
141
+ return;
142
+ if (state.settled)
143
+ return;
144
+ if (Date.now() >= deadline) {
145
+ if (process.env.TESTIVAI_DEBUG === 'true') {
146
+ console.log(`[TestivAI] page did not settle within ${timeoutMs}ms — capturing anyway`);
147
+ }
148
+ return;
149
+ }
150
+ await new Promise((resolve) => setTimeout(resolve, 50));
151
+ }
152
+ }
153
+ /** Detach the settle observer so it does not linger on the page under test. */
154
+ async function stopSettleObserver(page) {
155
+ try {
156
+ await page.evaluate(witness_1.SETTLE_STOP_EXPRESSION);
157
+ }
158
+ catch {
159
+ // best-effort cleanup
160
+ }
161
+ }
package/dist/index.d.ts CHANGED
@@ -1,8 +1,6 @@
1
1
  import { snapshot } from './snapshot';
2
- import { getCiRunId } from './ci';
3
2
  export { snapshot, snapshot as witness };
4
3
  export declare const testivai: {
5
4
  witness: typeof snapshot;
6
- ci: typeof getCiRunId;
7
5
  };
8
- export type { TestivAIConfig, TestivAIProjectConfig, StructureAnalysisConfig, StructureAnalysis } from './types';
6
+ export type { TestivAIConfig, TestivAIProjectConfig, IgnoreMode, IgnoreSelectorInput } from './types';
package/dist/index.js CHANGED
@@ -4,10 +4,6 @@ exports.testivai = exports.witness = exports.snapshot = void 0;
4
4
  const snapshot_1 = require("./snapshot");
5
5
  Object.defineProperty(exports, "snapshot", { enumerable: true, get: function () { return snapshot_1.snapshot; } });
6
6
  Object.defineProperty(exports, "witness", { enumerable: true, get: function () { return snapshot_1.snapshot; } });
7
- const ci_1 = require("./ci");
8
7
  exports.testivai = {
9
8
  witness: snapshot_1.snapshot,
10
- ci: ci_1.getCiRunId,
11
9
  };
12
- // Structure analyzer is now handled on the backend
13
- // The types are kept for backwards compatibility
@@ -1,9 +1,5 @@
1
- import { Reporter, FullConfig, Suite, FullResult } from './reporter-types';
2
- import { type CompressionOptions } from '@testivai/common';
1
+ import type { Reporter, FullConfig, Suite, FullResult } from './reporter-types';
3
2
  interface TestivaiReporterOptions {
4
- apiUrl?: string;
5
- apiKey?: string;
6
- compression?: CompressionOptions;
7
3
  debug?: boolean;
8
4
  /**
9
5
  * Capture only — write `.testivai/temp/` and skip comparison and report
@@ -23,13 +19,7 @@ interface TestivaiReporterOptions {
23
19
  }
24
20
  export declare class TestivAIPlaywrightReporter implements Reporter {
25
21
  private options;
26
- private gitInfo;
27
- private browserInfo;
28
- private runId;
29
- private ciInfo;
30
22
  private tempDir;
31
- private compressionHelper;
32
- private localMode;
33
23
  private captureOnly;
34
24
  private shard;
35
25
  constructor(options?: TestivaiReporterOptions);
@@ -40,7 +30,7 @@ export declare class TestivAIPlaywrightReporter implements Reporter {
40
30
  * 3. auto: Playwright says this is shard i/N with N > 1
41
31
  */
42
32
  private resolveCaptureOnly;
43
- onBegin(config: FullConfig, suite: Suite): Promise<void>;
33
+ onBegin(config: FullConfig, _suite: Suite): Promise<void>;
44
34
  onEnd(result: FullResult): Promise<void>;
45
35
  }
46
36
  export default TestivAIPlaywrightReporter;