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

main.ts

155 lines4.2 KB

1import { Capacitor } from '@capacitor/core'
2import { CapacitorSQLite, SQLiteConnection } from '@capacitor-community/sqlite'
3import { getPersistedConformanceTestNames } from '../../../../db-collection-e2e/src/fixtures/persisted-conformance-manifest'
4import { createNativeCapacitorSQLiteTestDatabaseFactory } from './native-capacitor-sqlite-test-db'
5import { registerCapacitorNativeE2ESuite } from './register-capacitor-e2e-suite'
6import {
7 getRegisteredTestCount,
8 resetRegisteredTests,
9 runRegisteredTests,
10} from './runtime-vitest'
11​
12const statusElement = document.querySelector(`#status`) as HTMLParagraphElement
13const detailsElement = document.querySelector(`#details`) as HTMLPreElement
14const runtimeRunId =
15 import.meta.env.VITE_TANSTACK_DB_CAPACITOR_E2E_RUN_ID ??
16 Date.now().toString(36)
17const resultsDatabaseName = `tanstack_db_capacitor_e2e_results_${runtimeRunId}`
18const suiteName = `capacitor persisted collection conformance`
19​
20function setStatus(status: string, details?: unknown): void {
21 statusElement.textContent = status
22 if (details !== undefined) {
23 detailsElement.textContent = JSON.stringify(details, null, 2)
24 }
26​
27async function persistRunResult(
28 sqlite: SQLiteConnection,
29 result: {
30 status: `passed` | `failed`
31 payload: unknown
32 },
33): Promise<void> {
34 const resultsDatabase = await sqlite.createConnection(
35 resultsDatabaseName,
36 false,
37 `no-encryption`,
38 1,
39 false,
40 )
41​
42 try {
43 await resultsDatabase.open()
44 await resultsDatabase.execute(
45 `CREATE TABLE IF NOT EXISTS test_run_results (
46 id INTEGER PRIMARY KEY AUTOINCREMENT,
47 status TEXT NOT NULL,
48 payload_json TEXT NOT NULL
49 );`,
50 false,
51 )
52 await resultsDatabase.run(`DELETE FROM test_run_results`, [], false)
53 await resultsDatabase.run(
54 `INSERT INTO test_run_results (status, payload_json) VALUES (?, ?)`,
55 [result.status, JSON.stringify(result.payload)],
56 false,
57 )
58 } finally {
59 try {
60 await resultsDatabase.close()
61 } catch {}
62​
63 try {
64 await sqlite.closeConnection(resultsDatabaseName, false)
65 } catch {}
66 }
68​
69async function run(): Promise<void> {
70 const platform = Capacitor.getPlatform()
71 const sqlite = new SQLiteConnection(CapacitorSQLite)
72​
73 setStatus(`Starting native e2e runtime on ${platform}`)
74​
75 if (platform === `web`) {
76 setStatus(`Expected a native Capacitor runtime but got web`, {
77 platform,
78 runId: runtimeRunId,
79 })
80 return
81 }
82​
83 try {
84 resetRegisteredTests()
85 registerCapacitorNativeE2ESuite({
86 suiteName,
87 createDatabase: createNativeCapacitorSQLiteTestDatabaseFactory({
88 sqlite,
89 runId: runtimeRunId,
90 }),
91 })
92​
93 const totalTests = getRegisteredTestCount()
94 setStatus(`Running native e2e suite`, {
95 totalTests,
96 runId: runtimeRunId,
97 })
98​
99 const result = await runRegisteredTests({
100 expectedTestNames: getPersistedConformanceTestNames(suiteName),
101 onTestStart: ({ index, name, total }) => {
102 setStatus(`Running test ${String(index)}/${String(total)}`, {
103 currentTest: name,
104 })
105 },
106 })
107​
108 const failedResults = result.results.filter(
109 (entry) => entry.status === `failed`,
110 )
111 const summary = {
112 ...result,
113 failures: failedResults,
114 runtime: `capacitor`,
115 provider: `@capacitor-community/sqlite`,
116 platform,
117 runId: runtimeRunId,
118 }
119​
120 if (!result.complete || result.failed > 0) {
121 await persistRunResult(sqlite, {
122 status: `failed`,
123 payload: summary,
124 })
125 setStatus(`Native e2e failed`, summary)
126 return
127 }
128​
129 await persistRunResult(sqlite, {
130 status: `passed`,
131 payload: summary,
132 })
133 setStatus(`Native e2e passed`, summary)
134 } catch (error) {
135 const message = error instanceof Error ? error.message : String(error)
136​
137 try {
138 await persistRunResult(sqlite, {
139 status: `failed`,
140 payload: {
141 error: message,
142 runId: runtimeRunId,
143 step: statusElement.textContent,
144 },
145 })
146 } catch {}
147​
148 setStatus(`Native e2e failed: ${message}`, {
149 step: statusElement.textContent,
150 runId: runtimeRunId,
151 })
152 }
154​
155void run()