|
| 1 | +/* |
| 2 | +* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | +*/ |
| 4 | + |
| 5 | +/** |
| 6 | + * TypeScript type checking tests for connection-related methods. |
| 7 | + * |
| 8 | + * This file validates: |
| 9 | + * - checkConnection() return type |
| 10 | + * - release() method |
| 11 | + * - releaseClient() standalone function |
| 12 | + * |
| 13 | + * These tests are compiled but NOT executed - they verify type correctness only. |
| 14 | + * Run with: npm run test:types |
| 15 | + */ |
| 16 | + |
| 17 | +// Create test types that should match the actual marklogic types |
| 18 | +type ConnectionCheckResult = { |
| 19 | + connected: boolean; |
| 20 | + httpStatusCode?: number; |
| 21 | + httpStatusMessage?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +type DatabaseClient = { |
| 25 | + checkConnection(): Promise<ConnectionCheckResult>; |
| 26 | + release(): void; |
| 27 | +}; |
| 28 | + |
| 29 | +type DatabaseClientConfig = { |
| 30 | + host?: string; |
| 31 | + port?: number; |
| 32 | + user?: string; |
| 33 | + password?: string; |
| 34 | +}; |
| 35 | + |
| 36 | +// Simulate the marklogic module interface |
| 37 | +type MarkLogicModule = { |
| 38 | + createDatabaseClient(config: DatabaseClientConfig): DatabaseClient; |
| 39 | + releaseClient(client: DatabaseClient): void; |
| 40 | +}; |
| 41 | + |
| 42 | +// Test checkConnection() return type |
| 43 | +async function testCheckConnection(marklogic: MarkLogicModule) { |
| 44 | + const client = marklogic.createDatabaseClient({ |
| 45 | + host: 'localhost', |
| 46 | + port: 8000, |
| 47 | + user: 'admin', |
| 48 | + password: 'admin' |
| 49 | + }); |
| 50 | + |
| 51 | + // Should return a Promise<ConnectionCheckResult> |
| 52 | + const result = await client.checkConnection(); |
| 53 | + |
| 54 | + // result.connected should be boolean |
| 55 | + const isConnected: boolean = result.connected; |
| 56 | + |
| 57 | + // When not connected, should have optional error properties |
| 58 | + if (!result.connected) { |
| 59 | + const statusCode: number | undefined = result.httpStatusCode; |
| 60 | + const statusMessage: string | undefined = result.httpStatusMessage; |
| 61 | + |
| 62 | + console.log(`Connection failed: ${statusCode} - ${statusMessage}`); |
| 63 | + } |
| 64 | + |
| 65 | + return isConnected; |
| 66 | +} |
| 67 | + |
| 68 | +// Test release() method on client |
| 69 | +function testRelease(marklogic: MarkLogicModule) { |
| 70 | + const client = marklogic.createDatabaseClient({ |
| 71 | + host: 'localhost', |
| 72 | + port: 8000, |
| 73 | + user: 'admin', |
| 74 | + password: 'admin' |
| 75 | + }); |
| 76 | + |
| 77 | + // Should be callable with no return value |
| 78 | + client.release(); |
| 79 | + |
| 80 | + // This is the correct usage pattern |
| 81 | +} |
| 82 | + |
| 83 | +// Test releaseClient() standalone function |
| 84 | +function testReleaseClientFunction(marklogic: MarkLogicModule) { |
| 85 | + const client = marklogic.createDatabaseClient({ |
| 86 | + host: 'localhost', |
| 87 | + port: 8000, |
| 88 | + user: 'admin', |
| 89 | + password: 'admin' |
| 90 | + }); |
| 91 | + |
| 92 | + // Should accept a DatabaseClient and return void |
| 93 | + marklogic.releaseClient(client); |
| 94 | + |
| 95 | + // This is equivalent to client.release() |
| 96 | +} |
| 97 | + |
| 98 | +// Test proper cleanup pattern |
| 99 | +async function testProperCleanupPattern(marklogic: MarkLogicModule) { |
| 100 | + const client = marklogic.createDatabaseClient({ |
| 101 | + host: 'localhost', |
| 102 | + port: 8000, |
| 103 | + user: 'admin', |
| 104 | + password: 'admin' |
| 105 | + }); |
| 106 | + |
| 107 | + try { |
| 108 | + const result = await client.checkConnection(); |
| 109 | + if (result.connected) { |
| 110 | + console.log('Connected successfully!'); |
| 111 | + // Do database operations... |
| 112 | + } else { |
| 113 | + console.error(`Connection failed: ${result.httpStatusCode}`); |
| 114 | + } |
| 115 | + } finally { |
| 116 | + // Always clean up resources |
| 117 | + client.release(); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// Test ConnectionCheckResult structure |
| 122 | +const successResult: ConnectionCheckResult = { |
| 123 | + connected: true |
| 124 | +}; |
| 125 | + |
| 126 | +const failureResult: ConnectionCheckResult = { |
| 127 | + connected: false, |
| 128 | + httpStatusCode: 401, |
| 129 | + httpStatusMessage: 'Unauthorized' |
| 130 | +}; |
| 131 | + |
| 132 | +// These should cause errors if uncommented: |
| 133 | +// const invalidResult1: ConnectionCheckResult = { |
| 134 | +// connected: 'yes' // Error: should be boolean |
| 135 | +// }; |
| 136 | + |
| 137 | +// const invalidResult2: ConnectionCheckResult = { |
| 138 | +// connected: true, |
| 139 | +// httpStatusCode: 'error' // Error: should be number |
| 140 | +// }; |
| 141 | + |
| 142 | +console.log('✅ Connection method types validated!'); |
| 143 | + |
| 144 | +// Export to prevent "unused" errors |
| 145 | +export { |
| 146 | + testCheckConnection, |
| 147 | + testRelease, |
| 148 | + testReleaseClientFunction, |
| 149 | + testProperCleanupPattern, |
| 150 | + successResult, |
| 151 | + failureResult |
| 152 | +}; |
0 commit comments