@kurvball/rehearse 0.0.0-stage → 1.2.1
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/LICENSE +21 -0
- package/README.md +102 -2
- package/dist/affected/affected.d.ts +22 -0
- package/dist/affected/affected.js +162 -0
- package/dist/affected/index.d.ts +1 -0
- package/dist/affected/index.js +1 -0
- package/dist/args.d.ts +12 -0
- package/dist/args.js +42 -0
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +100 -0
- package/dist/commands/check.d.ts +2 -0
- package/dist/commands/check.js +42 -0
- package/dist/commands/clean.d.ts +2 -0
- package/dist/commands/clean.js +37 -0
- package/dist/commands/command.d.ts +29 -0
- package/dist/commands/command.js +29 -0
- package/dist/commands/compare.d.ts +4 -0
- package/dist/commands/compare.js +84 -0
- package/dist/commands/drive.d.ts +2 -0
- package/dist/commands/drive.js +244 -0
- package/dist/commands/index.d.ts +3 -0
- package/dist/commands/index.js +32 -0
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.js +19 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +69 -0
- package/dist/commands/open.d.ts +2 -0
- package/dist/commands/open.js +60 -0
- package/dist/commands/people.d.ts +2 -0
- package/dist/commands/people.js +49 -0
- package/dist/commands/run.d.ts +4 -0
- package/dist/commands/run.js +82 -0
- package/dist/commands/services.d.ts +28 -0
- package/dist/commands/services.js +51 -0
- package/dist/commands/stage.d.ts +7 -0
- package/dist/commands/stage.js +49 -0
- package/dist/commands/up.d.ts +3 -0
- package/dist/commands/up.js +77 -0
- package/dist/commands/validate.d.ts +2 -0
- package/dist/commands/validate.js +35 -0
- package/dist/commands/worlds.d.ts +2 -0
- package/dist/commands/worlds.js +15 -0
- package/dist/compare/announce.d.ts +6 -0
- package/dist/compare/announce.js +27 -0
- package/dist/compare/compare.d.ts +82 -0
- package/dist/compare/compare.js +310 -0
- package/dist/compare/diff.d.ts +15 -0
- package/dist/compare/diff.js +105 -0
- package/dist/compare/index.d.ts +4 -0
- package/dist/compare/index.js +4 -0
- package/dist/compare/setaside.d.ts +29 -0
- package/dist/compare/setaside.js +121 -0
- package/dist/compare/verdict.d.ts +21 -0
- package/dist/compare/verdict.js +30 -0
- package/dist/drive/aria.d.ts +8 -0
- package/dist/drive/aria.js +12 -0
- package/dist/drive/browser.d.ts +34 -0
- package/dist/drive/browser.js +216 -0
- package/dist/drive/daemon.d.ts +1 -0
- package/dist/drive/daemon.js +171 -0
- package/dist/drive/grammar.d.ts +93 -0
- package/dist/drive/grammar.js +303 -0
- package/dist/drive/index.d.ts +4 -0
- package/dist/drive/index.js +4 -0
- package/dist/drive/names.d.ts +24 -0
- package/dist/drive/names.js +97 -0
- package/dist/drive/state.d.ts +13 -0
- package/dist/drive/state.js +28 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +32 -0
- package/dist/exec.d.ts +16 -0
- package/dist/exec.js +43 -0
- package/dist/git/checkout.d.ts +11 -0
- package/dist/git/checkout.js +84 -0
- package/dist/git/git.d.ts +7 -0
- package/dist/git/git.js +27 -0
- package/dist/git/index.d.ts +2 -0
- package/dist/git/index.js +2 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +15 -0
- package/dist/kinds/all.d.ts +4 -0
- package/dist/kinds/all.js +4 -0
- package/dist/kinds/call/check.d.ts +29 -0
- package/dist/kinds/call/check.js +187 -0
- package/dist/kinds/call/definition.d.ts +16 -0
- package/dist/kinds/call/definition.js +33 -0
- package/dist/kinds/call/hurl.d.ts +65 -0
- package/dist/kinds/call/hurl.js +333 -0
- package/dist/kinds/call/kind.d.ts +8 -0
- package/dist/kinds/call/kind.js +122 -0
- package/dist/kinds/call/send.d.ts +32 -0
- package/dist/kinds/call/send.js +152 -0
- package/dist/kinds/call/words.d.ts +22 -0
- package/dist/kinds/call/words.js +24 -0
- package/dist/kinds/file/kind.d.ts +44 -0
- package/dist/kinds/file/kind.js +331 -0
- package/dist/kinds/file/layouts.d.ts +14 -0
- package/dist/kinds/file/layouts.js +103 -0
- package/dist/kinds/flow/kind.d.ts +21 -0
- package/dist/kinds/flow/kind.js +401 -0
- package/dist/kinds/flow/literal.d.ts +8 -0
- package/dist/kinds/flow/literal.js +148 -0
- package/dist/kinds/index.d.ts +10 -0
- package/dist/kinds/index.js +8 -0
- package/dist/kinds/kind.d.ts +57 -0
- package/dist/kinds/kind.js +21 -0
- package/dist/kinds/piece/canvas.d.ts +18 -0
- package/dist/kinds/piece/canvas.js +165 -0
- package/dist/kinds/piece/conventions.d.ts +12 -0
- package/dist/kinds/piece/conventions.js +101 -0
- package/dist/kinds/piece/csf.d.ts +10 -0
- package/dist/kinds/piece/csf.js +304 -0
- package/dist/kinds/piece/kind.d.ts +12 -0
- package/dist/kinds/piece/kind.js +297 -0
- package/dist/kinds/piece/server.d.ts +23 -0
- package/dist/kinds/piece/server.js +236 -0
- package/dist/manifest/fill.d.ts +4 -0
- package/dist/manifest/fill.js +7 -0
- package/dist/manifest/index.d.ts +5 -0
- package/dist/manifest/index.js +5 -0
- package/dist/manifest/init.d.ts +1 -0
- package/dist/manifest/init.js +47 -0
- package/dist/manifest/load.d.ts +7 -0
- package/dist/manifest/load.js +244 -0
- package/dist/manifest/types.d.ts +253 -0
- package/dist/manifest/types.js +7 -0
- package/dist/manifest/yaml.d.ts +5 -0
- package/dist/manifest/yaml.js +47 -0
- package/dist/people/index.d.ts +1 -0
- package/dist/people/index.js +1 -0
- package/dist/people/people.d.ts +39 -0
- package/dist/people/people.js +139 -0
- package/dist/run/cache.d.ts +20 -0
- package/dist/run/cache.js +98 -0
- package/dist/run/index.d.ts +4 -0
- package/dist/run/index.js +3 -0
- package/dist/run/observation.d.ts +27 -0
- package/dist/run/observation.js +1 -0
- package/dist/run/session.d.ts +32 -0
- package/dist/run/session.js +258 -0
- package/dist/run/vars.d.ts +3 -0
- package/dist/run/vars.js +13 -0
- package/dist/scenarios/area.d.ts +6 -0
- package/dist/scenarios/area.js +37 -0
- package/dist/scenarios/files.d.ts +9 -0
- package/dist/scenarios/files.js +26 -0
- package/dist/scenarios/glob.d.ts +6 -0
- package/dist/scenarios/glob.js +106 -0
- package/dist/scenarios/index.d.ts +4 -0
- package/dist/scenarios/index.js +4 -0
- package/dist/scenarios/read.d.ts +18 -0
- package/dist/scenarios/read.js +54 -0
- package/dist/scenarios/scenario.d.ts +34 -0
- package/dist/scenarios/scenario.js +10 -0
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +122 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +2 -0
- package/dist/services/registry.d.ts +23 -0
- package/dist/services/registry.js +49 -0
- package/dist/services/service.d.ts +22 -0
- package/dist/services/service.js +284 -0
- package/dist/stage/api.d.ts +28 -0
- package/dist/stage/api.js +206 -0
- package/dist/stage/client/client/api.js +11 -0
- package/dist/stage/client/client/index.js +1 -0
- package/dist/stage/client/features/canvas/call.js +65 -0
- package/dist/stage/client/features/canvas/context.js +1 -0
- package/dist/stage/client/features/canvas/file.js +36 -0
- package/dist/stage/client/features/canvas/flow.js +59 -0
- package/dist/stage/client/features/canvas/index.js +1 -0
- package/dist/stage/client/features/canvas/observed.js +8 -0
- package/dist/stage/client/features/canvas/parts.js +24 -0
- package/dist/stage/client/features/canvas/piece.js +31 -0
- package/dist/stage/client/features/canvas/reach.js +6 -0
- package/dist/stage/client/features/canvas/views.js +16 -0
- package/dist/stage/client/features/list/index.js +1 -0
- package/dist/stage/client/features/list/list.js +53 -0
- package/dist/stage/client/features/list/reach.js +10 -0
- package/dist/stage/client/features/scenario/heading.js +14 -0
- package/dist/stage/client/features/scenario/index.js +1 -0
- package/dist/stage/client/features/scenario/reach.js +9 -0
- package/dist/stage/client/features/scenario/scenario.js +145 -0
- package/dist/stage/client/features/try/index.js +1 -0
- package/dist/stage/client/features/try/reach.js +12 -0
- package/dist/stage/client/features/try/try.js +65 -0
- package/dist/stage/client/index.html +16 -0
- package/dist/stage/client/main.js +24 -0
- package/dist/stage/client/primitives/busy.js +6 -0
- package/dist/stage/client/primitives/focus.js +21 -0
- package/dist/stage/client/primitives/h.js +25 -0
- package/dist/stage/client/primitives/index.js +6 -0
- package/dist/stage/client/primitives/keyed.js +16 -0
- package/dist/stage/client/primitives/say.js +6 -0
- package/dist/stage/client/primitives/state.js +21 -0
- package/dist/stage/client/primitives/types.js +1 -0
- package/dist/stage/client/primitives/verdicts.js +8 -0
- package/dist/stage/client/stage.css +652 -0
- package/dist/stage/compared.d.ts +20 -0
- package/dist/stage/compared.js +58 -0
- package/dist/stage/files.d.ts +4 -0
- package/dist/stage/files.js +59 -0
- package/dist/stage/guard.d.ts +3 -0
- package/dist/stage/guard.js +20 -0
- package/dist/stage/http.d.ts +12 -0
- package/dist/stage/http.js +34 -0
- package/dist/stage/index.d.ts +1 -0
- package/dist/stage/index.js +1 -0
- package/dist/stage/sending.d.ts +18 -0
- package/dist/stage/sending.js +66 -0
- package/dist/stage/server.d.ts +31 -0
- package/dist/stage/server.js +250 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +5 -0
- package/dist/worlds/define.d.ts +16 -0
- package/dist/worlds/define.js +45 -0
- package/dist/worlds/index.d.ts +2 -0
- package/dist/worlds/index.js +2 -0
- package/dist/worlds/worlds.d.ts +15 -0
- package/dist/worlds/worlds.js +46 -0
- package/llms.txt +46 -0
- package/package.json +84 -3
- package/schema/v1/announce.schema.json +28 -0
- package/schema/v1/check.schema.json +67 -0
- package/schema/v1/clean.schema.json +19 -0
- package/schema/v1/common.schema.json +210 -0
- package/schema/v1/comparison.schema.json +298 -0
- package/schema/v1/dependents.schema.json +30 -0
- package/schema/v1/drive.schema.json +112 -0
- package/schema/v1/error.schema.json +26 -0
- package/schema/v1/file-scenario.schema.json +175 -0
- package/schema/v1/flow.schema.json +216 -0
- package/schema/v1/init.schema.json +20 -0
- package/schema/v1/list.schema.json +70 -0
- package/schema/v1/manifest.schema.json +430 -0
- package/schema/v1/observation.schema.json +388 -0
- package/schema/v1/open.schema.json +14 -0
- package/schema/v1/people.schema.json +88 -0
- package/schema/v1/samples/announce.yaml +5 -0
- package/schema/v1/samples/check.json +13 -0
- package/schema/v1/samples/clean.json +1 -0
- package/schema/v1/samples/comparison.json +83 -0
- package/schema/v1/samples/dependents.json +7 -0
- package/schema/v1/samples/drive.json +10 -0
- package/schema/v1/samples/error.json +10 -0
- package/schema/v1/samples/file-scenario.yaml +13 -0
- package/schema/v1/samples/flow.yaml +13 -0
- package/schema/v1/samples/init.json +1 -0
- package/schema/v1/samples/list.json +24 -0
- package/schema/v1/samples/manifest.yaml +55 -0
- package/schema/v1/samples/observation.json +49 -0
- package/schema/v1/samples/open.json +6 -0
- package/schema/v1/samples/people.json +11 -0
- package/schema/v1/samples/services.json +27 -0
- package/schema/v1/samples/stage.json +6 -0
- package/schema/v1/samples/try.json +13 -0
- package/schema/v1/samples/up.json +16 -0
- package/schema/v1/samples/validate.json +7 -0
- package/schema/v1/samples/worlds.json +21 -0
- package/schema/v1/services.schema.json +72 -0
- package/schema/v1/stage.schema.json +14 -0
- package/schema/v1/try.schema.json +56 -0
- package/schema/v1/up.schema.json +55 -0
- package/schema/v1/validate.schema.json +40 -0
- package/schema/v1/worlds.schema.json +33 -0
- package/skill/rehearse/SKILL.md +148 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Browser, BrowserContext, Page } from 'playwright-core';
|
|
2
|
+
import { type Manifest } from '../manifest/index.ts';
|
|
3
|
+
import { type Command, type DriveResult, type Failure, type Step, type Verb } from './grammar.ts';
|
|
4
|
+
export declare const ACT_MS = 5000;
|
|
5
|
+
export declare const FIXED_TIME: Date;
|
|
6
|
+
export declare function launch(): Promise<Browser>;
|
|
7
|
+
export declare class Stopped extends Error {
|
|
8
|
+
name: string;
|
|
9
|
+
readonly failure: Exclude<Failure, 'usage'>;
|
|
10
|
+
readonly says: string;
|
|
11
|
+
constructor(failure: Exclude<Failure, 'usage'>, says: string);
|
|
12
|
+
}
|
|
13
|
+
export declare const treeOf: (page: Page) => Promise<string>;
|
|
14
|
+
export declare function calm(page: Page): Promise<string>;
|
|
15
|
+
export declare const failed: (command: Verb | null, at: number, failure: Failure, says: string, driver?: string | null) => DriveResult;
|
|
16
|
+
export declare function act(page: Page, command: Command, at?: number): Promise<DriveResult>;
|
|
17
|
+
export interface SignIn {
|
|
18
|
+
headers?: Record<string, string>;
|
|
19
|
+
storage?: Record<string, string>;
|
|
20
|
+
cookies?: {
|
|
21
|
+
name: string;
|
|
22
|
+
value: string;
|
|
23
|
+
path?: string;
|
|
24
|
+
}[];
|
|
25
|
+
}
|
|
26
|
+
export declare function contextFor(browser: Browser, url: string | null, signIn: SignIn | null, colorScheme?: 'light' | 'dark', viewport?: {
|
|
27
|
+
width: number;
|
|
28
|
+
height: number;
|
|
29
|
+
}): Promise<BrowserContext>;
|
|
30
|
+
export declare function errorsOf(page: Page): string[];
|
|
31
|
+
export declare function signInOf(m: Manifest, person: string | null, tokens: Record<string, string>): (SignIn & {
|
|
32
|
+
steps: Step[];
|
|
33
|
+
}) | null;
|
|
34
|
+
export declare function signInBySteps(page: Page, origin: string, steps: readonly Step[]): Promise<string | null>;
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { mkdirSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { RehearseError } from '../errors.js';
|
|
4
|
+
import { fill, fillEnv } from '../manifest/index.js';
|
|
5
|
+
import { FAILURE_WORDS, stepCommand, } from './grammar.js';
|
|
6
|
+
import { ambiguousWords, findNamed, namedIn, notFoundWords, sameWords } from './names.js';
|
|
7
|
+
export const ACT_MS = 5_000;
|
|
8
|
+
const CALM_MS = 120;
|
|
9
|
+
const CALM_LIMIT_MS = 4_000;
|
|
10
|
+
export const FIXED_TIME = new Date('2026-01-15T12:00:00.000Z');
|
|
11
|
+
let loaded = null;
|
|
12
|
+
async function playwright() {
|
|
13
|
+
if (loaded)
|
|
14
|
+
return loaded;
|
|
15
|
+
try {
|
|
16
|
+
loaded = (await import('playwright-core'));
|
|
17
|
+
return loaded;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
throw new RehearseError('no_browser', 'Rehearse drives a browser with playwright-core, which isn’t installed. Install Rehearse with its optional dependencies, then `npx playwright-core install chromium`.');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function launch() {
|
|
24
|
+
const pw = await playwright();
|
|
25
|
+
try {
|
|
26
|
+
return await pw.chromium.launch({ headless: true, args: ['--disable-dev-shm-usage'] });
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
const said = error.message.split('\n')[0] ?? '';
|
|
30
|
+
throw new RehearseError('no_browser', /Executable doesn't exist|browserType.launch/.test(said)
|
|
31
|
+
? `Rehearse found no Chromium for Playwright: run \`npx playwright-core install chromium\`. (${said})`
|
|
32
|
+
: `Chromium didn't start: ${said}`);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class Stopped extends Error {
|
|
36
|
+
name = 'Stopped';
|
|
37
|
+
failure;
|
|
38
|
+
says;
|
|
39
|
+
constructor(failure, says) {
|
|
40
|
+
super(says);
|
|
41
|
+
this.failure = failure;
|
|
42
|
+
this.says = says;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export const treeOf = (page) => page.locator('body').ariaSnapshot({ timeout: ACT_MS });
|
|
46
|
+
export async function calm(page) {
|
|
47
|
+
const begun = Date.now();
|
|
48
|
+
let last = await treeOf(page).catch(() => '');
|
|
49
|
+
while (Date.now() - begun < CALM_LIMIT_MS) {
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, CALM_MS));
|
|
51
|
+
const now = await treeOf(page).catch(() => '');
|
|
52
|
+
if (now === last)
|
|
53
|
+
return now;
|
|
54
|
+
last = now;
|
|
55
|
+
}
|
|
56
|
+
return last;
|
|
57
|
+
}
|
|
58
|
+
async function theOne(page, name, role) {
|
|
59
|
+
const found = findNamed(namedIn(await treeOf(page)), name, role);
|
|
60
|
+
if (found.found === 'none')
|
|
61
|
+
throw new Stopped('not_found', notFoundWords(name, role, found.near));
|
|
62
|
+
if (found.found === 'many')
|
|
63
|
+
throw new Stopped('ambiguous', ambiguousWords(name, found.many));
|
|
64
|
+
const { one } = found;
|
|
65
|
+
const at = page.getByRole(one.role, { name: one.raw, exact: true });
|
|
66
|
+
const count = await at.count();
|
|
67
|
+
if (count === 1)
|
|
68
|
+
return { named: one, at };
|
|
69
|
+
if (count === 0)
|
|
70
|
+
throw new Stopped('not_found', notFoundWords(name, role, []));
|
|
71
|
+
const lines = await Promise.all((await at.all()).map(async (each) => (await each.ariaSnapshot()).split('\n')[0].trim()));
|
|
72
|
+
throw new Stopped('ambiguous', sameWords(one, lines));
|
|
73
|
+
}
|
|
74
|
+
const timedOut = (error) => error instanceof Error && error.name === 'TimeoutError';
|
|
75
|
+
const done = (command, at, extra = {}) => ({
|
|
76
|
+
v: 1,
|
|
77
|
+
ok: true,
|
|
78
|
+
command,
|
|
79
|
+
ms: Date.now() - at,
|
|
80
|
+
file: extra.file ?? null,
|
|
81
|
+
tree: extra.tree ?? null,
|
|
82
|
+
driver: 'browser',
|
|
83
|
+
});
|
|
84
|
+
export const failed = (command, at, failure, says, driver = 'browser') => ({
|
|
85
|
+
v: 1,
|
|
86
|
+
ok: false,
|
|
87
|
+
command,
|
|
88
|
+
ms: Date.now() - at,
|
|
89
|
+
failure,
|
|
90
|
+
says,
|
|
91
|
+
driver,
|
|
92
|
+
});
|
|
93
|
+
export async function act(page, command, at = Date.now()) {
|
|
94
|
+
try {
|
|
95
|
+
switch (command.command) {
|
|
96
|
+
case 'click': {
|
|
97
|
+
const { named, at: target } = await theOne(page, command.name, command.role);
|
|
98
|
+
try {
|
|
99
|
+
await target.click({ timeout: ACT_MS });
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
if (timedOut(error))
|
|
103
|
+
throw new Stopped('timed_out', FAILURE_WORDS.timed_out(named.name));
|
|
104
|
+
throw error;
|
|
105
|
+
}
|
|
106
|
+
await calm(page);
|
|
107
|
+
return done('click', at);
|
|
108
|
+
}
|
|
109
|
+
case 'fill': {
|
|
110
|
+
const { named, at: target } = await theOne(page, command.name, command.role);
|
|
111
|
+
try {
|
|
112
|
+
await target.fill(command.text, { timeout: ACT_MS });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (timedOut(error))
|
|
116
|
+
throw new Stopped('timed_out', FAILURE_WORDS.timed_out(named.name));
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
await calm(page);
|
|
120
|
+
return done('fill', at);
|
|
121
|
+
}
|
|
122
|
+
case 'type':
|
|
123
|
+
await page.keyboard.type(command.text);
|
|
124
|
+
await calm(page);
|
|
125
|
+
return done('type', at);
|
|
126
|
+
case 'key':
|
|
127
|
+
await page.keyboard.press(command.key);
|
|
128
|
+
await calm(page);
|
|
129
|
+
return done('key', at);
|
|
130
|
+
case 'snapshot': {
|
|
131
|
+
if (command.name === null)
|
|
132
|
+
return done('snapshot', at, { tree: await calm(page) });
|
|
133
|
+
await calm(page);
|
|
134
|
+
const { at: target } = await theOne(page, command.name, command.role);
|
|
135
|
+
return done('snapshot', at, { tree: await target.ariaSnapshot({ timeout: ACT_MS }) });
|
|
136
|
+
}
|
|
137
|
+
case 'screenshot': {
|
|
138
|
+
mkdirSync(path.dirname(path.resolve(command.file)), { recursive: true });
|
|
139
|
+
await page.screenshot({ path: command.file });
|
|
140
|
+
return done('screenshot', at, { file: command.file });
|
|
141
|
+
}
|
|
142
|
+
case 'record':
|
|
143
|
+
return failed('record', at, 'usage', 'Rehearse’s browser doesn’t record video yet: take screenshots instead.');
|
|
144
|
+
case 'start':
|
|
145
|
+
case 'stop':
|
|
146
|
+
return done(command.command, at);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (error instanceof Stopped)
|
|
151
|
+
return failed(command.command, at, error.failure, error.says);
|
|
152
|
+
if (timedOut(error))
|
|
153
|
+
return failed(command.command, at, 'timed_out', FAILURE_WORDS.timed_out(null));
|
|
154
|
+
return failed(command.command, at, 'crashed', `${FAILURE_WORDS.crashed()} ${error.message.split('\n')[0]}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export async function contextFor(browser, url, signIn, colorScheme = 'light', viewport = { width: 1280, height: 800 }) {
|
|
158
|
+
const context = await browser.newContext({
|
|
159
|
+
viewport,
|
|
160
|
+
colorScheme,
|
|
161
|
+
reducedMotion: 'reduce',
|
|
162
|
+
...(signIn?.headers && { extraHTTPHeaders: signIn.headers }),
|
|
163
|
+
});
|
|
164
|
+
if (signIn?.cookies?.length && url)
|
|
165
|
+
await context.addCookies(signIn.cookies.map((c) => ({
|
|
166
|
+
...c,
|
|
167
|
+
url: new URL(url).origin,
|
|
168
|
+
...(c.path && { url: undefined, domain: new URL(url).hostname, path: c.path }),
|
|
169
|
+
})));
|
|
170
|
+
if (signIn?.storage && Object.keys(signIn.storage).length)
|
|
171
|
+
await context.addInitScript((values) => {
|
|
172
|
+
for (const [k, v] of Object.entries(values))
|
|
173
|
+
window.localStorage.setItem(k, v);
|
|
174
|
+
}, signIn.storage);
|
|
175
|
+
return context;
|
|
176
|
+
}
|
|
177
|
+
export function errorsOf(page) {
|
|
178
|
+
const errors = [];
|
|
179
|
+
page.on('console', (m) => {
|
|
180
|
+
if (m.type() === 'error' && !/favicon\.ico/i.test(m.text()))
|
|
181
|
+
errors.push(m.text().split('\n')[0].slice(0, 500));
|
|
182
|
+
});
|
|
183
|
+
page.on('pageerror', (e) => errors.push(`${e.name}: ${e.message}`.split('\n')[0].slice(0, 500)));
|
|
184
|
+
return errors;
|
|
185
|
+
}
|
|
186
|
+
export function signInOf(m, person, tokens) {
|
|
187
|
+
if (!person)
|
|
188
|
+
return null;
|
|
189
|
+
const p = m.people[person];
|
|
190
|
+
if (!p?.browser)
|
|
191
|
+
return null;
|
|
192
|
+
const vars = { token: tokens[person] ?? '', person };
|
|
193
|
+
return {
|
|
194
|
+
...(p.browser.headers && { headers: fillEnv(p.browser.headers, vars) }),
|
|
195
|
+
...(p.browser.storage && { storage: fillEnv(p.browser.storage, vars) }),
|
|
196
|
+
...(p.browser.cookies && {
|
|
197
|
+
cookies: p.browser.cookies.map((c) => ({ ...c, value: fill(c.value, vars) })),
|
|
198
|
+
}),
|
|
199
|
+
steps: (p.browser.steps ?? []).map((st) => ({
|
|
200
|
+
...st,
|
|
201
|
+
...(st.with !== undefined && { with: fill(st.with, vars) }),
|
|
202
|
+
})),
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
export async function signInBySteps(page, origin, steps) {
|
|
206
|
+
if (steps.length === 0)
|
|
207
|
+
return null;
|
|
208
|
+
await page.goto(origin, { waitUntil: 'load', timeout: 30_000 });
|
|
209
|
+
await calm(page);
|
|
210
|
+
for (const [i, step] of steps.entries()) {
|
|
211
|
+
const did = await act(page, stepCommand(step));
|
|
212
|
+
if (!did.ok)
|
|
213
|
+
return `Signing in stopped at its step ${i + 1}: ${did.says}`;
|
|
214
|
+
}
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { createServer } from 'node:http';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { parseArgs } from 'node:util';
|
|
5
|
+
import { errorText } from '../errors.js';
|
|
6
|
+
import { fill, loadManifest } from '../manifest/index.js';
|
|
7
|
+
import { scrub, scrubDeep, signIn } from '../people/index.js';
|
|
8
|
+
import { startPieceServer } from '../kinds/index.js';
|
|
9
|
+
import { applyWorld, baseVars } from '../run/index.js';
|
|
10
|
+
import { runningServices, bringDown, bringUp, freePort } from '../services/index.js';
|
|
11
|
+
import { act, calm, contextFor, errorsOf, failed, FIXED_TIME, launch, signInBySteps, signInOf, } from './browser.js';
|
|
12
|
+
import { driveArgv, parseDrive } from './grammar.js';
|
|
13
|
+
import { forgetDriving, writeDriving } from './state.js';
|
|
14
|
+
import { readScenarios } from '../scenarios/index.js';
|
|
15
|
+
const { values } = parseArgs({
|
|
16
|
+
options: {
|
|
17
|
+
manifest: { type: 'string' },
|
|
18
|
+
on: { type: 'string' },
|
|
19
|
+
service: { type: 'string' },
|
|
20
|
+
piece: { type: 'string' },
|
|
21
|
+
world: { type: 'string' },
|
|
22
|
+
as: { type: 'string' },
|
|
23
|
+
start: { type: 'string' },
|
|
24
|
+
scheme: { type: 'string' },
|
|
25
|
+
},
|
|
26
|
+
strict: true,
|
|
27
|
+
});
|
|
28
|
+
const m = loadManifest(values.manifest);
|
|
29
|
+
const checkout = values.on;
|
|
30
|
+
const world = values.world ?? m.defaults.world;
|
|
31
|
+
const started = [];
|
|
32
|
+
let browser = null;
|
|
33
|
+
let context = null;
|
|
34
|
+
let pieces = null;
|
|
35
|
+
let page;
|
|
36
|
+
let errors = [];
|
|
37
|
+
let tokens = {};
|
|
38
|
+
const secret = randomBytes(24).toString('hex');
|
|
39
|
+
async function open() {
|
|
40
|
+
let url;
|
|
41
|
+
if (values.piece) {
|
|
42
|
+
const piece = (await readScenarios(m, checkout, ['piece'])).scenarios.find((p) => p.id === values.piece);
|
|
43
|
+
const spec = piece?.spec;
|
|
44
|
+
if (!piece || !spec?.packageDir)
|
|
45
|
+
throw new Error(`There is no piece ${values.piece} in ${checkout}.`);
|
|
46
|
+
pieces = await startPieceServer({
|
|
47
|
+
dir: spec.packageDir,
|
|
48
|
+
preview: m.scenarios.pieces.preview ?? null,
|
|
49
|
+
styles: m.scenarios.pieces.styles ?? null,
|
|
50
|
+
config: m.scenarios.pieces.config ?? null,
|
|
51
|
+
pieces: [{ file: spec.inPackage, exportName: spec.exportName }],
|
|
52
|
+
});
|
|
53
|
+
url = pieces.canvas(spec.inPackage, spec.exportName);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const name = values.service ?? m.scenarios.flows.service ?? m.defaults.service;
|
|
57
|
+
const svc = name ? m.services[name] : undefined;
|
|
58
|
+
if (!svc)
|
|
59
|
+
throw new Error(`There is no service to drive: name one with --service.`);
|
|
60
|
+
let running = runningServices(m.dir).filter((r) => path.resolve(r.checkout) === path.resolve(checkout));
|
|
61
|
+
let mine = running.find((r) => r.name === svc.name);
|
|
62
|
+
if (!mine) {
|
|
63
|
+
if (world)
|
|
64
|
+
await applyWorld(m, world, 'before', checkout, baseVars(m, checkout, running));
|
|
65
|
+
mine = await bringUp(m, svc, checkout, {
|
|
66
|
+
world,
|
|
67
|
+
real: new Set(),
|
|
68
|
+
vars: baseVars(m, checkout, running),
|
|
69
|
+
});
|
|
70
|
+
started.push(mine);
|
|
71
|
+
running = [...running, mine];
|
|
72
|
+
if (world)
|
|
73
|
+
await applyWorld(m, world, 'add', checkout, baseVars(m, checkout, running));
|
|
74
|
+
}
|
|
75
|
+
if (!mine.url)
|
|
76
|
+
throw new Error(`${svc.name} has no URL to open.`);
|
|
77
|
+
url = new URL(values.start ?? '/', mine.url).href;
|
|
78
|
+
if (m.signIn || Object.values(m.people).some((p) => p.token)) {
|
|
79
|
+
const v = baseVars(m, checkout, running);
|
|
80
|
+
tokens = (await signIn(m, { cwd: checkout, env: {}, fill: (t) => fill(t, v) })).tokens;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const person = values.as ?? m.defaults.person;
|
|
84
|
+
const signedIn = signInOf(m, person, tokens);
|
|
85
|
+
browser = await launch();
|
|
86
|
+
context = await contextFor(browser, url, signedIn, values.scheme === 'dark' ? 'dark' : 'light');
|
|
87
|
+
page = await context.newPage();
|
|
88
|
+
await page.clock.setFixedTime(FIXED_TIME);
|
|
89
|
+
errors = errorsOf(page);
|
|
90
|
+
if (signedIn?.steps.length && !values.piece) {
|
|
91
|
+
const stopped = await signInBySteps(page, new URL(url).origin, signedIn.steps);
|
|
92
|
+
if (stopped)
|
|
93
|
+
throw new Error(scrub(stopped, tokens));
|
|
94
|
+
}
|
|
95
|
+
await page.goto(url, { waitUntil: 'load', timeout: 30_000 });
|
|
96
|
+
await calm(page);
|
|
97
|
+
}
|
|
98
|
+
async function stop() {
|
|
99
|
+
await context?.close().catch(() => undefined);
|
|
100
|
+
await browser?.close().catch(() => undefined);
|
|
101
|
+
await pieces?.close().catch(() => undefined);
|
|
102
|
+
for (const r of started.toReversed())
|
|
103
|
+
await bringDown(m, r).catch(() => undefined);
|
|
104
|
+
forgetDriving(m.dir);
|
|
105
|
+
}
|
|
106
|
+
function onlyWhatTheGrammarAllows(given) {
|
|
107
|
+
const readAgainAsWords = parseDrive(driveArgv(given));
|
|
108
|
+
if (!readAgainAsWords.ok)
|
|
109
|
+
throw new Error(readAgainAsWords.says);
|
|
110
|
+
return readAgainAsWords.command;
|
|
111
|
+
}
|
|
112
|
+
async function main() {
|
|
113
|
+
try {
|
|
114
|
+
await open();
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
process.stderr.write(`${errorText(error)}\n`);
|
|
118
|
+
await stop();
|
|
119
|
+
process.exit(1);
|
|
120
|
+
}
|
|
121
|
+
const port = await freePort();
|
|
122
|
+
const server = createServer((req, res) => {
|
|
123
|
+
let body = '';
|
|
124
|
+
req.on('data', (c) => (body += c.toString('utf8')));
|
|
125
|
+
req.on('end', () => {
|
|
126
|
+
void (async () => {
|
|
127
|
+
const at = Date.now();
|
|
128
|
+
if (req.headers['x-rehearse-drive'] !== secret) {
|
|
129
|
+
res.writeHead(403).end();
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
let command;
|
|
133
|
+
try {
|
|
134
|
+
command = onlyWhatTheGrammarAllows(JSON.parse(body));
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
res.writeHead(200, { 'content-type': 'application/json' });
|
|
138
|
+
res.end(JSON.stringify(failed(null, at, 'usage', errorText(error))));
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
const seen = errors.length;
|
|
142
|
+
const result = command.command === 'stop'
|
|
143
|
+
? { v: 1, ok: true, command: 'stop', ms: 0, file: null, tree: null, driver: 'browser' }
|
|
144
|
+
: await act(page, command, at).catch((e) => failed(command.command, at, 'crashed', errorText(e)));
|
|
145
|
+
const fresh = errors.slice(seen);
|
|
146
|
+
const answer = scrubDeep(result.ok && fresh.length ? { ...result, errors: fresh } : result, tokens);
|
|
147
|
+
res.writeHead(200, { 'content-type': 'application/json' });
|
|
148
|
+
res.end(JSON.stringify(answer), () => {
|
|
149
|
+
if (command.command === 'stop')
|
|
150
|
+
void stop().finally(() => process.exit(0));
|
|
151
|
+
});
|
|
152
|
+
})();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
server.listen(port, '127.0.0.1', () => {
|
|
156
|
+
writeDriving(m.dir, {
|
|
157
|
+
driver: 'browser',
|
|
158
|
+
service: values.service ?? null,
|
|
159
|
+
checkout,
|
|
160
|
+
port,
|
|
161
|
+
pid: process.pid,
|
|
162
|
+
secret,
|
|
163
|
+
since: new Date().toISOString(),
|
|
164
|
+
});
|
|
165
|
+
process.stdout.write(`ready ${port}\n`);
|
|
166
|
+
});
|
|
167
|
+
const end = () => void stop().finally(() => process.exit(0));
|
|
168
|
+
process.once('SIGTERM', end);
|
|
169
|
+
process.once('SIGINT', end);
|
|
170
|
+
}
|
|
171
|
+
void main();
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export declare const ROLES: readonly ['alert', 'alertdialog', 'application', 'article', 'banner', 'blockquote', 'button', 'caption', 'cell', 'checkbox', 'code', 'columnheader', 'combobox', 'complementary', 'contentinfo', 'definition', 'deletion', 'dialog', 'directory', 'document', 'emphasis', 'feed', 'figure', 'form', 'generic', 'grid', 'gridcell', 'group', 'heading', 'img', 'insertion', 'link', 'list', 'listbox', 'listitem', 'log', 'main', 'marquee', 'math', 'meter', 'menu', 'menubar', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'navigation', 'none', 'note', 'option', 'paragraph', 'presentation', 'progressbar', 'radio', 'radiogroup', 'region', 'row', 'rowgroup', 'rowheader', 'scrollbar', 'search', 'searchbox', 'separator', 'slider', 'spinbutton', 'status', 'strong', 'subscript', 'superscript', 'switch', 'tab', 'table', 'tablist', 'tabpanel', 'term', 'textbox', 'time', 'timer', 'toolbar', 'tooltip', 'tree', 'treegrid', 'treeitem'];
|
|
2
|
+
export type Role = (typeof ROLES)[number];
|
|
3
|
+
export declare const isRole: (v: unknown) => v is Role;
|
|
4
|
+
export declare const isKey: (v: unknown) => v is string;
|
|
5
|
+
export interface Step {
|
|
6
|
+
click?: string;
|
|
7
|
+
type?: string;
|
|
8
|
+
key?: string;
|
|
9
|
+
snapshot?: string;
|
|
10
|
+
fill?: string;
|
|
11
|
+
with?: string;
|
|
12
|
+
role?: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const VERBS: readonly ['start', 'click', 'type', 'key', 'fill', 'snapshot', 'screenshot', 'record', 'stop'];
|
|
15
|
+
export type Verb = (typeof VERBS)[number];
|
|
16
|
+
export type Command = {
|
|
17
|
+
command: 'start';
|
|
18
|
+
} | {
|
|
19
|
+
command: 'click';
|
|
20
|
+
name: string;
|
|
21
|
+
role: Role | null;
|
|
22
|
+
} | {
|
|
23
|
+
command: 'type';
|
|
24
|
+
text: string;
|
|
25
|
+
} | {
|
|
26
|
+
command: 'key';
|
|
27
|
+
key: string;
|
|
28
|
+
} | {
|
|
29
|
+
command: 'fill';
|
|
30
|
+
name: string;
|
|
31
|
+
role: Role | null;
|
|
32
|
+
text: string;
|
|
33
|
+
} | {
|
|
34
|
+
command: 'snapshot';
|
|
35
|
+
name: string | null;
|
|
36
|
+
role: Role | null;
|
|
37
|
+
} | {
|
|
38
|
+
command: 'screenshot';
|
|
39
|
+
file: string;
|
|
40
|
+
} | {
|
|
41
|
+
command: 'record';
|
|
42
|
+
file: string;
|
|
43
|
+
} | {
|
|
44
|
+
command: 'stop';
|
|
45
|
+
};
|
|
46
|
+
export type Failure = 'usage' | 'not_found' | 'ambiguous' | 'not_running' | 'timed_out' | 'crashed';
|
|
47
|
+
export type DriveResult = {
|
|
48
|
+
v: 1;
|
|
49
|
+
ok: true;
|
|
50
|
+
command: Verb;
|
|
51
|
+
ms: number;
|
|
52
|
+
file: string | null;
|
|
53
|
+
tree: string | null;
|
|
54
|
+
driver: string | null;
|
|
55
|
+
errors?: string[];
|
|
56
|
+
said?: string | null;
|
|
57
|
+
} | {
|
|
58
|
+
v: 1;
|
|
59
|
+
ok: false;
|
|
60
|
+
command: Verb | null;
|
|
61
|
+
ms: number;
|
|
62
|
+
failure: Failure;
|
|
63
|
+
says: string;
|
|
64
|
+
driver: string | null;
|
|
65
|
+
};
|
|
66
|
+
export declare const FAILURE_WORDS: {
|
|
67
|
+
not_found: (name: string | null) => string;
|
|
68
|
+
ambiguous: (name: string | null) => string;
|
|
69
|
+
not_running: () => string;
|
|
70
|
+
timed_out: (name: string | null) => string;
|
|
71
|
+
crashed: () => string;
|
|
72
|
+
};
|
|
73
|
+
export declare const EXIT: {
|
|
74
|
+
readonly done: 0;
|
|
75
|
+
readonly failed: 1;
|
|
76
|
+
readonly usage: 2;
|
|
77
|
+
};
|
|
78
|
+
export declare const exitOf: (r: DriveResult) => number;
|
|
79
|
+
export declare function stepCommand(step: Step): Command;
|
|
80
|
+
export declare function stepWords(step: Step): string;
|
|
81
|
+
export declare function stepOf(v: unknown, n: number): Step;
|
|
82
|
+
export type Parsed = {
|
|
83
|
+
ok: true;
|
|
84
|
+
command: Command;
|
|
85
|
+
json: boolean;
|
|
86
|
+
options: Record<string, string>;
|
|
87
|
+
} | {
|
|
88
|
+
ok: false;
|
|
89
|
+
says: string;
|
|
90
|
+
};
|
|
91
|
+
export declare function parseDrive(argv: readonly string[]): Parsed;
|
|
92
|
+
export declare function driveArgv(c: Command): string[];
|
|
93
|
+
export declare function resultText(r: DriveResult): string;
|