@richjava/req0 0.1.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/LICENSE +21 -0
- package/README.md +117 -0
- package/cockpit/android-chrome-192x192.png +0 -0
- package/cockpit/android-chrome-512x512.png +0 -0
- package/cockpit/app.js +1710 -0
- package/cockpit/apple-touch-icon.png +0 -0
- package/cockpit/favicon-16x16.png +0 -0
- package/cockpit/favicon-32x32.png +0 -0
- package/cockpit/favicon.ico +0 -0
- package/cockpit/index.html +180 -0
- package/cockpit/markdown-editor.js +93 -0
- package/cockpit/req0-logo-horiz.png +0 -0
- package/cockpit/req0-logo-stacked.png +0 -0
- package/cockpit/site.webmanifest +19 -0
- package/cockpit/styles.css +1710 -0
- package/cockpit/tokens.css +118 -0
- package/dist/adapter.d.ts +5 -0
- package/dist/adapter.js +16 -0
- package/dist/browser.d.ts +33 -0
- package/dist/browser.js +524 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +265 -0
- package/dist/compile.d.ts +5 -0
- package/dist/compile.js +456 -0
- package/dist/diff.d.ts +15 -0
- package/dist/diff.js +114 -0
- package/dist/fix-from-proof.d.ts +13 -0
- package/dist/fix-from-proof.js +61 -0
- package/dist/gates.d.ts +7 -0
- package/dist/gates.js +7 -0
- package/dist/implement-brief.d.ts +9 -0
- package/dist/implement-brief.js +142 -0
- package/dist/implement.d.ts +70 -0
- package/dist/implement.js +615 -0
- package/dist/improve-log.d.ts +11 -0
- package/dist/improve-log.js +38 -0
- package/dist/improve-review.d.ts +45 -0
- package/dist/improve-review.js +182 -0
- package/dist/improve.d.ts +16 -0
- package/dist/improve.js +127 -0
- package/dist/jev-pack.d.ts +2 -0
- package/dist/jev-pack.js +114 -0
- package/dist/jev.d.ts +23 -0
- package/dist/jev.js +142 -0
- package/dist/pack.d.ts +101 -0
- package/dist/pack.js +609 -0
- package/dist/personas.d.ts +13 -0
- package/dist/personas.js +88 -0
- package/dist/proof-report.d.ts +8 -0
- package/dist/proof-report.js +82 -0
- package/dist/proof.d.ts +45 -0
- package/dist/proof.js +394 -0
- package/dist/qa-plan.d.ts +6 -0
- package/dist/qa-plan.js +58 -0
- package/dist/ready.d.ts +29 -0
- package/dist/ready.js +317 -0
- package/dist/runtime.d.ts +25 -0
- package/dist/runtime.js +125 -0
- package/dist/server.d.ts +15 -0
- package/dist/server.js +588 -0
- package/dist/stack.d.ts +35 -0
- package/dist/stack.js +220 -0
- package/dist/stages.d.ts +15 -0
- package/dist/stages.js +345 -0
- package/dist/template.d.ts +5 -0
- package/dist/template.js +55 -0
- package/dist/types.d.ts +282 -0
- package/dist/types.js +12 -0
- package/docs/requirement-template.md +341 -0
- package/package.json +81 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/* Theme-independent scale */
|
|
2
|
+
:root {
|
|
3
|
+
--font-ui: Inter, "Segoe UI", system-ui, sans-serif;
|
|
4
|
+
--font-mono: ui-monospace, "SF Mono", Menlo, monospace;
|
|
5
|
+
|
|
6
|
+
--text-12: 12px;
|
|
7
|
+
--text-13: 13px;
|
|
8
|
+
--text-14: 14px;
|
|
9
|
+
--text-16: 16px;
|
|
10
|
+
--text-20: 20px;
|
|
11
|
+
--text-24: 24px;
|
|
12
|
+
|
|
13
|
+
--space-1: 4px;
|
|
14
|
+
--space-2: 8px;
|
|
15
|
+
--space-3: 12px;
|
|
16
|
+
--space-4: 16px;
|
|
17
|
+
--space-5: 20px;
|
|
18
|
+
--space-6: 24px;
|
|
19
|
+
|
|
20
|
+
--radius-sm: 8px;
|
|
21
|
+
--radius-md: 12px;
|
|
22
|
+
--radius-pill: 9999px;
|
|
23
|
+
|
|
24
|
+
--sidebar-width: 248px;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/*
|
|
28
|
+
Surfaces (lowest → highest):
|
|
29
|
+
0 page canvas
|
|
30
|
+
1 chrome (sidebar, bars)
|
|
31
|
+
2 raised panels
|
|
32
|
+
3 inset fields / nested rows
|
|
33
|
+
Components use these names only. Hex lives per theme.
|
|
34
|
+
*/
|
|
35
|
+
|
|
36
|
+
:root,
|
|
37
|
+
[data-theme="light"] {
|
|
38
|
+
color-scheme: light;
|
|
39
|
+
|
|
40
|
+
--color-primary: #7b68ee;
|
|
41
|
+
--color-primary-hover: #6647f0;
|
|
42
|
+
--color-on-primary: #ffffff;
|
|
43
|
+
--color-focus-ring: rgba(123, 104, 238, 0.35);
|
|
44
|
+
--surface-selected: #eeeaff;
|
|
45
|
+
|
|
46
|
+
--surface-0: #f4f5f7;
|
|
47
|
+
--surface-1: #ffffff;
|
|
48
|
+
--surface-2: #ffffff;
|
|
49
|
+
--surface-3: #f7f8fa;
|
|
50
|
+
--surface-hover: #f0f1f4;
|
|
51
|
+
--surface-disabled: #d5d8de;
|
|
52
|
+
|
|
53
|
+
--text-primary: #202020;
|
|
54
|
+
--text-secondary: #646464;
|
|
55
|
+
--text-muted: #838383;
|
|
56
|
+
--text-on-disabled: #ffffff;
|
|
57
|
+
|
|
58
|
+
--border-subtle: #e6e8ec;
|
|
59
|
+
--border-strong: #d0d4db;
|
|
60
|
+
|
|
61
|
+
--shadow-1: none;
|
|
62
|
+
--shadow-2: 0 1px 2px rgba(32, 32, 32, 0.06), 0 8px 24px rgba(32, 32, 32, 0.04);
|
|
63
|
+
--shadow-3: 0 8px 28px rgba(32, 32, 32, 0.12);
|
|
64
|
+
|
|
65
|
+
--color-status-idle: #6b7280;
|
|
66
|
+
--color-status-draft: #7b68ee;
|
|
67
|
+
--color-status-ok: #078d3b;
|
|
68
|
+
--color-status-bad: #c56f63;
|
|
69
|
+
--color-status-progress: #7b68ee;
|
|
70
|
+
|
|
71
|
+
--color-status-idle-bg: #eef0f2;
|
|
72
|
+
--color-status-draft-bg: #eeeaff;
|
|
73
|
+
--color-status-ok-bg: #e6f6ec;
|
|
74
|
+
--color-status-bad-bg: #f5ecea;
|
|
75
|
+
--color-status-progress-bg: #eeeaff;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/* Reserved. Do not wire a toggle. Flip html[data-theme] later. */
|
|
79
|
+
[data-theme="dark"] {
|
|
80
|
+
color-scheme: dark;
|
|
81
|
+
|
|
82
|
+
--color-primary: #8b7cf2;
|
|
83
|
+
--color-primary-hover: #9d90f5;
|
|
84
|
+
--color-on-primary: #ffffff;
|
|
85
|
+
--color-focus-ring: rgba(139, 124, 242, 0.45);
|
|
86
|
+
--surface-selected: #3a3560;
|
|
87
|
+
|
|
88
|
+
--surface-0: #16181d;
|
|
89
|
+
--surface-1: #1e2128;
|
|
90
|
+
--surface-2: #262a33;
|
|
91
|
+
--surface-3: #2e333d;
|
|
92
|
+
--surface-hover: #323844;
|
|
93
|
+
--surface-disabled: #3d4450;
|
|
94
|
+
|
|
95
|
+
--text-primary: #f2f3f5;
|
|
96
|
+
--text-secondary: #b4b8c0;
|
|
97
|
+
--text-muted: #8b909a;
|
|
98
|
+
--text-on-disabled: #9aa0aa;
|
|
99
|
+
|
|
100
|
+
--border-subtle: #323844;
|
|
101
|
+
--border-strong: #3f4654;
|
|
102
|
+
|
|
103
|
+
--shadow-1: none;
|
|
104
|
+
--shadow-2: 0 1px 2px rgba(0, 0, 0, 0.35);
|
|
105
|
+
--shadow-3: 0 12px 32px rgba(0, 0, 0, 0.45);
|
|
106
|
+
|
|
107
|
+
--color-status-idle: #9aa0aa;
|
|
108
|
+
--color-status-draft: #8b7cf2;
|
|
109
|
+
--color-status-ok: #3dce74;
|
|
110
|
+
--color-status-bad: #d98980;
|
|
111
|
+
--color-status-progress: #8b7cf2;
|
|
112
|
+
|
|
113
|
+
--color-status-idle-bg: #2a2e37;
|
|
114
|
+
--color-status-draft-bg: #2e2a4a;
|
|
115
|
+
--color-status-ok-bg: #1c3d2a;
|
|
116
|
+
--color-status-bad-bg: #3d2220;
|
|
117
|
+
--color-status-progress-bg: #2e2a4a;
|
|
118
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AdapterId } from "./types.js";
|
|
2
|
+
export declare const ADAPTER_IDS: readonly ["cursor", "copilot", "manual"];
|
|
3
|
+
export declare function parseAdapterId(raw: unknown): AdapterId | null;
|
|
4
|
+
export declare function adapterDisplayName(adapter: AdapterId): string;
|
|
5
|
+
export declare function adapterLaunchesAgent(adapter: AdapterId): boolean;
|
package/dist/adapter.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const ADAPTER_IDS = ["cursor", "copilot", "manual"];
|
|
2
|
+
export function parseAdapterId(raw) {
|
|
3
|
+
if (raw === "cursor" || raw === "copilot" || raw === "manual")
|
|
4
|
+
return raw;
|
|
5
|
+
return null;
|
|
6
|
+
}
|
|
7
|
+
export function adapterDisplayName(adapter) {
|
|
8
|
+
if (adapter === "copilot")
|
|
9
|
+
return "Copilot";
|
|
10
|
+
if (adapter === "manual")
|
|
11
|
+
return "Manual";
|
|
12
|
+
return "Cursor";
|
|
13
|
+
}
|
|
14
|
+
export function adapterLaunchesAgent(adapter) {
|
|
15
|
+
return adapter === "cursor" || adapter === "copilot";
|
|
16
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Persona } from "./personas.js";
|
|
2
|
+
import type { QaCase } from "./qa-plan.js";
|
|
3
|
+
import type { RuntimeFixture } from "./runtime.js";
|
|
4
|
+
import type { ProgressFn } from "./types.js";
|
|
5
|
+
export type CaseObservation = {
|
|
6
|
+
url: string;
|
|
7
|
+
text: string;
|
|
8
|
+
options: string[];
|
|
9
|
+
controlAvailable: boolean;
|
|
10
|
+
controlDisabled: boolean;
|
|
11
|
+
clicked: boolean;
|
|
12
|
+
error?: string;
|
|
13
|
+
};
|
|
14
|
+
export type ProofDriver = {
|
|
15
|
+
runCase(input: {
|
|
16
|
+
runtime: RuntimeFixture;
|
|
17
|
+
persona: Persona;
|
|
18
|
+
qa: QaCase;
|
|
19
|
+
}): Promise<CaseObservation>;
|
|
20
|
+
close?(): Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
export declare function createPlaywrightDriver(onProgress?: ProgressFn): Promise<ProofDriver>;
|
|
23
|
+
export declare function controlPendingName(control: string | null): string | null;
|
|
24
|
+
export declare function selectWantedLabel(step: string): string;
|
|
25
|
+
export declare function optionMatches(optionLabel: string, wanted: string): boolean;
|
|
26
|
+
export declare function pickOpenTarget(candidates: {
|
|
27
|
+
index: number;
|
|
28
|
+
name: string;
|
|
29
|
+
}[], step: string, preconditions: string): {
|
|
30
|
+
index: number;
|
|
31
|
+
name: string;
|
|
32
|
+
} | undefined;
|
|
33
|
+
export declare function shortError(err: unknown): string;
|
package/dist/browser.js
ADDED
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import { access } from "node:fs/promises";
|
|
2
|
+
export async function createPlaywrightDriver(onProgress) {
|
|
3
|
+
let playwright;
|
|
4
|
+
try {
|
|
5
|
+
playwright = await import("playwright");
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
8
|
+
throw new Error('Playwright is not installed. Run "npx playwright install chromium" (Req0 depends on the playwright package).');
|
|
9
|
+
}
|
|
10
|
+
const browser = await launchChromium(playwright, onProgress);
|
|
11
|
+
return {
|
|
12
|
+
async close() {
|
|
13
|
+
await browser.close();
|
|
14
|
+
},
|
|
15
|
+
async runCase({ runtime, persona, qa }) {
|
|
16
|
+
const context = await browser.newContext();
|
|
17
|
+
const page = await context.newPage();
|
|
18
|
+
page.setDefaultTimeout(15_000);
|
|
19
|
+
try {
|
|
20
|
+
const loginUrl = new URL(runtime.login.path, `${runtime.baseUrl}/`).toString();
|
|
21
|
+
await page.goto(loginUrl, { waitUntil: "load", timeout: 20_000 });
|
|
22
|
+
const loginError = await submitLogin(page, runtime, persona);
|
|
23
|
+
if (loginError) {
|
|
24
|
+
return emptyObservation(page.url(), loginError);
|
|
25
|
+
}
|
|
26
|
+
for (const step of qa.steps) {
|
|
27
|
+
if (/^open\b/i.test(step)) {
|
|
28
|
+
const opened = await openTarget(page, step, qa.preconditions);
|
|
29
|
+
if (!opened) {
|
|
30
|
+
return emptyObservation(page.url(), `Could not follow: ${step}`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
await settleAfterOpen(page);
|
|
35
|
+
for (const step of qa.steps) {
|
|
36
|
+
if (!/^select\b/i.test(step))
|
|
37
|
+
continue;
|
|
38
|
+
const selectError = await applySelect(page, step);
|
|
39
|
+
if (selectError) {
|
|
40
|
+
return emptyObservation(page.url(), selectError);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const listedOptions = await page
|
|
44
|
+
.locator("select option")
|
|
45
|
+
.allInnerTexts()
|
|
46
|
+
.then((items) => items.map((item) => item.trim()).filter(Boolean))
|
|
47
|
+
.catch(() => []);
|
|
48
|
+
const control = qa.control
|
|
49
|
+
? page.getByRole("button", { name: new RegExp(`^${escapeRegExp(qa.control)}$`, "i") })
|
|
50
|
+
: null;
|
|
51
|
+
const controlCount = control ? await control.count() : 0;
|
|
52
|
+
const controlAvailable = controlCount > 0;
|
|
53
|
+
const controlDisabled = controlAvailable ? await control.first().isDisabled() : false;
|
|
54
|
+
let clicked = false;
|
|
55
|
+
if (qa.kind === "allow" && control && controlAvailable && !controlDisabled) {
|
|
56
|
+
const before = ((await page.locator("body").innerText().catch(() => "")) ?? "")
|
|
57
|
+
.replace(/\s+/g, " ")
|
|
58
|
+
.trim();
|
|
59
|
+
await control.first().click();
|
|
60
|
+
clicked = true;
|
|
61
|
+
await settleAfterAllow(page, qa, before);
|
|
62
|
+
}
|
|
63
|
+
const seen = await observe(page, qa.control);
|
|
64
|
+
const options = listedOptions.length ? listedOptions : seen.options;
|
|
65
|
+
const prefix = options.length ? `Selectable options: ${options.join("; ")}. ` : "";
|
|
66
|
+
return {
|
|
67
|
+
...seen,
|
|
68
|
+
text: `${prefix}${seen.text}`.slice(0, 4000),
|
|
69
|
+
options,
|
|
70
|
+
controlAvailable,
|
|
71
|
+
controlDisabled,
|
|
72
|
+
clicked,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
return emptyObservation(page.url(), shortError(err));
|
|
77
|
+
}
|
|
78
|
+
finally {
|
|
79
|
+
await context.close();
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
async function submitLogin(page, runtime, persona) {
|
|
85
|
+
const email = page.locator(runtime.login.email);
|
|
86
|
+
const password = page.locator(runtime.login.password);
|
|
87
|
+
const submit = page.locator(runtime.login.submit);
|
|
88
|
+
await email.waitFor({ state: "visible" });
|
|
89
|
+
await waitForReactField(email);
|
|
90
|
+
await fillControlled(email, persona.email);
|
|
91
|
+
await fillControlled(password, persona.password ?? "");
|
|
92
|
+
if ((await email.inputValue()) !== persona.email) {
|
|
93
|
+
return `Login form did not keep ${persona.email} (saw "${await email.inputValue()}").`;
|
|
94
|
+
}
|
|
95
|
+
if ((await password.inputValue()) !== (persona.password ?? "")) {
|
|
96
|
+
await fillControlled(password, persona.password ?? "");
|
|
97
|
+
}
|
|
98
|
+
await submit.click();
|
|
99
|
+
const leftLogin = await waitToLeaveLogin(page, runtime, email);
|
|
100
|
+
if (leftLogin) {
|
|
101
|
+
await waitForPostLogin(page);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
await fillControlled(email, persona.email);
|
|
105
|
+
await fillControlled(password, persona.password ?? "");
|
|
106
|
+
await submit.click();
|
|
107
|
+
if (await waitToLeaveLogin(page, runtime, email)) {
|
|
108
|
+
await waitForPostLogin(page);
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
const body = ((await page.locator("body").innerText().catch(() => "")) ?? "")
|
|
112
|
+
.replace(/\s+/g, " ")
|
|
113
|
+
.trim()
|
|
114
|
+
.slice(0, 400);
|
|
115
|
+
if (new URL(page.url()).search === "?") {
|
|
116
|
+
return `Deterministic login for ${persona.email} never ran the sign-in script (the form did a plain HTML GET). fixtures/runtime.yaml baseUrl must match the origin you use in the browser — localhost and 127.0.0.1 are different.`;
|
|
117
|
+
}
|
|
118
|
+
return `Deterministic login failed for ${persona.email}. ${body}`;
|
|
119
|
+
}
|
|
120
|
+
async function waitToLeaveLogin(page, runtime, email) {
|
|
121
|
+
await Promise.race([
|
|
122
|
+
page.waitForURL((url) => !isLoginUrl(String(url), runtime), { timeout: 15_000 }),
|
|
123
|
+
email.waitFor({ state: "hidden", timeout: 15_000 }),
|
|
124
|
+
]).catch(() => { });
|
|
125
|
+
if (!(isLoginUrl(page.url(), runtime) && (await email.count()) > 0))
|
|
126
|
+
return true;
|
|
127
|
+
const body = ((await page.locator("body").innerText().catch(() => "")) ?? "").toLowerCase();
|
|
128
|
+
if (!/signing in/.test(body))
|
|
129
|
+
return false;
|
|
130
|
+
await Promise.race([
|
|
131
|
+
page.waitForURL((url) => !isLoginUrl(String(url), runtime), { timeout: 15_000 }),
|
|
132
|
+
email.waitFor({ state: "hidden", timeout: 15_000 }),
|
|
133
|
+
]).catch(() => { });
|
|
134
|
+
return !(isLoginUrl(page.url(), runtime) && (await email.count()) > 0);
|
|
135
|
+
}
|
|
136
|
+
async function waitForPostLogin(page) {
|
|
137
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 15_000 }).catch(() => { });
|
|
138
|
+
await page.getByRole("link").first().waitFor({ state: "visible", timeout: 15_000 }).catch(() => { });
|
|
139
|
+
}
|
|
140
|
+
function isLoginUrl(url, runtime) {
|
|
141
|
+
try {
|
|
142
|
+
const path = new URL(url).pathname.replace(/\/$/, "") || "/";
|
|
143
|
+
const login = new URL(runtime.login.path, `${runtime.baseUrl}/`).pathname.replace(/\/$/, "") || "/";
|
|
144
|
+
return path === login;
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
return url.includes(runtime.login.path);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async function waitForReactField(locator) {
|
|
151
|
+
await locator.waitFor({ state: "visible" });
|
|
152
|
+
await locator.evaluate(`(el) => {
|
|
153
|
+
const deadline = Date.now() + 10000;
|
|
154
|
+
const ready = () => Object.keys(el).some((key) => key.startsWith("__reactFiber") || key.startsWith("__reactProps"));
|
|
155
|
+
if (ready()) return;
|
|
156
|
+
return new Promise((resolve, reject) => {
|
|
157
|
+
const tick = () => {
|
|
158
|
+
if (ready()) {
|
|
159
|
+
resolve(undefined);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (Date.now() > deadline) {
|
|
163
|
+
reject(new Error("Login form did not hydrate."));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
requestAnimationFrame(tick);
|
|
167
|
+
};
|
|
168
|
+
tick();
|
|
169
|
+
});
|
|
170
|
+
}`);
|
|
171
|
+
}
|
|
172
|
+
async function fillControlled(locator, value) {
|
|
173
|
+
await locator.evaluate(`(el, next) => {
|
|
174
|
+
const input = el;
|
|
175
|
+
input.setAttribute("autocomplete", "off");
|
|
176
|
+
const proto = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value");
|
|
177
|
+
proto && proto.set && proto.set.call(input, next);
|
|
178
|
+
const propsKey = Object.keys(input).find((key) => key.startsWith("__reactProps"));
|
|
179
|
+
const props = propsKey ? input[propsKey] : undefined;
|
|
180
|
+
const event = new Event("input", { bubbles: true });
|
|
181
|
+
Object.defineProperty(event, "target", { writable: false, value: input });
|
|
182
|
+
Object.defineProperty(event, "currentTarget", { writable: false, value: input });
|
|
183
|
+
input.dispatchEvent(event);
|
|
184
|
+
try {
|
|
185
|
+
if (props && props.onChange) props.onChange(event);
|
|
186
|
+
} catch (err) {}
|
|
187
|
+
input.dispatchEvent(new Event("change", { bubbles: true }));
|
|
188
|
+
}`, value);
|
|
189
|
+
if ((await locator.inputValue()) === value)
|
|
190
|
+
return;
|
|
191
|
+
await locator.click();
|
|
192
|
+
await locator.fill(value);
|
|
193
|
+
}
|
|
194
|
+
async function settleAfterAllow(page, qa, beforeText) {
|
|
195
|
+
const pendingName = controlPendingName(qa.control);
|
|
196
|
+
if (pendingName) {
|
|
197
|
+
const pending = page.getByRole("button", { name: new RegExp(`^${escapeRegExp(pendingName)}`, "i") });
|
|
198
|
+
await pending.waitFor({ state: "visible", timeout: 2_000 }).catch(() => { });
|
|
199
|
+
await pending.waitFor({ state: "hidden", timeout: 10_000 }).catch(() => { });
|
|
200
|
+
}
|
|
201
|
+
const before = beforeText.replace(/\s+/g, " ").trim();
|
|
202
|
+
const deadline = Date.now() + 8_000;
|
|
203
|
+
while (Date.now() < deadline) {
|
|
204
|
+
const now = ((await page.locator("body").innerText().catch(() => "")) ?? "").replace(/\s+/g, " ").trim();
|
|
205
|
+
if (now && now !== before)
|
|
206
|
+
return;
|
|
207
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
export function controlPendingName(control) {
|
|
211
|
+
const verb = control?.trim().split(/\s+/)[0];
|
|
212
|
+
if (!verb)
|
|
213
|
+
return null;
|
|
214
|
+
return `${verb.replace(/e$/i, "")}ing`;
|
|
215
|
+
}
|
|
216
|
+
async function readSelectOptions(select) {
|
|
217
|
+
const nodes = select.locator("option");
|
|
218
|
+
const count = await nodes.count();
|
|
219
|
+
const options = [];
|
|
220
|
+
for (let i = 0; i < count; i++) {
|
|
221
|
+
const option = nodes.nth(i);
|
|
222
|
+
const label = ((await option.innerText()) ?? "").replace(/\s+/g, " ").trim();
|
|
223
|
+
const value = (await option.getAttribute("value")) ?? "";
|
|
224
|
+
options.push({ value, label });
|
|
225
|
+
}
|
|
226
|
+
return options;
|
|
227
|
+
}
|
|
228
|
+
async function applySelect(page, step) {
|
|
229
|
+
const wanted = selectWantedLabel(step);
|
|
230
|
+
const select = page.locator("select").first();
|
|
231
|
+
await select.waitFor({ state: "visible", timeout: 8_000 }).catch(() => { });
|
|
232
|
+
if ((await select.count()) === 0) {
|
|
233
|
+
return `No select control for: ${step}`;
|
|
234
|
+
}
|
|
235
|
+
const deadline = Date.now() + 8_000;
|
|
236
|
+
let options = await readSelectOptions(select);
|
|
237
|
+
let match = options.find((item) => optionMatches(item.label, wanted));
|
|
238
|
+
while (!match && Date.now() < deadline) {
|
|
239
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
240
|
+
options = await readSelectOptions(select);
|
|
241
|
+
match = options.find((item) => optionMatches(item.label, wanted));
|
|
242
|
+
}
|
|
243
|
+
if (!match) {
|
|
244
|
+
const listed = options.map((item) => item.label).filter(Boolean).join(", ") || "(none)";
|
|
245
|
+
return `No option matching "${wanted}". Saw: ${listed}.`;
|
|
246
|
+
}
|
|
247
|
+
if (match.value) {
|
|
248
|
+
await select.selectOption({ value: match.value }).catch(() => { });
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
await select.selectOption({ label: match.label }).catch(() => { });
|
|
252
|
+
}
|
|
253
|
+
await select.evaluate(`(el, next) => {
|
|
254
|
+
const proto = Object.getOwnPropertyDescriptor(window.HTMLSelectElement.prototype, "value");
|
|
255
|
+
proto && proto.set && proto.set.call(el, next);
|
|
256
|
+
const propsKey = Object.keys(el).find((key) => key.startsWith("__reactProps"));
|
|
257
|
+
const props = propsKey ? el[propsKey] : undefined;
|
|
258
|
+
const native = new Event("change", { bubbles: true });
|
|
259
|
+
try {
|
|
260
|
+
if (props && props.onChange) props.onChange({ target: { value: next } });
|
|
261
|
+
} catch (err) {}
|
|
262
|
+
el.dispatchEvent(native);
|
|
263
|
+
}`, match.value || match.label);
|
|
264
|
+
return null;
|
|
265
|
+
}
|
|
266
|
+
export function selectWantedLabel(step) {
|
|
267
|
+
return step
|
|
268
|
+
.replace(/^select\s+/i, "")
|
|
269
|
+
.replace(/^the\s+/i, "")
|
|
270
|
+
.replace(/\s+in\s+the\s+.+$/i, "")
|
|
271
|
+
.trim();
|
|
272
|
+
}
|
|
273
|
+
export function optionMatches(optionLabel, wanted) {
|
|
274
|
+
const option = optionLabel.toLowerCase();
|
|
275
|
+
const want = wanted.toLowerCase().trim();
|
|
276
|
+
if (!want)
|
|
277
|
+
return false;
|
|
278
|
+
if (option.includes(want))
|
|
279
|
+
return true;
|
|
280
|
+
if (want.split(/\s+/).every((word) => option.includes(word)))
|
|
281
|
+
return true;
|
|
282
|
+
const wantTokens = tokenize(wanted);
|
|
283
|
+
if (wantTokens.length === 0)
|
|
284
|
+
return false;
|
|
285
|
+
return wantTokens.every((token) => option.includes(token));
|
|
286
|
+
}
|
|
287
|
+
async function openTarget(page, step, preconditions) {
|
|
288
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 15_000 }).catch(() => { });
|
|
289
|
+
await page.getByRole("link").first().waitFor({ state: "visible", timeout: 8_000 }).catch(() => { });
|
|
290
|
+
const links = page.getByRole("link");
|
|
291
|
+
const count = await links.count();
|
|
292
|
+
const candidates = [];
|
|
293
|
+
for (let i = 0; i < count; i++) {
|
|
294
|
+
const name = ((await links.nth(i).innerText()) ?? "").replace(/\s+/g, " ").trim();
|
|
295
|
+
if (!name)
|
|
296
|
+
continue;
|
|
297
|
+
if (/sign out/i.test(name))
|
|
298
|
+
continue;
|
|
299
|
+
if (/\blist\b/i.test(step) && linkMatchesOpen(name, step)) {
|
|
300
|
+
await links.nth(i).click();
|
|
301
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 15_000 });
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
if (/^←/.test(name))
|
|
305
|
+
continue;
|
|
306
|
+
candidates.push({ index: i, name });
|
|
307
|
+
}
|
|
308
|
+
if (/\blist\b/i.test(step))
|
|
309
|
+
return true;
|
|
310
|
+
const pick = pickOpenTarget(candidates, step, preconditions);
|
|
311
|
+
if (!pick)
|
|
312
|
+
return candidates.length === 0;
|
|
313
|
+
const beforeUrl = page.url();
|
|
314
|
+
const href = (await links.nth(pick.index).getAttribute("href")) ?? "";
|
|
315
|
+
await links.nth(pick.index).click();
|
|
316
|
+
const hrefPath = href.replace(/^https?:\/\/[^/]+/i, "").split("#")[0] ?? "";
|
|
317
|
+
if (hrefPath && hrefPath !== "/" && hrefPath !== "#") {
|
|
318
|
+
await page
|
|
319
|
+
.waitForURL((url) => {
|
|
320
|
+
const now = String(url);
|
|
321
|
+
if (now === beforeUrl)
|
|
322
|
+
return false;
|
|
323
|
+
return now.includes(hrefPath) || new URL(now).pathname.includes(hrefPath);
|
|
324
|
+
}, { timeout: 15_000 })
|
|
325
|
+
.catch(() => { });
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
await page.waitForLoadState("domcontentloaded", { timeout: 15_000 });
|
|
329
|
+
}
|
|
330
|
+
await settleAfterOpen(page);
|
|
331
|
+
return true;
|
|
332
|
+
}
|
|
333
|
+
async function settleAfterOpen(page) {
|
|
334
|
+
const loading = page.getByText(/^Loading/).first();
|
|
335
|
+
await loading.waitFor({ state: "visible", timeout: 500 }).catch(() => { });
|
|
336
|
+
await loading.waitFor({ state: "hidden", timeout: 10_000 }).catch(() => { });
|
|
337
|
+
await page
|
|
338
|
+
.locator("#status-label, [data-testid='status-label'], select, h1")
|
|
339
|
+
.first()
|
|
340
|
+
.waitFor({ state: "visible", timeout: 8_000 })
|
|
341
|
+
.catch(() => { });
|
|
342
|
+
}
|
|
343
|
+
function linkMatchesOpen(name, step) {
|
|
344
|
+
const wanted = tokenize(step.replace(/\blist\b/i, ""));
|
|
345
|
+
if (wanted.length === 0)
|
|
346
|
+
return /\blist\b/i.test(name);
|
|
347
|
+
const have = new Set(tokenize(name));
|
|
348
|
+
return wanted.every((token) => have.has(token) || name.toLowerCase().includes(token));
|
|
349
|
+
}
|
|
350
|
+
const STOP = new Set([
|
|
351
|
+
"the",
|
|
352
|
+
"a",
|
|
353
|
+
"an",
|
|
354
|
+
"and",
|
|
355
|
+
"or",
|
|
356
|
+
"to",
|
|
357
|
+
"of",
|
|
358
|
+
"in",
|
|
359
|
+
"on",
|
|
360
|
+
"for",
|
|
361
|
+
"is",
|
|
362
|
+
"are",
|
|
363
|
+
"be",
|
|
364
|
+
"been",
|
|
365
|
+
"being",
|
|
366
|
+
"this",
|
|
367
|
+
"that",
|
|
368
|
+
"with",
|
|
369
|
+
"as",
|
|
370
|
+
"their",
|
|
371
|
+
"them",
|
|
372
|
+
"they",
|
|
373
|
+
"it",
|
|
374
|
+
"its",
|
|
375
|
+
"at",
|
|
376
|
+
"by",
|
|
377
|
+
"from",
|
|
378
|
+
"exists",
|
|
379
|
+
"exist",
|
|
380
|
+
"visible",
|
|
381
|
+
"logged",
|
|
382
|
+
"login",
|
|
383
|
+
"persona",
|
|
384
|
+
"open",
|
|
385
|
+
"choose",
|
|
386
|
+
"look",
|
|
387
|
+
"select",
|
|
388
|
+
"has",
|
|
389
|
+
"have",
|
|
390
|
+
"had",
|
|
391
|
+
"can",
|
|
392
|
+
"may",
|
|
393
|
+
"must",
|
|
394
|
+
"admin",
|
|
395
|
+
"viewer",
|
|
396
|
+
]);
|
|
397
|
+
function tokenize(value) {
|
|
398
|
+
return value
|
|
399
|
+
.replace(/([a-z])([A-Z])/g, "$1 $2")
|
|
400
|
+
.toLowerCase()
|
|
401
|
+
.split(/[^a-z0-9]+/)
|
|
402
|
+
.filter((word) => word.length >= 2 && !STOP.has(word));
|
|
403
|
+
}
|
|
404
|
+
function clausesWithHead(preconditions, head) {
|
|
405
|
+
if (!head)
|
|
406
|
+
return [];
|
|
407
|
+
const singular = head.endsWith("s") ? head.slice(0, -1) : head;
|
|
408
|
+
const plural = head.endsWith("s") ? head : `${head}s`;
|
|
409
|
+
return preconditions.split(/(?<=[.!?])\s+/).filter((clause) => {
|
|
410
|
+
const tokens = tokenize(clause);
|
|
411
|
+
return tokens.includes(head) || tokens.includes(singular) || tokens.includes(plural);
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
export function pickOpenTarget(candidates, step, preconditions) {
|
|
415
|
+
if (/\blist\b/i.test(step))
|
|
416
|
+
return undefined;
|
|
417
|
+
const stepPhrase = step.replace(/^open\s+/i, "").replace(/^the\s+/i, "").trim();
|
|
418
|
+
const stepTokens = tokenize(stepPhrase);
|
|
419
|
+
const head = stepTokens.at(-1);
|
|
420
|
+
const stepModifiers = stepTokens.slice(0, -1);
|
|
421
|
+
const relevant = clausesWithHead(preconditions, head).join(" ");
|
|
422
|
+
const relevantLower = relevant.toLowerCase();
|
|
423
|
+
const wantNegation = /\bno\b|\bnone\b|\bwithout\b|\bunassigned\b|\bnot\b/.test(relevantLower);
|
|
424
|
+
const wantAssigned = !wantNegation && /assigned/.test(relevantLower);
|
|
425
|
+
const preTokens = tokenize(relevant);
|
|
426
|
+
let best;
|
|
427
|
+
for (const item of candidates) {
|
|
428
|
+
const text = item.name.toLowerCase();
|
|
429
|
+
const tokens = new Set(tokenize(item.name));
|
|
430
|
+
if (stepModifiers.some((modifier) => !text.includes(modifier) && !tokens.has(modifier))) {
|
|
431
|
+
continue;
|
|
432
|
+
}
|
|
433
|
+
let score = 0;
|
|
434
|
+
for (const modifier of stepModifiers) {
|
|
435
|
+
if (text.includes(modifier) || tokens.has(modifier))
|
|
436
|
+
score += 20;
|
|
437
|
+
}
|
|
438
|
+
for (const token of preTokens) {
|
|
439
|
+
if (token === head)
|
|
440
|
+
continue;
|
|
441
|
+
if (text.includes(token) || tokens.has(token))
|
|
442
|
+
score += 3;
|
|
443
|
+
}
|
|
444
|
+
const itemNegation = /\bno\b|\bnone\b|\bwithout\b|\bunassigned\b/.test(text);
|
|
445
|
+
if (wantNegation)
|
|
446
|
+
score += itemNegation ? 15 : -15;
|
|
447
|
+
if (wantAssigned)
|
|
448
|
+
score += itemNegation ? -15 : 8;
|
|
449
|
+
if (!best || score > best.score)
|
|
450
|
+
best = { item, score };
|
|
451
|
+
}
|
|
452
|
+
return best?.item ?? candidates[0];
|
|
453
|
+
}
|
|
454
|
+
async function observe(page, control) {
|
|
455
|
+
const text = ((await page.locator("body").innerText().catch(() => "")) ?? "").replace(/\s+/g, " ").trim();
|
|
456
|
+
const options = await page.locator("select option").allInnerTexts().catch(() => []);
|
|
457
|
+
return {
|
|
458
|
+
url: page.url(),
|
|
459
|
+
text: `${text}${control ? ` control:${control}` : ""}`.slice(0, 4000),
|
|
460
|
+
options: options.map((item) => item.trim()).filter(Boolean),
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
async function launchChromium(playwright, onProgress) {
|
|
464
|
+
const attempts = [];
|
|
465
|
+
const bundled = playwright.chromium.executablePath();
|
|
466
|
+
if (await pathExists(bundled)) {
|
|
467
|
+
attempts.push({ label: "Playwright Chromium", options: { headless: true, timeout: 15_000 } });
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
onProgress?.("Playwright Chromium is not installed (chrome-headless-shell missing). Trying system Chrome…");
|
|
471
|
+
}
|
|
472
|
+
attempts.push({ label: "system Chrome", options: { headless: true, channel: "chrome", timeout: 15_000 } });
|
|
473
|
+
attempts.push({ label: "system Edge", options: { headless: true, channel: "msedge", timeout: 15_000 } });
|
|
474
|
+
const errors = [];
|
|
475
|
+
for (const attempt of attempts) {
|
|
476
|
+
onProgress?.(`Launching ${attempt.label}…`);
|
|
477
|
+
await yieldEventLoop();
|
|
478
|
+
try {
|
|
479
|
+
return await playwright.chromium.launch({
|
|
480
|
+
...attempt.options,
|
|
481
|
+
args: [
|
|
482
|
+
"--disable-save-password-bubble",
|
|
483
|
+
"--disable-features=PasswordManagerOnboarding,PasswordCheck,AutofillServerCommunication",
|
|
484
|
+
],
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
catch (err) {
|
|
488
|
+
const message = shortError(err);
|
|
489
|
+
onProgress?.(`${attempt.label} failed: ${message}`, "error");
|
|
490
|
+
errors.push(`${attempt.label}: ${message}`);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
throw new Error(`Could not launch a browser. ${errors.join(" ")} Install Google Chrome, or run "npx playwright install chromium".`);
|
|
494
|
+
}
|
|
495
|
+
async function pathExists(file) {
|
|
496
|
+
try {
|
|
497
|
+
await access(file);
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
catch {
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
export function shortError(err) {
|
|
505
|
+
const text = err instanceof Error ? err.message : String(err);
|
|
506
|
+
return text.split("\n")[0]?.replace(/^browserType\.launch:\s*/i, "").slice(0, 220) ?? "Browser failed.";
|
|
507
|
+
}
|
|
508
|
+
async function yieldEventLoop() {
|
|
509
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
510
|
+
}
|
|
511
|
+
function emptyObservation(url, error) {
|
|
512
|
+
return {
|
|
513
|
+
url,
|
|
514
|
+
text: "",
|
|
515
|
+
options: [],
|
|
516
|
+
controlAvailable: false,
|
|
517
|
+
controlDisabled: false,
|
|
518
|
+
clicked: false,
|
|
519
|
+
error,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
function escapeRegExp(value) {
|
|
523
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
524
|
+
}
|