@povio/lightning-panda 0.0.0-stage → 0.0.17

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,173 @@
1
+ import { Cdp } from './cdp.js';
2
+ import { type ScreenshotOptions, type StepLike } from './steps.js';
3
+ import { type ScreenshotResult, type SentBaselines } from './screenshots.js';
4
+ export * from './steps.js';
5
+ export { Cdp, CdpError } from './cdp.js';
6
+ export { ScreenshotMismatchError, updateMode, type ScreenshotResult } from './screenshots.js';
7
+ export type Profile = {
8
+ /** Where its pages boot (and stay until a test takes one). */
9
+ url: string;
10
+ /** Written to localStorage before the app's scripts run (e.g. auth tokens). */
11
+ localStorage?: Record<string, string>;
12
+ /** Run in every document before its scripts. */
13
+ initScripts?: string[];
14
+ viewport?: {
15
+ width: number;
16
+ height: number;
17
+ };
18
+ /** Pages kept booted (1-8). */
19
+ pool?: number;
20
+ /** Keep pages booted ahead (default); false boots each page when a test asks. */
21
+ prepare?: boolean;
22
+ /**
23
+ * Steps run once on the booted page before tests get it (`serve --fork`
24
+ * only): every page of this profile then starts from their result.
25
+ */
26
+ setup?: StepLike[];
27
+ /**
28
+ * Starts from another profile's snapshot (`serve --fork` only): its page is
29
+ * forked from that profile's, and only this profile's `setup` runs on it.
30
+ */
31
+ base?: string;
32
+ };
33
+ export type RunResult = {
34
+ /** Values of `read(as)` and `evaluate(expr, as)` steps. */
35
+ values: Record<string, unknown>;
36
+ /** `matchScreenshot` steps by name. */
37
+ screenshots: Record<string, ScreenshotResult>;
38
+ /** One entry per step: what a check saw, a request it matched, settle time. */
39
+ results: unknown[];
40
+ /** Requests the page made during the run. */
41
+ requests: {
42
+ method: string;
43
+ url: string;
44
+ status: number | null;
45
+ error: string | null;
46
+ ms: number | null;
47
+ }[];
48
+ /** Time in the browser. */
49
+ ms: number;
50
+ };
51
+ export declare class RunError extends Error {
52
+ readonly step: number;
53
+ constructor(message: string, step: number);
54
+ }
55
+ /** One CDP connection: one browser thread, with its own page pools and screenshot baselines. */
56
+ type Conn = {
57
+ cdp: Cdp;
58
+ open: number;
59
+ baselines: SentBaselines;
60
+ /** Opened for one page (fork mode): closed with it. */
61
+ own?: boolean;
62
+ };
63
+ export type ConnectOptions = {
64
+ /**
65
+ * CDP connections to open (default 1). Each is its own browser thread with
66
+ * its own pools: a test takes a page from the connection with the fewest
67
+ * open pages, and a connection refills its pools as soon as it has no open
68
+ * page, in parallel with tests on the others.
69
+ */
70
+ connections?: number;
71
+ };
72
+ export declare class Lightningpanda {
73
+ private wsUrl;
74
+ private conns;
75
+ private child?;
76
+ private profiles;
77
+ private profileMutation;
78
+ private turn;
79
+ /** `serve --fork`: one connection (a forked process) per page. */
80
+ private fork;
81
+ /** Pages taken from a pool, and pages booted on demand because none was waiting. */
82
+ readonly stats: {
83
+ prepared: number;
84
+ preparedBooting: number;
85
+ fresh: number;
86
+ };
87
+ private constructor();
88
+ /** Asks the server whether it forks connections (`serve --fork`). */
89
+ private detect;
90
+ /** The first connection, for browser-level commands. */
91
+ get cdp(): Cdp;
92
+ static connect(wsUrl: string, opts?: ConnectOptions): Promise<Lightningpanda>;
93
+ /**
94
+ * Starts `lightningpanda serve` on a free port and connects to it. The binary
95
+ * is `bin`, else `$LIGHTNINGPANDA_BIN`, else the bundled binary for this
96
+ * platform (or the sibling binary in a legacy release archive).
97
+ */
98
+ static launch(opts?: ConnectOptions & {
99
+ bin?: string;
100
+ args?: string[];
101
+ }): Promise<Lightningpanda>;
102
+ private updateProfiles;
103
+ /** Declares a kind of page tests ask for by name (`lp.page({ as: name })`). */
104
+ profile(name: string, profile: Profile): Promise<void>;
105
+ /** Releases one name, retiring shared native snapshots only after their last alias. Acquired pages remain independent. */
106
+ releaseProfile(name: string): Promise<void>;
107
+ /** Waits until every connection's pool of profile `name` is fully booted. */
108
+ warm(name: string, timeoutMs?: number): Promise<void>;
109
+ /** The connection with the fewest open pages (round-robin among equals). */
110
+ private pick;
111
+ /**
112
+ * A page of profile `as` (signed in, booted ahead when one is waiting),
113
+ * or a blank page in its own context. `route` (same-document) or `url`
114
+ * (a full navigation) runs as the first step of the first `run`.
115
+ * `initScripts` run in every document of this page before its scripts
116
+ * (after the profile's); a booted page already ran its document, so the
117
+ * page then boots fresh instead of coming from the pool.
118
+ */
119
+ page(opts?: {
120
+ as?: string;
121
+ route?: string;
122
+ url?: string;
123
+ initScripts?: string[];
124
+ }): Promise<Page>;
125
+ private openPage;
126
+ /** The connection URL that forks from `profileId`'s snapshot (fork mode). */
127
+ private forkUrl;
128
+ close(): void;
129
+ }
130
+ export declare class Page {
131
+ private conn;
132
+ readonly sessionId: string;
133
+ readonly targetId: string;
134
+ readonly browserContextId: string;
135
+ /** Steps the next run starts with (the page's route or navigation). */
136
+ prefix: StepLike[];
137
+ private closed;
138
+ constructor(conn: Conn, sessionId: string, targetId: string, browserContextId: string);
139
+ private get cdp();
140
+ /**
141
+ * Runs the steps in the browser as one command; throws on the first failing
142
+ * step (a `ScreenshotMismatchError` for a screenshot that does not match).
143
+ */
144
+ run(steps: StepLike[], opts?: {
145
+ timeout?: number;
146
+ }): Promise<RunResult>;
147
+ /**
148
+ * Expects the page (or `clip`, or `fullPage`) to match the baseline `name`:
149
+ * one `matchScreenshot` step. Throws `ScreenshotMismatchError` on a mismatch.
150
+ */
151
+ matchScreenshot(name: string, opts?: ScreenshotOptions): Promise<ScreenshotResult>;
152
+ /**
153
+ * Runs `source` in every document this page loads from now on, before its
154
+ * scripts (Playwright's `page.addInitScript`): follow it with `goto` or
155
+ * `reload` for the current page to see it.
156
+ */
157
+ addInitScript(source: string): Promise<void>;
158
+ /** Any other CDP command on this page. */
159
+ send<T = any>(method: string, params?: object): Promise<T>;
160
+ /** Closes the page and its context without waiting. */
161
+ close(): void;
162
+ [Symbol.asyncDispose](): Promise<void>;
163
+ }
164
+ /**
165
+ * `expect.extend(screenshotMatchers)`, then
166
+ * `await expect(page).toMatchScreenshot('home', { dir })`.
167
+ */
168
+ export declare const screenshotMatchers: {
169
+ toMatchScreenshot(page: Page, name: string, opts?: ScreenshotOptions): Promise<{
170
+ pass: boolean;
171
+ message: () => string;
172
+ }>;
173
+ };