|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `TursoDriverConfig.timeout` beside an UPPERCASE `WSS://` / `WS://` url in |
| 5 | + * forced remote mode is refused at construction, exactly as the lowercase pair |
| 6 | + * already is — the last corner of the same ADR-0049 gap. |
| 7 | + * |
| 8 | + * # Why the corner was open (the upstream step the first reading missed) |
| 9 | + * |
| 10 | + * `@libsql/client`'s routing switch really does match the literal lowercase |
| 11 | + * (`lib-esm/node.js`: `config.scheme === "wss" || config.scheme === "ws"`), and |
| 12 | + * reading only that switch says an uppercase url can never reach the WebSocket |
| 13 | + * arm. It can: `expandConfig` runs BEFORE the switch and has already lowercased |
| 14 | + * the scheme, so the switch never sees the original casing. Measured against |
| 15 | + * `@libsql/core@0.17.4`, whose `lib-esm/config.js` does it on one line — |
| 16 | + * `const originalUriScheme = uri.scheme.toLowerCase();`: |
| 17 | + * |
| 18 | + * ``` |
| 19 | + * expandConfig({ url: 'WSS://db.example.turso.io' }, true).scheme === 'wss' |
| 20 | + * expandConfig({ url: 'Ws://127.0.0.1:8080' }, true).scheme === 'ws' |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * (and the control that makes those two a reading rather than a coincidence: |
| 24 | + * `'LIBSQL://db.example.turso.io'` expands to `'https'`, so the same call is |
| 25 | + * observably capable of answering something other than the input's own letters.) |
| 26 | + * `@libsql/client@0.17.4`'s node entry is `_createClient(expandConfig(config, |
| 27 | + * true))`, so that lowercased scheme IS what the switch reads. An uppercase |
| 28 | + * `WSS://` url therefore reaches the WebSocket client, which has no window seam |
| 29 | + * at all — the reading `refuseWebSocketTimeout` already stands on. |
| 30 | + * |
| 31 | + * A correct local observation plus a missed upstream step yields a wrong |
| 32 | + * conclusion, and the observation itself survives re-checking; that is why this |
| 33 | + * pin spells the chain out rather than asserting the outcome alone. |
| 34 | + * |
| 35 | + * # What this file pins |
| 36 | + * |
| 37 | + * The refusal reaching the uppercase spellings, as the ADR-0112 envelope |
| 38 | + * (`code` + `status`) and as THE SAME MESSAGE the lowercase refusal produces — |
| 39 | + * asserted by construction, not by re-typing the text: the uppercase message |
| 40 | + * must equal the lowercase one with the echoed scheme swapped. The echo is the |
| 41 | + * one deliberate difference, and it is deliberate because an operator greps the |
| 42 | + * config for what they actually typed. |
| 43 | + * |
| 44 | + * And the three controls the refusal must not eat: |
| 45 | + * |
| 46 | + * 1. `detectMode` is untouched — an uppercase url with NO explicit mode still |
| 47 | + * falls through to `'local'`, the pre-existing behaviour this card |
| 48 | + * deliberately does not change. (No pin held this before; the scope ruling |
| 49 | + * on this card requires one, so it is written here rather than assumed.) |
| 50 | + * 2. The existing lowercase `wss://` / `ws://` refusals still fire |
| 51 | + * (`turso-driver-ws-timeout-refusal.test.ts` is the primary pin; repeated |
| 52 | + * here as the immediate neighbour of the widened predicate). |
| 53 | + * 3. `https://` remote + `timeout` is still ACCEPTED, in every casing. An |
| 54 | + * implementation that refused `timeout` on every remote would turn this |
| 55 | + * file green while deleting the whole option. |
| 56 | + * |
| 57 | + * # Reverse verification — direction predicted before it was run |
| 58 | + * |
| 59 | + * Restore `ridesWebSocketTransport` to its literal-prefix form and the refusal |
| 60 | + * cases go RED (the constructor returns a driver with `transportMode: |
| 61 | + * 'remote'`, and there is no envelope to read); all three controls stay GREEN, |
| 62 | + * because each describes behaviour that is identical before and after. |
| 63 | + * Measured both ways — see the PR. |
| 64 | + */ |
| 65 | + |
| 66 | +import { describe, expect, it } from 'vitest'; |
| 67 | +import { createTursoDriver } from './index.js'; |
| 68 | +import { TursoDriver } from './turso-driver.js'; |
| 69 | + |
| 70 | +type Refusal = Error & { code?: string; status?: number }; |
| 71 | + |
| 72 | +/** The error `build` threw, or `null` when it returned. */ |
| 73 | +function refusalOf(build: () => unknown): Refusal | null { |
| 74 | + try { |
| 75 | + build(); |
| 76 | + return null; |
| 77 | + } catch (error) { |
| 78 | + return error as Refusal; |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +const WINDOW_MS = 30_000; |
| 83 | +const HOST = 'db.example.turso.io'; |
| 84 | + |
| 85 | +describe('timeout beside an UPPERCASE WebSocket url in forced remote mode — refused at construction', () => { |
| 86 | + it.each([ |
| 87 | + ['WSS://', `WSS://${HOST}`], |
| 88 | + ['Wss://', `Wss://${HOST}`], |
| 89 | + ['WS://', 'WS://127.0.0.1:8080'], |
| 90 | + ['Ws://', 'Ws://127.0.0.1:8080'], |
| 91 | + ])('%s + explicit remote + timeout is refused as VALIDATION_ERROR / 400', (scheme, url) => { |
| 92 | + const refusal = refusalOf(() => new TursoDriver({ url, mode: 'remote', timeout: WINDOW_MS })); |
| 93 | + |
| 94 | + expect(refusal).not.toBeNull(); |
| 95 | + expect(refusal!.code).toBe('VALIDATION_ERROR'); |
| 96 | + expect(refusal!.status).toBe(400); |
| 97 | + expect(refusal!.message).toContain('TursoDriverConfig.timeout'); |
| 98 | + expect(refusal!.message).toContain(`${WINDOW_MS} ms`); |
| 99 | + // The scheme is echoed in the caller's own spelling. |
| 100 | + expect(refusal!.message).toContain(`\`${scheme}\``); |
| 101 | + // Both ways out survive. |
| 102 | + expect(refusal!.message).toContain('libsql://'); |
| 103 | + expect(refusal!.message).toContain('omit `timeout`'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('is the SAME message the lowercase refusal produces, differing only in the echoed scheme', () => { |
| 107 | + const upper = refusalOf(() => new TursoDriver({ url: `WSS://${HOST}`, mode: 'remote', timeout: WINDOW_MS })); |
| 108 | + const lower = refusalOf(() => new TursoDriver({ url: `wss://${HOST}`, mode: 'remote', timeout: WINDOW_MS })); |
| 109 | + |
| 110 | + expect(lower).not.toBeNull(); |
| 111 | + expect(upper).not.toBeNull(); |
| 112 | + expect(upper!.code).toBe(lower!.code); |
| 113 | + expect(upper!.status).toBe(lower!.status); |
| 114 | + expect(upper!.message).toBe(lower!.message.split('`wss://`').join('`WSS://`')); |
| 115 | + }); |
| 116 | + |
| 117 | + it('createTursoDriver() is the same constructor, and refuses the same pair', () => { |
| 118 | + const refusal = refusalOf(() => createTursoDriver({ url: `WSS://${HOST}`, mode: 'remote', timeout: WINDOW_MS })); |
| 119 | + |
| 120 | + expect(refusal?.code).toBe('VALIDATION_ERROR'); |
| 121 | + expect(refusal?.status).toBe(400); |
| 122 | + }); |
| 123 | +}); |
| 124 | + |
| 125 | +describe('CONTROL 1 — detectMode is untouched: an uppercase url with no explicit mode is still local', () => { |
| 126 | + it.each([`WSS://${HOST}`, 'Ws://127.0.0.1:8080', `HTTPS://${HOST}`, `LIBSQL://${HOST}`])( |
| 127 | + '%s with no `mode` falls through to local, exactly as before this change', |
| 128 | + (url) => { |
| 129 | + const driver = new TursoDriver({ url }); |
| 130 | + |
| 131 | + expect(driver.transportMode).toBe('local'); |
| 132 | + expect(driver.isRemote).toBe(false); |
| 133 | + }, |
| 134 | + ); |
| 135 | + |
| 136 | + it('and it stays local WITH a timeout — the refusal is scoped to remote mode, so it cannot reach here', () => { |
| 137 | + const driver = new TursoDriver({ url: `WSS://${HOST}`, timeout: WINDOW_MS }); |
| 138 | + |
| 139 | + expect(driver.transportMode).toBe('local'); |
| 140 | + expect(driver.getTursoConfig().timeout).toBe(WINDOW_MS); |
| 141 | + }); |
| 142 | +}); |
| 143 | + |
| 144 | +describe('CONTROL 2 — the existing lowercase refusals still fire', () => { |
| 145 | + it.each([ |
| 146 | + ['wss://', `wss://${HOST}`], |
| 147 | + ['ws://', 'ws://127.0.0.1:8080'], |
| 148 | + ])('%s + timeout is still refused as VALIDATION_ERROR / 400', (_scheme, url) => { |
| 149 | + const refusal = refusalOf(() => new TursoDriver({ url, authToken: 'token', timeout: WINDOW_MS })); |
| 150 | + |
| 151 | + expect(refusal?.code).toBe('VALIDATION_ERROR'); |
| 152 | + expect(refusal?.status).toBe(400); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +describe('CONTROL 3 — https:// remote + timeout stays ACCEPTED, in every casing', () => { |
| 157 | + it.each([`https://${HOST}`, `HTTPS://${HOST}`, `Https://${HOST}`, `LIBSQL://${HOST}`, `HTTP://127.0.0.1:8080`])( |
| 158 | + '%s + explicit remote + timeout constructs and keeps the window — the HTTP arm IS bounded', |
| 159 | + (url) => { |
| 160 | + const driver = new TursoDriver({ url, mode: 'remote', authToken: 'token', timeout: WINDOW_MS }); |
| 161 | + |
| 162 | + expect(driver.transportMode).toBe('remote'); |
| 163 | + expect(driver.getTursoConfig().timeout).toBe(WINDOW_MS); |
| 164 | + }, |
| 165 | + ); |
| 166 | + |
| 167 | + it('an uppercase WebSocket url with NO timeout still constructs as remote when mode is forced', () => { |
| 168 | + const driver = new TursoDriver({ url: `WSS://${HOST}`, mode: 'remote', authToken: 'token' }); |
| 169 | + |
| 170 | + expect(driver.transportMode).toBe('remote'); |
| 171 | + expect(driver.getTursoConfig().timeout).toBeUndefined(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('`timeout: 0` is the documented "no bound" and is not refused on an uppercase url either', () => { |
| 175 | + const driver = new TursoDriver({ url: `WSS://${HOST}`, mode: 'remote', authToken: 'token', timeout: 0 }); |
| 176 | + |
| 177 | + expect(driver.transportMode).toBe('remote'); |
| 178 | + expect(driver.getTursoConfig().timeout).toBe(0); |
| 179 | + }); |
| 180 | +}); |
0 commit comments