Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Compiles and runs the native dependency regression directly.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6import * as NodeURL from "node:url";
7import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test";
8
9// oxlint-disable-next-line t3code/no-global-process-runtime -- This native integration test uses the host Swift compiler.
10describe.skipIf(NodeOS.platform() !== "darwin")("NetworkModule native path handoff", () => {
11 let directory: string;
12 let executable: string;
13
14 beforeAll(() => {
15 directory = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-network-test-"));
16 executable = NodePath.join(directory, "network-regression");
17 const source = NodeFS.readFileSync(
18 new URL("../node_modules/expo-network/ios/NetworkModule.swift", import.meta.url),
19 "utf8",
20 );
21 // Compile the dependency's query and conversion methods, replacing only
22 // NWPathMonitor so we can deliver updates during and after cancellation.
23 const start = source.indexOf(" private func getNetworkPathAsync()");
24 if (start === -1) throw new Error("NetworkModule query implementation not found");
25 const methods = source
26 .slice(start, source.lastIndexOf("\n}"))
27 .replace("private func getNetworkStateAsync", "func getNetworkStateAsync");
28 const module = NodePath.join(directory, "NetworkModule.swift");
29 NodeFS.writeFileSync(
30 module,
31 `import Foundation\nfinal class NetworkModule {\n private let monitorQueue = DispatchQueue.global(qos: .default)\n${methods}\n}\n`,
32 );
33 const types = NodePath.join(directory, "NetworkType.swift");
34 NodeFS.writeFileSync(
35 types,
36 NodeFS.readFileSync(
37 new URL("../node_modules/expo-network/ios/NetworkType.swift", import.meta.url),
38 "utf8",
39 ).replace(/^import Network\n/m, ""),
40 );
41 NodeChildProcess.execFileSync(
42 "swiftc",
43 [
44 "-swift-version",
45 "5",
46 "-sanitize=thread",
47 module,
48 types,
49 NodeURL.fileURLToPath(new URL("./fixtures/NetworkModuleRegression.swift", import.meta.url)),
50 "-o",
51 executable,
52 ],
53 { timeout: 30_000, encoding: "utf8" },
54 );
55 });
56
57 afterAll(() => {
58 if (directory) NodeFS.rmSync(directory, { recursive: true, force: true });
59 });
60
61 it.each([
62 ["first", "keeps the first path when several updates arrive before the query resumes"],
63 ["offline", "reports a disconnected path"],
64 ["concurrent", "reads paths during overlapping queries and late callbacks without data races"],
65 ])("%s: %s", (name) => {
66 expect(
67 NodeChildProcess.execFileSync(executable, [name], {
68 encoding: "utf8",
69 timeout: 15_000,
70 env: { ...process.env, TSAN_OPTIONS: "halt_on_error=1" },
71 }).trim(),
72 ).toBe("passed");
73 });
74});