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

main.ts

142 lines3.6 KB

1import { getPersistedConformanceTestNames } from '../../../../db-collection-e2e/src/fixtures/persisted-conformance-manifest'
2import { createNativeTauriSQLiteTestDatabase } from './native-tauri-sql-test-db'
3import { registerTauriNativeE2ESuite } from './register-tauri-e2e-suite'
4import {
5 getRegisteredTestCount,
6 resetRegisteredTests,
7 runRegisteredTests,
8} from './runtime-vitest'
9​
10const statusElement = document.querySelector(`#status`) as HTMLParagraphElement
11const detailsElement = document.querySelector(`#details`) as HTMLPreElement
12const runtimeRunId =
13 import.meta.env.VITE_TANSTACK_DB_TAURI_E2E_RUN_ID ?? Date.now().toString(36)
14const reportUrl = import.meta.env.VITE_TANSTACK_DB_TAURI_E2E_REPORT_URL
15const suiteName = `tauri persisted collection conformance`
16​
17function setStatus(status: string, details?: unknown): void {
18 statusElement.textContent = status
19 if (details !== undefined) {
20 detailsElement.textContent = JSON.stringify(details, null, 2)
21 }
23​
24async function postHarnessMessage(
25 payload: Record<string, unknown>,
26): Promise<void> {
27 if (!reportUrl) {
28 return
29 }
30​
31 await fetch(reportUrl, {
32 method: `POST`,
33 headers: {
34 'content-type': `application/json`,
35 },
36 body: JSON.stringify(payload),
37 })
39​
40async function reportRunResult(result: {
41 status: `passed` | `failed`
42 payload: unknown
43}): Promise<void> {
44 await postHarnessMessage({
45 kind: `result`,
46 ...result,
47 })
49​
50async function reportStatus(phase: string, details?: unknown): Promise<void> {
51 await postHarnessMessage({
52 kind: `status`,
53 phase,
54 details,
55 })
57​
58async function run(): Promise<void> {
59 setStatus(`Starting Tauri e2e runtime`)
60​
61 try {
62 await reportStatus(`starting`, { runId: runtimeRunId })
63 const database = await createNativeTauriSQLiteTestDatabase({
64 runId: runtimeRunId,
65 })
66 await reportStatus(`database-loaded`)
67​
68 resetRegisteredTests()
69 registerTauriNativeE2ESuite({
70 suiteName,
71 database,
72 runId: runtimeRunId,
73 })
74 await reportStatus(`suite-registered`)
75​
76 const totalTests = getRegisteredTestCount()
77 setStatus(`Running Tauri e2e suite`, {
78 totalTests,
79 runId: runtimeRunId,
80 })
81 await reportStatus(`tests-starting`, {
82 totalTests,
83 })
84​
85 const result = await runRegisteredTests({
86 expectedTestNames: getPersistedConformanceTestNames(suiteName),
87 onTestStart: ({ index, name, total }) => {
88 setStatus(`Running test ${String(index)}/${String(total)}`, {
89 currentTest: name,
90 })
91 },
92 })
93 await reportStatus(`tests-finished`, {
94 total: result.total,
95 failed: result.failed,
96 })
97​
98 const failedResults = result.results.filter(
99 (entry) => entry.status === `failed`,
100 )
101 const summary = {
102 ...result,
103 failures: failedResults,
104 runtime: `tauri`,
105 provider: `@tauri-apps/plugin-sql`,
106 runId: runtimeRunId,
107 }
108​
109 if (!result.complete || result.failed > 0) {
110 await reportRunResult({
111 status: `failed`,
112 payload: summary,
113 })
114 setStatus(`Tauri e2e failed`, summary)
115 return
116 }
117​
118 await reportRunResult({
119 status: `passed`,
120 payload: summary,
121 })
122 setStatus(`Tauri e2e passed`, summary)
123 } catch (error) {
124 const message = error instanceof Error ? error.message : String(error)
125 const payload = {
126 error: message,
127 runId: runtimeRunId,
128 step: statusElement.textContent,
129 }
130​
131 try {
132 await reportRunResult({
133 status: `failed`,
134 payload,
135 })
136 } catch {}
137​
138 setStatus(`Tauri e2e failed: ${message}`, payload)
139 }
141​
142void run()