forked from vlcn-io/cr-sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdist.test.ts
More file actions
180 lines (150 loc) · 6.22 KB
/
dist.test.ts
File metadata and controls
180 lines (150 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env -S npx tsx
import { Console, Effect } from "effect";
import { Command } from "@effect/platform";
import { BunContext, BunRuntime } from "@effect/platform-bun";
import { existsSync, readdirSync } from "node:fs";
import { pathToSQLite } from "@effect-native/libsqlite";
import { Database } from "bun:sqlite";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
Database.setCustomSQLite(pathToSQLite);
const __dirname = dirname(fileURLToPath(import.meta.url));
const testBasicFunctionality = Effect.gen(function* () {
yield* Console.log("🧪 Testing basic package functionality...");
// Test 1: Check if main files exist
yield* Console.log("📁 Test 1: Checking main files...");
const requiredFiles = [
"package.json",
"flake.nix",
"index.js",
"index.d.ts",
"bin/libcrsql-extension-path.ts",
"scripts/build-production.ts",
"scripts/sync-version.ts",
"scripts/build-zig.sh",
"scripts/bundle-zig-lib.sh",
"build-macros.ts",
];
for (const file of requiredFiles) {
if (!existsSync(file)) {
yield* Effect.fail(`Required file missing: ${file}`);
}
yield* Console.log(` ✅ ${file}`);
}
// Test 2: Check Nix flake
yield* Console.log("\n🏗️ Test 2: Checking Nix flake...");
yield* Command.make("nix", "flake", "check", "--no-build").pipe(
Command.exitCode,
Effect.mapError(() => "Nix flake check failed")
);
yield* Console.log(" ✅ Nix flake is valid");
// Test 3: Check TypeScript compilation
yield* Console.log("\n📝 Test 3: Checking TypeScript compilation...");
yield* Command.make("npx", "tsc", "--noEmit", "index.d.ts").pipe(
Command.exitCode,
Effect.mapError(() => "TypeScript compilation failed"),
Effect.ignore // Ignore errors for now since we might not have all dependencies
);
yield* Console.log("✅ All basic tests passed!");
});
const testLibArtifacts = Effect.gen(function* () {
yield* Console.log("\n📦 Test 4: Checking lib/ artifacts...");
const libDir = resolve(__dirname, "lib");
if (!existsSync(libDir)) {
yield* Console.log(" ⚠️ lib/ directory does not exist - skipping artifact check");
return;
}
const files = readdirSync(libDir);
// Check for C/Rust artifacts (legacy)
const cRustArtifacts = files.filter(f =>
f.startsWith("crsqlite-") && !f.includes("-zig-") && (f.endsWith(".dylib") || f.endsWith(".so"))
);
// Check for Zig artifacts (new)
const zigArtifacts = files.filter(f =>
f.startsWith("crsqlite-zig-") && (f.endsWith(".dylib") || f.endsWith(".so"))
);
yield* Console.log(` Found ${cRustArtifacts.length} C/Rust artifact(s):`);
for (const f of cRustArtifacts) {
yield* Console.log(` ✅ ${f}`);
}
yield* Console.log(` Found ${zigArtifacts.length} Zig artifact(s):`);
for (const f of zigArtifacts) {
yield* Console.log(` ✅ ${f}`);
}
if (cRustArtifacts.length === 0 && zigArtifacts.length === 0) {
yield* Console.log(" ⚠️ No platform-specific artifacts found in lib/");
yield* Console.log(" Run 'npm run bundle-lib' for C/Rust artifacts");
yield* Console.log(" Run 'npm run bundle-lib:zig' for Zig artifacts");
}
});
const testZigBuildAvailable = Effect.gen(function* () {
yield* Console.log("\n🔧 Test 5: Checking Zig build artifacts...");
const zigOutDir = resolve(__dirname, "zig/zig-out/lib");
const zigOutUniversal = resolve(__dirname, "zig/zig-out-universal/lib");
const zigOutArm64 = resolve(__dirname, "zig/zig-out-arm64/lib");
const zigOutX64 = resolve(__dirname, "zig/zig-out-x64/lib");
const checkDir = (dir: string, label: string) => {
if (existsSync(dir)) {
const files = readdirSync(dir).filter(f => f.includes("crsqlite"));
if (files.length > 0) {
return { exists: true, files, label };
}
}
return { exists: false, files: [], label };
};
const locations = [
checkDir(zigOutDir, "zig-out (native)"),
checkDir(zigOutUniversal, "zig-out-universal (macOS universal)"),
checkDir(zigOutArm64, "zig-out-arm64 (macOS ARM64)"),
checkDir(zigOutX64, "zig-out-x64 (macOS x64)"),
];
const available = locations.filter(l => l.exists);
if (available.length === 0) {
yield* Console.log(" ⚠️ No Zig build outputs found");
yield* Console.log(" Run 'npm run build:zig' to build for current platform");
yield* Console.log(" Run 'npm run build:zig darwin' to build macOS universal binary");
} else {
yield* Console.log(` Found ${available.length} Zig build location(s):`);
for (const loc of available) {
yield* Console.log(` ✅ ${loc.label}: ${loc.files.join(", ")}`);
}
}
});
const testLoaderSelection = Effect.gen(function* () {
yield* Console.log("\n🎯 Test 6: Checking loader selection logic...");
// Import the loader dynamically to test it
const loader = yield* Effect.tryPromise(() => import("./index.js"));
// Check that the exports exist
if (typeof loader.getExtensionPath !== "function") {
yield* Effect.fail("getExtensionPath is not a function");
}
yield* Console.log(" ✅ getExtensionPath function exported");
if (typeof loader.PREFER_IMPLEMENTATION !== "string") {
yield* Effect.fail("PREFER_IMPLEMENTATION is not exported");
}
yield* Console.log(` ✅ PREFER_IMPLEMENTATION = "${loader.PREFER_IMPLEMENTATION}"`);
// Try to get extension path (may fail if no artifacts present)
try {
const path = loader.getExtensionPath();
yield* Console.log(` ✅ Extension path resolved: ${path}`);
} catch (e: unknown) {
const msg = e instanceof Error ? e.message : String(e);
yield* Console.log(` ⚠️ Extension path not found (expected if no artifacts): ${msg.split('.')[0]}`);
}
});
const main = Effect.gen(function* () {
yield* Console.log("🚀 Running @effect-native/cr-sqlite package tests...\n");
yield* testBasicFunctionality;
yield* testLibArtifacts;
yield* testZigBuildAvailable;
yield* testLoaderSelection;
yield* Console.log("\n🎉 All tests completed!");
});
// Run when called directly
if (import.meta.main) {
const program = main.pipe(
Effect.provide(BunContext.layer),
Effect.catchAll((error) => Console.error(`Tests failed: ${error}`))
);
BunRuntime.runMain(program);
}