Skip to content

Commit 248a0a1

Browse files
committed
Bring six never-run browser tests up to the app as it is, and hold the app's Desktop calls to the real shell
1 parent ba4b2ed commit 248a0a1

7 files changed

Lines changed: 399 additions & 123 deletions

‎app/tests/admin-path-layout.test.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ async function open(
179179
reducedMotion: "reduce",
180180
});
181181
page.on("pageerror", (error) => console.error(error));
182-
const inter = join(packageDir("@fontsource-variable/inter"), "files");
182+
// The app's own face (app/src/styles.css): the page is measured in the type it ships with.
183+
const figtree = join(packageDir("@fontsource-variable/figtree"), "files");
183184
await page.route(`${ORIGIN}/**`, (route) => {
184185
const at = new URL(route.request().url()).pathname;
185186
if (at === "/admin-path.js")
186187
return route.fulfill({ contentType: "text/javascript", body: script });
187-
const font = join(inter, at.split("/").pop() ?? "");
188+
const font = join(figtree, at.split("/").pop() ?? "");
188189
if (at.startsWith("/files/") && existsSync(font))
189190
return route.fulfill({ path: font });
190191
// The fixture answers the API itself; anything that reaches the network has nowhere to go.

‎app/tests/browse-steps-layout.test.tsx‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,12 @@ describe.skipIf(!asked)("a Bot's browse steps, opened", () => {
170170
test(`name each field and button, never what was typed, in ${engine}, ${theme}, ${window.width}px`, async () => {
171171
const page = await open(engine, theme, window);
172172
try {
173+
// The turn is over (the Bot said "You're signed in."), so its work is one line,
174+
// "Worked through 6 steps", as a finished turn's completed tool work is in any
175+
// conversation (`foldTurnWork`, chat-transcript.tsx), and a Bot's browser steps are rows
176+
// of that line, not a line of their own. Opened, it lists them.
173177
await page
174-
.locator("summary", {
175-
hasText: "Browsed the-internet.herokuapp.com",
176-
})
178+
.locator("summary", { hasText: "Worked through 6 steps" })
177179
.click();
178180
const rows = page.locator("details[open] ol > li");
179181
await rows.first().waitFor({ timeout: 5_000 });
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { readdirSync, readFileSync } from "node:fs";
3+
import { join } from "node:path";
4+
5+
/**
6+
* EVERY CALL THE APP'S PAGE MAKES TO OPENBOT DESKTOP IS ONE THE REAL DESKTOP ANSWERS IT.
7+
*
8+
* In OpenBot Desktop the app's page asks the shell through Tauri: `invokeDesktop("status")` is the
9+
* command `plugin:openbot-desktop|status`. Tauri answers it only when the shell registers the
10+
* command (`generate_handler!` in desktop/src-tauri/src/desktop_page.rs) and grants the page its
11+
* permission (`openbot-desktop:allow-status`, in `PERMISSIONS`, which `allow_app_page` in main.rs
12+
* grants the page's own origin and nothing else). A call the app makes that the shell lacks is
13+
* refused on a person's Mac or PC at the moment they use it, and nothing before then says so.
14+
*
15+
* Nothing in CI held the app's calls to the real shell. The Workbench's fake shell
16+
* (desktop/src/fakeNative/pageRegistry.test.ts) checks them against its own list, which it holds
17+
* to `generate_handler!`, but the Workbench's tests are a dev tool's and run in no CI. #2720 added a
18+
* call in app/src and that test went red where nobody ran it. This reads the real shell's source
19+
* instead, and runs in the suite, on every change to the app or the shell and in every merge group.
20+
*
21+
* The page's calls to Tauri's own plugins go through the same gate: each is granted to the page in
22+
* `allow_app_page`, or is one the app makes knowing it may be refused and has a way round
23+
* (`BEST_EFFORT`, each with where its way round is).
24+
*/
25+
26+
const ROOT = join(import.meta.dir, "..", "..");
27+
const APP_SRC = join(ROOT, "app", "src");
28+
const GENERATED = join(APP_SRC, "lib", "api", "generated");
29+
const PAGE_RS = join(ROOT, "desktop", "src-tauri", "src", "desktop_page.rs");
30+
const MAIN_RS = join(ROOT, "desktop", "src-tauri", "src", "main.rs");
31+
const PAGE_PLUGIN = "plugin:openbot-desktop";
32+
33+
/**
34+
* Calls to Tauri's own plugins that the shell does not grant the page, on purpose, each with its way
35+
* round when refused. `allow_app_page` grants the page window dragging, Settings' commands and
36+
* notifications, "and nothing more": `core:event` would let the page hear every event the shell
37+
* sends its window.
38+
*/
39+
const BEST_EFFORT: Record<string, string> = {
40+
// Whether the Desktop window is in front: the DOM's own focus and blur say the same.
41+
"plugin:event|listen": "app/src/lib/desktop/window-attention.ts",
42+
"plugin:event|unlisten": "app/src/lib/desktop/window-attention.ts",
43+
};
44+
45+
function sources(dir: string): string[] {
46+
return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
47+
const path = join(dir, entry.name);
48+
if (entry.isDirectory()) return path === GENERATED ? [] : sources(path);
49+
return /\.tsx?$/.test(entry.name) && !/\.test\./.test(entry.name)
50+
? [path]
51+
: [];
52+
});
53+
}
54+
55+
/** A call's name, with any type arguments between it and its first argument. */
56+
const GENERIC = String.raw`\s*(?:<[^>]*(?:<[^>]*>[^>]*)*>)?\s*\(\s*"([a-z_]+)"`;
57+
58+
/** Every `plugin:<name>|<command>` the app's own code asks the shell for, with a file that asks. */
59+
function appCalls(): Map<string, string> {
60+
const calls = new Map<string, string>();
61+
const add = (call: string, file: string) => {
62+
if (!calls.has(call)) calls.set(call, file.slice(ROOT.length + 1));
63+
};
64+
for (const file of sources(APP_SRC)) {
65+
const text = readFileSync(file, "utf8");
66+
for (const match of text.matchAll(
67+
new RegExp(String.raw`\binvokeDesktop${GENERIC}`, "gs"),
68+
))
69+
add(`${PAGE_PLUGIN}|${match[1]}`, file);
70+
// The AI connection's own helper names the page's commands the same way.
71+
if (file.endsWith(join("lib", "ai-connection", "desktop.ts")))
72+
for (const match of text.matchAll(
73+
new RegExp(String.raw`\bdesktop${GENERIC}`, "gs"),
74+
))
75+
add(`${PAGE_PLUGIN}|${match[1]}`, file);
76+
// `${PLUGIN}|command`, where the file names the plugin in a constant.
77+
const plugin = /const PLUGIN = "(plugin:[a-z-]+)"/.exec(text)?.[1];
78+
if (plugin)
79+
for (const match of text.matchAll(/\$\{PLUGIN\}\|([a-z_]+)/g))
80+
add(`${plugin}|${match[1]}`, file);
81+
for (const match of text.matchAll(/"(plugin:[a-z-]+\|[a-z_]+)"/g))
82+
add(match[1] as string, file);
83+
}
84+
return calls;
85+
}
86+
87+
const pageRs = readFileSync(PAGE_RS, "utf8");
88+
const mainRs = readFileSync(MAIN_RS, "utf8");
89+
90+
/** The commands the shell registers for the page: `generate_handler![…]` in desktop_page.rs. */
91+
function registered(): Set<string> {
92+
const start = pageRs.indexOf("generate_handler![");
93+
expect(start).toBeGreaterThanOrEqual(0);
94+
const body = pageRs.slice(
95+
start + "generate_handler![".length,
96+
pageRs.indexOf("]", start),
97+
);
98+
return new Set(body.split(/[\s,]+/).filter(Boolean));
99+
}
100+
101+
/** The permissions the shell grants the page's own origin: `allow_app_page` and what it chains. */
102+
function granted(): Set<string> {
103+
const quoted = (text: string) =>
104+
[...text.matchAll(/"([a-z-]+:[a-z-]+)"/g)].map(
105+
(match) => match[1] as string,
106+
);
107+
const list = (source: string, name: string) => {
108+
const start = source.indexOf(`const ${name}:`);
109+
expect(start).toBeGreaterThanOrEqual(0);
110+
return quoted(source.slice(start, source.indexOf("];", start)));
111+
};
112+
const start = mainRs.indexOf("fn allow_app_page");
113+
expect(start).toBeGreaterThanOrEqual(0);
114+
const body = mainRs.slice(start, mainRs.indexOf("\n}\n", start));
115+
// The chained lists, by name: the page's own commands and its notifications.
116+
expect(body).toContain("desktop_page::PERMISSIONS");
117+
expect(body).toContain("APP_PAGE_NOTIFICATIONS");
118+
return new Set([
119+
...[...body.matchAll(/\.permission\("([^"]+)"\)/g)].map(
120+
(match) => match[1] as string,
121+
),
122+
...list(pageRs, "PERMISSIONS"),
123+
...list(mainRs, "APP_PAGE_NOTIFICATIONS"),
124+
]);
125+
}
126+
127+
/** The permission a call needs: Tauri's own plugins are `core:<plugin>:allow-<command>`. */
128+
function permissionFor(call: string): string {
129+
const [plugin, command] = call.replace(/^plugin:/, "").split("|") as [
130+
string,
131+
string,
132+
];
133+
const core = ["event", "window", "webview", "app", "path", "menu", "tray"];
134+
return `${core.includes(plugin) ? `core:${plugin}` : plugin}:allow-${command.replaceAll("_", "-")}`;
135+
}
136+
137+
describe("the app's page asking OpenBot Desktop", () => {
138+
const calls = appCalls();
139+
140+
test("finds the page's calls, the way the app makes them", () => {
141+
// Settings' commands, the update banner, speech, sign-in and the AI connection, at the least.
142+
expect(
143+
[...calls.keys()].filter((call) => call.startsWith(`${PAGE_PLUGIN}|`))
144+
.length,
145+
).toBeGreaterThanOrEqual(20);
146+
for (const call of [
147+
`${PAGE_PLUGIN}|status`,
148+
`${PAGE_PLUGIN}|speech_start`,
149+
"plugin:event|listen",
150+
])
151+
expect([...calls.keys()]).toContain(call);
152+
});
153+
154+
test("every Settings command the page calls is one the shell registers and grants it", () => {
155+
const commands = registered();
156+
const permissions = granted();
157+
const missing = [...calls]
158+
.filter(([call]) => call.startsWith(`${PAGE_PLUGIN}|`))
159+
.flatMap(([call, file]) => {
160+
const command = call.slice(PAGE_PLUGIN.length + 1);
161+
const problems = [
162+
...(commands.has(command) ? [] : ["not registered"]),
163+
...(permissions.has(permissionFor(call)) ? [] : ["not granted"]),
164+
];
165+
return problems.length
166+
? [`${call} (${file}): ${problems.join(", ")}`]
167+
: [];
168+
});
169+
expect(missing).toEqual([]);
170+
});
171+
172+
test("every call to Tauri's own plugins is granted to the page, or made knowing it may be refused", () => {
173+
const permissions = granted();
174+
const refused = [...calls]
175+
.filter(([call]) => !call.startsWith(`${PAGE_PLUGIN}|`))
176+
.filter(([call]) => !permissions.has(permissionFor(call)))
177+
.filter(([call, file]) => BEST_EFFORT[call] !== file)
178+
.map(([call, file]) => `${call} (${file})`);
179+
expect(refused).toEqual([]);
180+
});
181+
182+
test("a call made knowing it may be refused is still made, and still refused", () => {
183+
// So this list never outlives what it excuses: each is still asked for where it says, and the
184+
// shell still does not grant it. Granted one day, it comes off the list.
185+
const permissions = granted();
186+
for (const [call, file] of Object.entries(BEST_EFFORT)) {
187+
expect({ call, file: calls.get(call) }).toEqual({ call, file });
188+
expect({ call, granted: permissions.has(permissionFor(call)) }).toEqual({
189+
call,
190+
granted: false,
191+
});
192+
}
193+
});
194+
});

‎app/tests/run-ended-unheard-layout.test.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,25 @@ async function shoot(page: Page, name: string) {
615615
await page.screenshot({ path: join(shots, `${name}.png`) });
616616
}
617617

618+
/**
619+
* Opens every closed line that holds `said`, outermost first, as a person opens them. A Bot's steps
620+
* are folded once the turn is over: into "Worked through N steps" (`foldTurnWork`,
621+
* chat-transcript.tsx), or, with a command among them, into the line that names its commands, "Ran 2
622+
* commands" (#2432). Which one depends on the turn, and the steps are inside either way.
623+
*/
624+
async function openTo(page: Page, said: string) {
625+
// The lines around the step, never the step's own line, which the test opens itself.
626+
const closed = page.locator("details:not([open])", {
627+
has: page.locator("details", { has: page.getByText(said) }),
628+
});
629+
await page
630+
.getByText(said)
631+
.first()
632+
.waitFor({ state: "attached", timeout: 15_000 });
633+
for (let tries = 0; tries < 4 && (await closed.count()) > 0; tries += 1)
634+
await closed.first().locator(":scope > summary").click();
635+
}
636+
618637
describe.skipIf(!asked)("a run whose end never reached this browser", () => {
619638
for (const engine of engines)
620639
for (const theme of ["light", "dark"] as const)
@@ -662,6 +681,7 @@ describe.skipIf(!asked)("a run whose end never reached this browser", () => {
662681
// The command ran here once, and its step says what ran and what it printed.
663682
expect(scene.steps).toBe(1);
664683
expect(scene.runs).toBe(3);
684+
await openTo(page, "Ran a command");
665685
const step = page
666686
.locator("details")
667687
.filter({
@@ -707,6 +727,7 @@ describe.skipIf(!asked)("a run whose end never reached this browser", () => {
707727
.getByRole("button", { name: "Stop the Bot" })
708728
.waitFor({ state: "detached", timeout: 15_000 });
709729
// The file it saved, drawn from the server's result as a step this page took is.
730+
await openTo(page, "Saved a file");
710731
await page.getByText("Saved a file").first().waitFor();
711732
// The build the server ran, with what it printed, in the same terminal block.
712733
const step = page

0 commit comments

Comments
 (0)