// Exercise the packed integration in an isolated app with real Astro and at-astro. import assert from "node:assert/strict"; import { cp, mkdtemp, readdir, realpath, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; const root = fileURLToPath(new URL("../", import.meta.url)); const directory = await realpath(await mkdtemp(join(tmpdir(), "folio-astro-consumer-"))); const client = `file:${join(root, "target/packages/folio-client-0.1.0.tgz")}`; const adapter = `file:${join(root, "target/packages/folio-at-astro-0.1.0.tgz")}`; const react = `file:${join(root, "target/packages/folio-react-query-0.1.0.tgz")}`; const workspace = await Bun.file(join(root, "package.json")).json(); const clientManifest = await Bun.file(join(root, "packages/client/package.json")).json(); try { await cp(join(root, "packages/at-astro/test/app"), directory, { recursive: true }); await Bun.write(join(directory, "package.json"), JSON.stringify({ name: "folio-astro-consumer-smoke", private: true, type: "module", dependencies: { "@folio/client": client, "@folio/at-astro": adapter, "@folio/react-query": react, "at-astro": "1.1.1", "@atproto/lex": "^0.3.6", ...Object.fromEntries(["@atproto/lex-data", "@atproto/lex-json"].map((name) => [name, clientManifest.dependencies[name]])), ...Object.fromEntries(["astro", "@astrojs/node", "react", "react-dom", "@tanstack/react-query", "@ipld/dag-cbor", "multiformats", "@atproto/lex-schema"] .map((name) => [name, workspace.devDependencies[name]])), }, devDependencies: Object.fromEntries(["typescript", "@types/react", "@types/react-dom"].map((name) => [name, workspace.devDependencies[name]])), overrides: { "@folio/client": client }, })); const browser = await Bun.file(join(root, "packages/react-query/test/browser.tsx")).text(); await Bun.write(join(directory, "src/browser.tsx"), browser.replace("../../client/test/fixture.js", "./fixture.js").replace("../../client/test/generated/", "./generated/")); const fixture = await Bun.file(join(root, "packages/client/test/fixture.ts")).text(); await Bun.write(join(directory, "src/fixture.ts"), fixture.replace("../src/index.js", "@folio/client")); await cp(join(root, "packages/client/test/generated"), join(directory, "src/generated"), { recursive: true }); const config = await Bun.file(join(root, "tsconfig.json")).json(); await Bun.write(join(directory, "tsconfig.json"), JSON.stringify({ compilerOptions: config.compilerOptions, include: ["src"] })); const install = Bun.spawn([Bun.which("bun")!, "install"], { cwd: directory, stdout: "inherit", stderr: "inherit" }); assert.equal(await install.exited, 0, "Isolated Astro consumer installation failed"); const check = Bun.spawn([Bun.which("bun")!, "run", "tsc", "--noEmit"], { cwd: directory, stdout: "inherit", stderr: "inherit" }); assert.equal(await check.exited, 0, "Isolated consumer TypeScript check failed"); const { build, preview } = await import(pathToFileURL(join(directory, "node_modules/astro/dist/index.js")).href) as typeof import("astro"); async function verify(origin: string) { async function post(route: string, mime: string, body: string | Uint8Array, path: string) { const endpoint = new URL(`/folio-smoke/_folio/${route}/?did=did%3Aplc%3Aexample`, origin); const headers = { "Content-Type": mime, Origin: origin }; const response = await fetch(endpoint, { method: "POST", headers, body, redirect: "error" }); assert.equal(response.status, 202); assert.equal(response.headers.get("X-Folio-Pds"), "mocked"); assert.equal(response.headers.has("Content-Encoding"), false); assert.deepEqual(await response.json(), { path, method: "POST", headers: { "content-type": mime }, body: typeof body === "string" ? body : [...body], signal: true, }); for (const [url, requestHeaders, status] of [ [endpoint, { ...headers, Origin: "https://other.example" }, 403], [endpoint, { "Content-Type": mime }, 403], [new URL("?did=did:plc:other", endpoint), headers, 403], [endpoint, { ...headers, "x-folio-session": "signed-out" }, 401], ] as const) { assert.equal((await fetch(url, { method: "POST", headers: requestHeaders, body, redirect: "error" })).status, status); } } await post("write", "application/json", '{\n "repo": "did:plc:example", "writes": []\n}\n', "/xrpc/com.atproto.repo.applyWrites"); const bytes = new Uint8Array([0, 255, 128, 7]); await post("blob", "image/png", bytes, "/xrpc/com.atproto.repo.uploadBlob"); const cid = "bafkreibjfgx2gprinfvicegelk5kosd6y2frmqpqzwqkg7usac74l3t2v4"; const endpoint = new URL(`/folio-smoke/_folio/blob/?${new URLSearchParams({ did: "did:plc:example", cid })}`, origin); const response = await fetch(endpoint, { redirect: "error" }); assert.equal(response.status, 200); assert.equal(response.headers.get("Content-Type"), "image/png"); assert.equal(response.headers.has("Content-Encoding"), false); assert.equal(response.headers.get("X-Content-Type-Options"), "nosniff"); assert.equal(response.headers.get("Content-Security-Policy"), "default-src 'none'; sandbox"); assert.equal(response.headers.get("X-Folio-Signal"), "true"); assert.equal(response.headers.get("X-Folio-Method"), "GET"); assert.deepEqual(new Uint8Array(await response.arrayBuffer()), bytes); const upstream = new URL(response.headers.get("X-Folio-Path")!, "https://pds.example"); assert.equal(upstream.pathname, "/xrpc/com.atproto.sync.getBlob"); assert.equal(upstream.searchParams.get("did"), "did:plc:example"); assert.equal(upstream.searchParams.get("cid"), cid); assert.equal((await fetch(new URL(`?did=did:plc:other&cid=${cid}`, endpoint))).status, 403); assert.equal((await fetch(endpoint, { headers: { "x-folio-session": "signed-out" } })).status, 401); assert.equal((await fetch(new URL("/folio-smoke/", origin))).status, 200); } await build({ root: directory, logLevel: "error" }); const wasm = (await readdir(join(directory, "dist/client"), { recursive: true })).find((name) => name.endsWith(".wasm")); assert.ok(wasm, "Astro browser build must emit the Wasm asset"); const production = await preview({ root: directory, logLevel: "warn" }); try { const origin = `http://127.0.0.1:${production.port}`; await verify(origin); const asset = await fetch(new URL(`/folio-smoke/${wasm}`, origin)); assert.equal(asset.status, 200); assert.equal(asset.headers.get("Content-Type"), "application/wasm"); await WebAssembly.compile(await asset.arrayBuffer()); console.log("Packed packages: TypeScript, React/Astro build, served Wasm, authenticated writes, and binary blobs passed."); if (Bun.argv.includes("--serve")) { console.log(new URL("/folio-smoke/", origin).href); await new Promise((resolve) => { process.once("SIGINT", resolve); process.once("SIGTERM", resolve); }); } } finally { await production.stop(); } } finally { await rm(directory, { recursive: true, force: true }); }