| 1 | import assert from "node:assert/strict"; |
| 2 | import { mkdtempSync, mkdirSync, openSync, ftruncateSync, closeSync, statSync, writeFileSync, readFileSync, readdirSync } from "node:fs"; |
| 3 | import { tmpdir } from "node:os"; |
| 4 | import { resolve, join } from "node:path"; |
| 5 | import { fileURLToPath } from "node:url"; |
| 6 | import { _electron } from "playwright"; |
| 7 | |
| 8 | const root = resolve(fileURLToPath(new URL("..", import.meta.url))); |
| 9 | const home = mkdtempSync(join(tmpdir(), "reasonix-attachment-native-")); |
| 10 | writeFileSync(join(home, "config.toml"), ""); |
| 11 | const workspace = join(home, "workspace"); |
| 12 | mkdirSync(workspace); |
| 13 | const nativeFiles = ["large app.apk", "pasted app.apk", "dropped app.apk"].map(name => { |
| 14 | const path = join(home, name); |
| 15 | const fd = openSync(path, "w"); |
| 16 | ftruncateSync(fd, 120 * 1024 * 1024); |
| 17 | closeSync(fd); |
| 18 | return path; |
| 19 | }); |
| 20 | const [apk] = nativeFiles; |
| 21 | const report = { checks: [], errors: [] }; |
| 22 | const check = name => { report.checks.push(name); console.log(`PASS ${name}`); }; |
| 23 | const invoke = (page, name, args = []) => page.evaluate(({ name, args }) => window.reasonixDesktop.invoke(name, args), { name, args }); |
| 24 | async function until(predicate) { |
| 25 | const deadline = Date.now() + 60000; |
| 26 | while (Date.now() < deadline) { |
| 27 | if (await predicate()) return; |
| 28 | await new Promise(resolve => setTimeout(resolve, 100)); |
| 29 | } |
| 30 | throw new Error("Attachment smoke condition timed out"); |
| 31 | } |
| 32 | const shell = await _electron.launch({ executablePath: process.env.REASONIX_TEST_ELECTRON, args: [root, `--user-data-dir=${join(home, "electron")}`], env: { |
| 33 | ...process.env, REASONIX_HOME: home, REASONIX_STATE_HOME: home, REASONIX_CACHE_HOME: join(home, "cache"), |
| 34 | REASONIX_DEV: "1", REASONIX_DESKTOP_SERVICE: process.env.REASONIX_DESKTOP_SERVICE || resolve(root, "../build/bin/reasonix-desktop-service" + (process.platform === "win32" ? ".exe" : "")), |
| 35 | }, timeout: 60000 }); |
| 36 | try { |
| 37 | const page = await shell.firstWindow(); |
| 38 | page.setDefaultTimeout(30000); |
| 39 | page.on("pageerror", error => report.errors.push(error.message)); |
| 40 | await page.waitForURL("reasonix://app/index.html", { waitUntil: "domcontentloaded", timeout: 60000 }); |
| 41 | await page.waitForFunction(() => Boolean(window.reasonixDesktop) && !document.querySelector(".boot-shell"), null, { timeout: 60000 }); |
| 42 | await until(async () => { try { await invoke(page, "Version"); return true; } catch { return false; } }); |
| 43 | const operationId = "attachment-native-smoke"; |
| 44 | await invoke(page, "BeginManualSessionCreation", [{ operationId, scope: "project", workspaceRoot: workspace }]); |
| 45 | await until(async () => { |
| 46 | const creation = await invoke(page, "GetManualSessionCreation", [operationId]); |
| 47 | if (creation.phase === "failed") throw new Error(creation.error); |
| 48 | return creation.phase === "ready"; |
| 49 | }); |
| 50 | const creation = await invoke(page, "GetManualSessionCreation", [operationId]); |
| 51 | report.session = creation.ref; |
| 52 | const tabs = await invoke(page, "ListTabs"); |
| 53 | const tab = tabs.find(item => item.sessionId === creation.ref.sessionId); |
| 54 | assert.ok(tab, JSON.stringify(tabs)); |
| 55 | await invoke(page, "SetActiveTab", [tab.id]); |
| 56 | await page.reload(); |
| 57 | const input = page.locator(".composer-content-file-input"); |
| 58 | await input.waitFor({ state: "attached", timeout: 60000 }); |
| 59 | await page.waitForFunction(() => !document.querySelector(".composer-content-file-input")?.disabled); |
| 60 | await page.locator(".startup-splash").waitFor({ state: "hidden", timeout: 60000 }); |
| 61 | await page.locator("#composer-input").waitFor({ state: "visible" }); |
| 62 | let uploads = 0; |
| 63 | page.on("request", request => { if (request.url().includes("/__reasonix_attachment_upload")) uploads++; }); |
| 64 | await input.setInputFiles(apk); |
| 65 | await until(async () => { |
| 66 | const state = await invoke(page, "GetSessionComposerState", [creation.ref]); |
| 67 | return JSON.parse(state.contentJson).workspaceRefs?.some(item => item.path.endsWith("large app.apk")); |
| 68 | }); |
| 69 | assert.equal(uploads, 0); |
| 70 | assert.equal(statSync(apk).size, 120 * 1024 * 1024); |
| 71 | check("120 MiB native picker adds a path reference without an HTTP upload"); |
| 72 | await page.evaluate(() => { |
| 73 | const source = document.createElement("input"); |
| 74 | source.type = "file"; |
| 75 | source.id = "attachment-smoke-source"; |
| 76 | document.body.append(source); |
| 77 | }); |
| 78 | for (const [index, kind] of ["paste", "drop"].entries()) { |
| 79 | await page.locator("#attachment-smoke-source").setInputFiles(nativeFiles[index + 1]); |
| 80 | await page.evaluate(kind => { |
| 81 | const transfer = new DataTransfer(); |
| 82 | transfer.items.add(document.querySelector("#attachment-smoke-source").files[0]); |
| 83 | const event = kind === "paste" |
| 84 | ? new ClipboardEvent("paste", { clipboardData: transfer, bubbles: true }) |
| 85 | : new DragEvent("drop", { dataTransfer: transfer, bubbles: true }); |
| 86 | document.querySelector("#composer-input").dispatchEvent(event); |
| 87 | }, kind); |
| 88 | await until(async () => { |
| 89 | const state = await invoke(page, "GetSessionComposerState", [creation.ref]); |
| 90 | return JSON.parse(state.contentJson).workspaceRefs?.length === index + 2; |
| 91 | }); |
| 92 | assert.equal(uploads, 0); |
| 93 | check(`120 MiB native ${kind} adds a path reference without an HTTP upload`); |
| 94 | } |
| 95 | await page.evaluate(() => { |
| 96 | const transfer = new DataTransfer(); |
| 97 | const chunk = new Uint8Array(64 * 1024).fill(37); |
| 98 | transfer.items.add(new File(Array(1280).fill(chunk), "virtual.apk", { type: "application/vnd.android.package-archive" })); |
| 99 | const input = document.querySelector(".composer-content-file-input"); |
| 100 | input.files = transfer.files; |
| 101 | input.dispatchEvent(new Event("change", { bubbles: true })); |
| 102 | }); |
| 103 | await until(async () => { |
| 104 | const state = await invoke(page, "GetSessionComposerState", [creation.ref]); |
| 105 | return JSON.parse(state.contentJson).attachments?.some(item => item.displayName === "virtual.apk"); |
| 106 | }); |
| 107 | const saved = JSON.parse((await invoke(page, "GetSessionComposerState", [creation.ref])).contentJson); |
| 108 | const uploaded = saved.attachments.find(item => item.displayName === "virtual.apk"); |
| 109 | const stored = join(workspace, uploaded.path); |
| 110 | assert.equal(statSync(stored).size, 80 * 1024 * 1024); |
| 111 | assert.equal(uploads, 1); |
| 112 | assert.equal(readFileSync(stored).every(byte => byte === 37), true); |
| 113 | check("80 MiB pathless File streams through the real Electron protocol and Go service byte-for-byte"); |
| 114 | writeFileSync(join(home, "config.toml"), "[attachments]\nmax_stream_bytes = 4\n"); |
| 115 | const rejected = page.waitForResponse(response => response.url().includes("/__reasonix_attachment_upload") && response.status() === 413); |
| 116 | await page.evaluate(() => { |
| 117 | const transfer = new DataTransfer(); |
| 118 | transfer.items.add(new File(["12345"], "oversize.apk")); |
| 119 | const input = document.querySelector(".composer-content-file-input"); |
| 120 | input.files = transfer.files; |
| 121 | input.dispatchEvent(new Event("change", { bubbles: true })); |
| 122 | }); |
| 123 | const failure = await (await rejected).json(); |
| 124 | assert.equal(failure.code, "attachment.too_large"); |
| 125 | assert.equal(failure.limit_bytes, 4); |
| 126 | assert.equal(failure.limit_source, "user"); |
| 127 | try { |
| 128 | const notice = page.locator(".toast--warn").filter({ hasText: "oversize.apk" }); |
| 129 | await notice.waitFor({ state: "visible" }); |
| 130 | assert.ok((await notice.innerText()).includes(String(failure.limit_bytes / (1024 * 1024)))); |
| 131 | } catch (error) { |
| 132 | writeFileSync(join(home, "limit-failure-dom.txt"), await page.locator("body").innerText()); |
| 133 | await page.screenshot({ path: join(home, "limit-failure.png") }); |
| 134 | throw error; |
| 135 | } |
| 136 | assert.equal(readdirSync(join(workspace, ".reasonix", "attachments")).length, 1); |
| 137 | check("user byte ceiling returns typed 413, displays its reason, and removes the partial file"); |
| 138 | writeFileSync(join(home, "config.toml"), "[attachments]\nmax_stream_bytes = 0\n"); |
| 139 | await page.evaluate(() => { |
| 140 | const transfer = new DataTransfer(); |
| 141 | transfer.items.add(new File(["123456"], "unlimited.apk")); |
| 142 | const input = document.querySelector(".composer-content-file-input"); |
| 143 | input.files = transfer.files; |
| 144 | input.dispatchEvent(new Event("change", { bubbles: true })); |
| 145 | }); |
| 146 | await until(async () => { |
| 147 | const state = await invoke(page, "GetSessionComposerState", [creation.ref]); |
| 148 | return JSON.parse(state.contentJson).attachments?.some(item => item.displayName === "unlimited.apk"); |
| 149 | }); |
| 150 | assert.equal(uploads, 3); |
| 151 | check("explicit user zero disables the ordinary stream ceiling without restarting"); |
| 152 | assert.equal(typeof await invoke(page, "Version"), "string"); |
| 153 | assert.equal(report.errors.length, 0, report.errors.join("\n")); |
| 154 | assert.equal(await page.locator("[data-crash-overlay], .error-boundary").count(), 0); |
| 155 | await page.locator("#composer-input").fill("Attachment smoke remains responsive"); |
| 156 | assert.equal(await page.locator("#composer-input").inputValue(), "Attachment smoke remains responsive"); |
| 157 | await page.screenshot({ path: join(home, "attachments.png") }); |
| 158 | check("renderer and service remain responsive after large-file uploads and limit rejection"); |
| 159 | } finally { |
| 160 | await shell.close(); |
| 161 | writeFileSync(join(home, "attachment-smoke-results.json"), JSON.stringify(report, null, 2)); |
| 162 | console.log(`Evidence: ${home}`); |
| 163 | } |
| 164 |