Download tasks/linkedin/environment/reference/clone/tests/sqlite-contract.test.mjs from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 4.87 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/environment/reference/clone/tests/sqlite-contract.test.mjs
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/environment/reference/clone/tests/sqlite-contract.test.mjs
-
curl -L -o sqlite-contract.test.mjs https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/environment/reference/clone/tests/sqlite-contract.test.mjs
4.87 kB
| import assert from "node:assert/strict"; | |
| import { execFile } from "node:child_process"; | |
| import { mkdtemp, readFile, readdir, rm } from "node:fs/promises"; | |
| import { tmpdir } from "node:os"; | |
| import { join } from "node:path"; | |
| import test from "node:test"; | |
| import { DatabaseSync } from "node:sqlite"; | |
| import { promisify } from "node:util"; | |
| const root = process.cwd(); | |
| const execFileAsync = promisify(execFile); | |
| async function applyMigrations(path) { | |
| const sqlite = new DatabaseSync(path); | |
| sqlite.exec("PRAGMA foreign_keys = ON;"); | |
| const files = (await readdir(join(root, "drizzle"))).filter((file) => file.endsWith(".sql")).sort(); | |
| for (const file of files) { | |
| const source = await readFile(join(root, "drizzle", file), "utf8"); | |
| for (const statement of source.split("--> statement-breakpoint")) { | |
| if (statement.trim()) sqlite.exec(statement); | |
| } | |
| } | |
| sqlite.close(); | |
| } | |
| test("SQLite migrations seed the default tenant and D1 batch rolls back partial account creation", async () => { | |
| const directory = await mkdtemp(join(tmpdir(), "linkedin-d1-contract-")); | |
| const path = join(directory, "linkedin.sqlite3"); | |
| try { | |
| await applyMigrations(path); | |
| process.env.LINKEDIN_SQLITE_PATH = path; | |
| const [{ getDb }, schema] = await Promise.all([import("../db/index.ts"), import("../db/schema.ts")]); | |
| const db = getDb(); | |
| const tenants = await db.select().from(schema.organizations); | |
| const users = await db.select().from(schema.users); | |
| const posts = await db.select().from(schema.posts); | |
| assert.ok(tenants.some((tenant) => tenant.slug === "northstar-systems")); | |
| assert.ok(users.some((user) => user.email === "maya@example.com")); | |
| assert.ok(posts.some((post) => post.id === "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa")); | |
| assert.ok(users.find((user) => user.email === "maya@example.com")?.emailVerifiedAt instanceof Date); | |
| const rollbackUserId = "01234567-89ab-4cde-8fab-0123456789ab"; | |
| await assert.rejects(db.batch([ | |
| db.insert(schema.users).values({ | |
| id: rollbackUserId, | |
| email: "rollback@example.test", | |
| username: "rollback-test", | |
| passwordHash: "not-used", | |
| emailVerifiedAt: new Date(), | |
| name: "Rollback Test", | |
| }), | |
| db.insert(schema.memberships).values({ | |
| organizationId: "missing-tenant", | |
| userId: rollbackUserId, | |
| role: "member", | |
| }), | |
| ])); | |
| const sqlite = new DatabaseSync(path, { readOnly: true }); | |
| assert.equal(sqlite.prepare("SELECT count(*) AS count FROM users WHERE id = ?").get(rollbackUserId).count, 0); | |
| sqlite.close(); | |
| } finally { | |
| delete process.env.LINKEDIN_SQLITE_PATH; | |
| await rm(directory, { recursive: true, force: true }); | |
| } | |
| }); | |
| test("Harbor migrations freeze seeded and default timestamps across independent databases", async () => { | |
| const directory = await mkdtemp(join(tmpdir(), "linkedin-deterministic-time-")); | |
| const firstPath = join(directory, "first.sqlite3"); | |
| const secondPath = join(directory, "second.sqlite3"); | |
| const deterministicEpoch = 1767189679; | |
| try { | |
| for (const path of [firstPath, secondPath]) { | |
| await execFileAsync(process.execPath, [join(root, "scripts", "migrate-sqlite.mjs")], { | |
| cwd: root, | |
| env: { | |
| PATH: process.env.PATH, | |
| LINKEDIN_SQLITE_PATH: path, | |
| WEBSITEBENCH_DETERMINISTIC_EPOCH_SECONDS: String(deterministicEpoch), | |
| }, | |
| }); | |
| const sqlite = new DatabaseSync(path); | |
| sqlite.prepare("INSERT INTO organizations (id, name, slug) VALUES (?, ?, ?)") | |
| .run("12345678-1234-4123-8123-123456789abc", "Deterministic Test", "deterministic-test"); | |
| assert.deepEqual( | |
| { ...sqlite.prepare("SELECT created_at, updated_at FROM organizations WHERE id = ?").get("55555555-5555-4555-8555-555555555555") }, | |
| { created_at: 1764547200, updated_at: 1764547200 }, | |
| ); | |
| assert.deepEqual( | |
| { ...sqlite.prepare("SELECT created_at, updated_at FROM posts WHERE id = ?").get("aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa") }, | |
| { created_at: 1764547200, updated_at: 1764547200 }, | |
| ); | |
| assert.deepEqual( | |
| { ...sqlite.prepare("SELECT published_at, created_at, updated_at FROM jobs WHERE id = ?").get("77777777-7777-4777-8777-777777777777") }, | |
| { published_at: 1764547200, created_at: 1764547200, updated_at: 1764547200 }, | |
| ); | |
| assert.deepEqual( | |
| { ...sqlite.prepare("SELECT created_at, updated_at FROM organizations WHERE slug = ?").get("deterministic-test") }, | |
| { created_at: deterministicEpoch, updated_at: deterministicEpoch }, | |
| ); | |
| assert.equal( | |
| sqlite.prepare("SELECT min(applied_at) AS applied_at FROM __drizzle_migrations").get().applied_at, | |
| deterministicEpoch, | |
| ); | |
| sqlite.close(); | |
| } | |
| } finally { | |
| await rm(directory, { recursive: true, force: true }); | |
| } | |
| }); | |