|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { run, until, sleep, useAbortSignal, Err, Ok } from "effection"; |
| 3 | +import { execSync } from "child_process"; |
| 4 | +import { existsSync } from "fs"; |
| 5 | +import { useService } from "@simulacrum/server"; |
| 6 | + |
| 7 | +const AUTH0_PORT = 4400; |
| 8 | +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; |
| 9 | +const AUTH0_URL = `https://localhost:${AUTH0_PORT}`; |
| 10 | + |
| 11 | +// Ensure built distribution is present; if not, build it so the smoke test can run locally |
| 12 | +if (!existsSync("./dist/index.cjs")) { |
| 13 | + console.log("ci-smoke: dist not found, running `npm run build`..."); |
| 14 | + execSync("npm run prepack", { stdio: "inherit" }); |
| 15 | +} |
| 16 | + |
| 17 | +// Helper to start the built auth0 service with a wellness check (reused by tests) |
| 18 | +function startAuth0() { |
| 19 | + return useService("auth0", "node ./bin/start.cjs", { |
| 20 | + wellnessCheck: { |
| 21 | + timeout: 30000, |
| 22 | + *operation(_stdio) { |
| 23 | + const signal = yield* useAbortSignal(); |
| 24 | + const start = Date.now(); |
| 25 | + while (true) { |
| 26 | + try { |
| 27 | + yield* until( |
| 28 | + fetch(`${AUTH0_URL}/login`, { |
| 29 | + headers: { accept: "text/html" }, |
| 30 | + signal, |
| 31 | + }).then((r) => { |
| 32 | + if (!r.ok) throw new Error(`not ready: ${r.status}`); |
| 33 | + return true; |
| 34 | + }) |
| 35 | + ); |
| 36 | + return Ok<void>(void 0); |
| 37 | + } catch (err) { |
| 38 | + // ignore and retry |
| 39 | + } |
| 40 | + if (Date.now() - start > 30000) |
| 41 | + return Err(new Error("service did not start")); |
| 42 | + yield* sleep(200); |
| 43 | + } |
| 44 | + }, |
| 45 | + }, |
| 46 | + }); |
| 47 | +} |
| 48 | + |
| 49 | +describe("CI smoke: built dist server", () => { |
| 50 | + it("returns /login without escaped closing script tags", async () => { |
| 51 | + await run(function* () { |
| 52 | + yield* startAuth0(); |
| 53 | + |
| 54 | + const signal = yield* useAbortSignal(); |
| 55 | + const text = yield* until( |
| 56 | + fetch(`${AUTH0_URL}/login`, { signal }).then((r) => { |
| 57 | + if (!r.ok) throw new Error(`fetch failed: ${r.status}`); |
| 58 | + return r.text(); |
| 59 | + }) |
| 60 | + ); |
| 61 | + |
| 62 | + expect(text).toMatch(/<\/script>/); |
| 63 | + expect(text).not.toContain("<\\/script>"); |
| 64 | + }); |
| 65 | + }, 60000); |
| 66 | + |
| 67 | + it("returns /authorize?response_mode=web_message without escaped closing script tags", async () => { |
| 68 | + await run(function* () { |
| 69 | + yield* startAuth0(); |
| 70 | + |
| 71 | + const url = `${AUTH0_URL}/authorize?response_mode=web_message&redirect_uri=http://localhost:3000¤tUser=default`; |
| 72 | + const signal2 = yield* useAbortSignal(); |
| 73 | + const text = yield* until( |
| 74 | + fetch(url, { headers: { accept: "text/html" }, signal: signal2 }).then( |
| 75 | + (r) => { |
| 76 | + if (!r.ok) throw new Error(`fetch failed: ${r.status}`); |
| 77 | + return r.text(); |
| 78 | + } |
| 79 | + ) |
| 80 | + ); |
| 81 | + |
| 82 | + expect(text).toMatch(/<\/script>/); |
| 83 | + expect(text).not.toContain("<\\/script>"); |
| 84 | + }); |
| 85 | + }, 60000); |
| 86 | +}); |
0 commit comments