|
| 1 | +/** |
| 2 | + * Shared test utilities for integration tests |
| 3 | + * |
| 4 | + * This module provides common setup and teardown functions to avoid |
| 5 | + * code duplication across integration test files. |
| 6 | + * |
| 7 | + * Uses a shared database with per-test-suite branches for faster test isolation. |
| 8 | + */ |
| 9 | + |
| 10 | +import { WOQLClient } from "../index.js"; |
| 11 | +import { DbDetails } from "../dist/typescript/lib/typedef.js"; |
| 12 | + |
| 13 | +/** |
| 14 | + * Default server configuration for tests |
| 15 | + */ |
| 16 | +export const TEST_SERVER_URL = "http://127.0.0.1:6363"; |
| 17 | +export const TEST_USER = "admin"; |
| 18 | +export const TEST_ORG = "admin"; |
| 19 | +export const SHARED_TEST_DB = "db__integration_tests"; |
| 20 | + |
| 21 | +/** |
| 22 | + * Creates a WOQLClient configured for testing |
| 23 | + */ |
| 24 | +export function createTestClient(): WOQLClient { |
| 25 | + return new WOQLClient(TEST_SERVER_URL, { |
| 26 | + user: TEST_USER, |
| 27 | + organization: TEST_ORG, |
| 28 | + key: process.env.TDB_ADMIN_PASS ?? "root" |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Ensures the shared test database exists |
| 34 | + * Creates it if it doesn't exist, does nothing if it already exists |
| 35 | + */ |
| 36 | +export async function ensureSharedDatabase(client: WOQLClient): Promise<void> { |
| 37 | + try { |
| 38 | + // Try to list databases and check if shared db exists |
| 39 | + const dbs = await client.getDatabases(); |
| 40 | + const dbExists = dbs.some((db: any) => db.name === SHARED_TEST_DB || db.path === `${TEST_ORG}/${SHARED_TEST_DB}`); |
| 41 | + if (!dbExists) { |
| 42 | + throw new Error("Database not found"); |
| 43 | + } |
| 44 | + client.db(SHARED_TEST_DB); |
| 45 | + } catch (_e) { |
| 46 | + // Database doesn't exist, create it |
| 47 | + const dbObj: DbDetails = { |
| 48 | + label: SHARED_TEST_DB, |
| 49 | + comment: "Shared database for integration tests", |
| 50 | + schema: true |
| 51 | + }; |
| 52 | + await client.createDatabase(SHARED_TEST_DB, dbObj); |
| 53 | + client.db(SHARED_TEST_DB); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Cleans up a branch if it exists (safe to call if it doesn't exist) |
| 59 | + */ |
| 60 | +export async function cleanupBranch(client: WOQLClient, branchName: string): Promise<void> { |
| 61 | + try { |
| 62 | + await client.deleteBranch(branchName); |
| 63 | + } catch (_e) { |
| 64 | + // Branch doesn't exist, which is fine |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Sets up a test branch in the shared database |
| 70 | + * Cleans up any existing branch first, then creates a fresh one |
| 71 | + * |
| 72 | + * @param client - The WOQLClient instance |
| 73 | + * @param branchName - The name of the branch to create |
| 74 | + * @returns The result of branch() |
| 75 | + */ |
| 76 | +export async function setupTestBranch( |
| 77 | + client: WOQLClient, |
| 78 | + branchName: string |
| 79 | +): Promise<any> { |
| 80 | + // Ensure shared database exists |
| 81 | + await ensureSharedDatabase(client); |
| 82 | + |
| 83 | + // Clean up any existing branch from previous failed runs |
| 84 | + await cleanupBranch(client, branchName); |
| 85 | + |
| 86 | + // Create a new branch from empty (not from main, to get clean state) |
| 87 | + const result = await client.branch(branchName, true); |
| 88 | + |
| 89 | + // Switch to the new branch |
| 90 | + client.checkout(branchName); |
| 91 | + |
| 92 | + return result; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Tears down a test branch |
| 97 | + * Safe to call even if the branch doesn't exist |
| 98 | + */ |
| 99 | +export async function teardownTestBranch(client: WOQLClient, branchName: string): Promise<void> { |
| 100 | + // Switch back to main before deleting |
| 101 | + client.checkout("main"); |
| 102 | + await cleanupBranch(client, branchName); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Cleans up a database if it exists (safe to call if it doesn't exist) |
| 107 | + * This is useful for cleaning up leftover databases from failed test runs |
| 108 | + * |
| 109 | + * @param client - The WOQLClient instance |
| 110 | + * @param dbName - The name of the database to clean up |
| 111 | + */ |
| 112 | +export async function cleanupDatabase(client: WOQLClient, dbName: string): Promise<void> { |
| 113 | + try { |
| 114 | + await client.deleteDatabase(dbName); |
| 115 | + } catch (_e) { |
| 116 | + // Database doesn't exist, which is fine |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Sets up a test database with optional schema |
| 122 | + * Cleans up any existing database first, then creates a fresh one |
| 123 | + * |
| 124 | + * @param client - The WOQLClient instance |
| 125 | + * @param dbName - The name of the database to create |
| 126 | + * @param options - Optional configuration for the database |
| 127 | + * @returns The result of createDatabase |
| 128 | + */ |
| 129 | +export async function setupTestDatabase( |
| 130 | + client: WOQLClient, |
| 131 | + dbName: string, |
| 132 | + options: Partial<DbDetails> = {} |
| 133 | +): Promise<any> { |
| 134 | + // Clean up any existing database from previous failed runs |
| 135 | + await cleanupDatabase(client, dbName); |
| 136 | + |
| 137 | + // Set up the client to use this database |
| 138 | + client.db(dbName); |
| 139 | + |
| 140 | + // Create the database with default options merged with provided options |
| 141 | + const dbObj: DbDetails = { |
| 142 | + label: dbName, |
| 143 | + comment: options.comment || `Test database: ${dbName}`, |
| 144 | + schema: options.schema ?? true, |
| 145 | + ...options |
| 146 | + }; |
| 147 | + |
| 148 | + return client.createDatabase(dbName, dbObj); |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Tears down a test database |
| 153 | + * Safe to call even if the database doesn't exist |
| 154 | + * |
| 155 | + * @param client - The WOQLClient instance |
| 156 | + * @param dbName - The name of the database to delete |
| 157 | + */ |
| 158 | +export async function teardownTestDatabase(client: WOQLClient, dbName: string): Promise<void> { |
| 159 | + await cleanupDatabase(client, dbName); |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * Helper to run beforeAll setup for a standard integration test |
| 164 | + * Creates the client and cleans up any existing database |
| 165 | + * |
| 166 | + * @param dbName - The database name for this test suite |
| 167 | + * @returns Object with client that will be initialized |
| 168 | + */ |
| 169 | +export function createTestSetup(dbName: string): { |
| 170 | + client: WOQLClient; |
| 171 | + beforeAllFn: () => Promise<void>; |
| 172 | + afterAllFn: () => Promise<void>; |
| 173 | +} { |
| 174 | + let client: WOQLClient; |
| 175 | + |
| 176 | + return { |
| 177 | + get client() { |
| 178 | + return client; |
| 179 | + }, |
| 180 | + beforeAllFn: async () => { |
| 181 | + client = createTestClient(); |
| 182 | + client.db(dbName); |
| 183 | + await cleanupDatabase(client, dbName); |
| 184 | + }, |
| 185 | + afterAllFn: async () => { |
| 186 | + await cleanupDatabase(client, dbName); |
| 187 | + } |
| 188 | + }; |
| 189 | +} |
0 commit comments