|
| 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 | +}); |
0 commit comments