Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

run-tauri-e2e.ts

166 lines4.1 KB

1import { createServer } from 'node:http'
2import { join } from 'node:path'
3import { setTimeout as delay } from 'node:timers/promises'
4import { spawn } from 'node:child_process'
5​
6type TauriE2ERunResult =
7 | {
8 kind: `result`
9 status: `passed`
10 payload: unknown
11 }
12 | {
13 kind: `result`
14 status: `failed`
15 payload: unknown
16 }
17​
18function isTauriE2ERunResult(
19 value:
20 | TauriE2ERunResult
21 | { kind: `status`; phase: string; details?: unknown }
22 | null,
23): value is TauriE2ERunResult {
24 return value?.kind === `result`
26​
27function createOutputCollector() {
28 let output = ``
29​
30 return {
31 append(chunk: string | Buffer) {
32 output += chunk.toString()
33 if (output.length > 20_000) {
34 output = output.slice(-20_000)
35 }
36 },
37 getOutput() {
38 return output
39 },
40 }
42​
43export async function runTauriPersistedCollectionE2E(options?: {
44 timeoutMs?: number
45}): Promise<TauriE2ERunResult> {
46 const timeoutMs = options?.timeoutMs ?? 180_000
47 const runId = Date.now().toString(36)
48 const appDirectory = join(process.cwd(), `e2e`, `app`)
49 let latestMessage:
50 | TauriE2ERunResult
51 | {
52 kind: `status`
53 phase: string
54 details?: unknown
55 }
56 | null = null
57​
58 const reportServer = createServer((request, response) => {
59 response.setHeader(`Access-Control-Allow-Origin`, `*`)
60 response.setHeader(`Access-Control-Allow-Methods`, `POST, OPTIONS`)
61 response.setHeader(`Access-Control-Allow-Headers`, `content-type`)
62​
63 if (request.method === `OPTIONS`) {
64 response.statusCode = 204
65 response.end()
66 return
67 }
68​
69 if (request.method !== `POST`) {
70 response.statusCode = 404
71 response.end()
72 return
73 }
74​
75 const chunks: Array<Buffer> = []
76 request.on(`data`, (chunk) => {
77 chunks.push(Buffer.from(chunk))
78 })
79 request.on(`end`, () => {
80 try {
81 latestMessage = JSON.parse(Buffer.concat(chunks).toString(`utf8`)) as
82 | TauriE2ERunResult
83 | {
84 kind: `status`
85 phase: string
86 details?: unknown
87 }
88 response.statusCode = 204
89 response.end()
90 } catch {
91 response.statusCode = 400
92 response.end()
93 }
94 })
95 })
96​
97 await new Promise<void>((resolve, reject) => {
98 reportServer.listen(0, `127.0.0.1`, () => resolve())
99 reportServer.once(`error`, reject)
100 })
101​
102 const reportPort = (
103 reportServer.address() as {
104 port: number
105 }
106 ).port
107 const reportUrl = `http://127.0.0.1:${String(reportPort)}`
108​
109 const stdoutCollector = createOutputCollector()
110 const stderrCollector = createOutputCollector()
111​
112 const child = spawn(`pnpm`, [`exec`, `tauri`, `dev`, `--no-watch`], {
113 cwd: appDirectory,
114 env: {
115 ...process.env,
116 VITE_TANSTACK_DB_TAURI_E2E_RUN_ID: runId,
117 VITE_TANSTACK_DB_TAURI_E2E_REPORT_URL: reportUrl,
118 },
119 stdio: [`ignore`, `pipe`, `pipe`],
120 detached: true,
121 })
122​
123 child.stdout.on(`data`, (chunk) => {
124 stdoutCollector.append(chunk)
125 })
126 child.stderr.on(`data`, (chunk) => {
127 stderrCollector.append(chunk)
128 })
129​
130 const startedAt = Date.now()
131​
132 try {
133 while (Date.now() - startedAt < timeoutMs) {
134 if (child.exitCode !== null) {
135 throw new Error(
136 `Tauri e2e process exited before producing results.\nLatest message: ${JSON.stringify(latestMessage)}\nSTDOUT:\n${stdoutCollector.getOutput()}\nSTDERR:\n${stderrCollector.getOutput()}`,
137 )
138 }
139​
140 if (isTauriE2ERunResult(latestMessage)) {
141 return latestMessage
142 }
143​
144 await delay(1_000)
145 }
146​
147 throw new Error(
148 `Timed out waiting for Tauri e2e results.\nLatest message: ${JSON.stringify(latestMessage)}\nSTDOUT:\n${stdoutCollector.getOutput()}\nSTDERR:\n${stderrCollector.getOutput()}`,
149 )
150 } finally {
151 try {
152 if (typeof child.pid === `number`) {
153 process.kill(-child.pid, `SIGTERM`)
154 }
155 } catch {
156 try {
157 child.kill(`SIGTERM`)
158 } catch {}
159 }
160​
161 await delay(1_000).catch(() => {})
162 await new Promise<void>((resolve) => {
163 reportServer.close(() => resolve())
164 })
165 }