|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockFetch, mockResolveCredentialBundle } = vi.hoisted(() => ({ |
| 7 | + mockFetch: vi.fn(), |
| 8 | + mockResolveCredentialBundle: vi.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +vi.mock('@/lib/selectors/server/providers/credential-bundle', () => ({ |
| 12 | + resolveSelectorCredentialBundle: mockResolveCredentialBundle, |
| 13 | +})) |
| 14 | + |
| 15 | +import { createSelectorProtectedValues } from '@/lib/selectors/server/protected-values' |
| 16 | +import { snowflakeSelectorAttachments } from '@/lib/selectors/server/providers/snowflake' |
| 17 | +import type { ExecuteServerSelectorArgs } from '@/lib/selectors/server/types' |
| 18 | + |
| 19 | +const STATEMENT_HANDLE = '019c06a4-0000-df4f-0000-00100006589e' |
| 20 | + |
| 21 | +function jsonResponse(body: unknown, status = 200): Response { |
| 22 | + return new Response(JSON.stringify(body), { |
| 23 | + status, |
| 24 | + headers: { 'Content-Type': 'application/json' }, |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +function tableArgs(signal?: AbortSignal): ExecuteServerSelectorArgs { |
| 29 | + return { |
| 30 | + selectorKey: 'snowflake.tables', |
| 31 | + context: { |
| 32 | + oauthCredential: 'credential-1', |
| 33 | + database: 'ANALYTICS', |
| 34 | + schema: 'PUBLIC', |
| 35 | + }, |
| 36 | + request: { kind: 'list' }, |
| 37 | + scope: { kind: 'workspace', workspaceId: 'workspace-1' }, |
| 38 | + workspaceId: 'workspace-1', |
| 39 | + principal: { kind: 'session', userId: 'user-1', sessionId: 'session-1' }, |
| 40 | + requesterUserId: 'user-1', |
| 41 | + credential: { suppliedId: 'credential-1' }, |
| 42 | + references: new Map(), |
| 43 | + signal, |
| 44 | + protectedValues: createSelectorProtectedValues(), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +describe('Snowflake server selector adapter', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks() |
| 51 | + vi.stubGlobal('fetch', mockFetch) |
| 52 | + mockResolveCredentialBundle.mockResolvedValue({ |
| 53 | + accessToken: 'server-only-token', |
| 54 | + domain: 'acme.snowflakecomputing.com', |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + afterAll(() => vi.unstubAllGlobals()) |
| 59 | + |
| 60 | + it('returns every advertised result partition in order', async () => { |
| 61 | + mockFetch |
| 62 | + .mockResolvedValueOnce( |
| 63 | + jsonResponse({ |
| 64 | + statementHandle: STATEMENT_HANDLE, |
| 65 | + data: [['ALPHA', 'first']], |
| 66 | + resultSetMetaData: { |
| 67 | + numRows: 3, |
| 68 | + partitionInfo: [{ rowCount: 1 }, { rowCount: 2 }], |
| 69 | + }, |
| 70 | + }) |
| 71 | + ) |
| 72 | + .mockResolvedValueOnce( |
| 73 | + jsonResponse({ |
| 74 | + data: [ |
| 75 | + ['BETA', null], |
| 76 | + ['GAMMA', 'third'], |
| 77 | + ], |
| 78 | + }) |
| 79 | + ) |
| 80 | + |
| 81 | + await expect( |
| 82 | + snowflakeSelectorAttachments['snowflake.tables'].execute(tableArgs()) |
| 83 | + ).resolves.toEqual({ |
| 84 | + kind: 'list', |
| 85 | + items: [ |
| 86 | + { |
| 87 | + id: 'ALPHA', |
| 88 | + label: 'ALPHA — first', |
| 89 | + meta: { name: 'ALPHA', detail: 'first' }, |
| 90 | + }, |
| 91 | + { id: 'BETA', label: 'BETA', meta: { name: 'BETA' } }, |
| 92 | + { |
| 93 | + id: 'GAMMA', |
| 94 | + label: 'GAMMA — third', |
| 95 | + meta: { name: 'GAMMA', detail: 'third' }, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }) |
| 99 | + |
| 100 | + expect(mockFetch).toHaveBeenCalledTimes(2) |
| 101 | + expect(String(mockFetch.mock.calls[1]?.[0])).toBe( |
| 102 | + `https://acme.snowflakecomputing.com/api/v2/statements/${STATEMENT_HANDLE}?partition=1` |
| 103 | + ) |
| 104 | + expect(mockFetch.mock.calls[1]?.[1]).toMatchObject({ method: 'GET', redirect: 'error' }) |
| 105 | + }) |
| 106 | + |
| 107 | + it('rejects the whole selector when a later partition fails', async () => { |
| 108 | + mockFetch |
| 109 | + .mockResolvedValueOnce( |
| 110 | + jsonResponse({ |
| 111 | + statementHandle: STATEMENT_HANDLE, |
| 112 | + data: [['ALPHA', null]], |
| 113 | + resultSetMetaData: { |
| 114 | + numRows: 2, |
| 115 | + partitionInfo: [{ rowCount: 1 }, { rowCount: 1 }], |
| 116 | + }, |
| 117 | + }) |
| 118 | + ) |
| 119 | + .mockResolvedValueOnce(jsonResponse({ message: 'private provider payload' }, 500)) |
| 120 | + |
| 121 | + await expect( |
| 122 | + snowflakeSelectorAttachments['snowflake.tables'].execute(tableArgs()) |
| 123 | + ).rejects.toMatchObject({ |
| 124 | + name: 'SelectorOptionsUnavailableError', |
| 125 | + message: 'Options unavailable', |
| 126 | + status: 502, |
| 127 | + }) |
| 128 | + expect(mockFetch).toHaveBeenCalledTimes(2) |
| 129 | + }) |
| 130 | + |
| 131 | + it('preserves caller cancellation during a later partition', async () => { |
| 132 | + const controller = new AbortController() |
| 133 | + const abortError = new DOMException('The operation was aborted', 'AbortError') |
| 134 | + let markLaterFetchStarted: (() => void) | undefined |
| 135 | + const laterFetchStarted = new Promise<void>((resolve) => { |
| 136 | + markLaterFetchStarted = resolve |
| 137 | + }) |
| 138 | + mockFetch |
| 139 | + .mockResolvedValueOnce( |
| 140 | + jsonResponse({ |
| 141 | + statementHandle: STATEMENT_HANDLE, |
| 142 | + data: [['ALPHA', null]], |
| 143 | + resultSetMetaData: { |
| 144 | + numRows: 3, |
| 145 | + partitionInfo: [{ rowCount: 1 }, { rowCount: 1 }, { rowCount: 1 }], |
| 146 | + }, |
| 147 | + }) |
| 148 | + ) |
| 149 | + .mockImplementationOnce((_input: RequestInfo | URL, init?: RequestInit) => { |
| 150 | + markLaterFetchStarted?.() |
| 151 | + return new Promise<Response>((_resolve, reject) => { |
| 152 | + init?.signal?.addEventListener('abort', () => reject(init.signal?.reason), { once: true }) |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + const execution = snowflakeSelectorAttachments['snowflake.tables'].execute( |
| 157 | + tableArgs(controller.signal) |
| 158 | + ) |
| 159 | + await laterFetchStarted |
| 160 | + controller.abort(abortError) |
| 161 | + |
| 162 | + await expect(execution).rejects.toBe(abortError) |
| 163 | + expect(mockFetch).toHaveBeenCalledTimes(2) |
| 164 | + }) |
| 165 | + |
| 166 | + it.each([ |
| 167 | + { |
| 168 | + name: 'missing partition metadata', |
| 169 | + body: { |
| 170 | + statementHandle: STATEMENT_HANDLE, |
| 171 | + data: [['ALPHA', null]], |
| 172 | + resultSetMetaData: { numRows: 1 }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + { |
| 176 | + name: 'more than 16 partitions', |
| 177 | + body: { |
| 178 | + statementHandle: STATEMENT_HANDLE, |
| 179 | + data: [['ALPHA', null]], |
| 180 | + resultSetMetaData: { |
| 181 | + numRows: 1, |
| 182 | + partitionInfo: Array.from({ length: 17 }, () => ({ rowCount: 0 })), |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + { |
| 187 | + name: 'more than 1,000 rows', |
| 188 | + body: { |
| 189 | + statementHandle: STATEMENT_HANDLE, |
| 190 | + data: [['ALPHA', null]], |
| 191 | + resultSetMetaData: { numRows: 1_001, partitionInfo: [{ rowCount: 1 }] }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: 'an invalid handle for a partitioned result', |
| 196 | + body: { |
| 197 | + statementHandle: '../untrusted-handle', |
| 198 | + data: [['ALPHA', null]], |
| 199 | + resultSetMetaData: { |
| 200 | + numRows: 2, |
| 201 | + partitionInfo: [{ rowCount: 1 }, { rowCount: 1 }], |
| 202 | + }, |
| 203 | + }, |
| 204 | + }, |
| 205 | + ])('rejects $name before requesting more data', async ({ body }) => { |
| 206 | + mockFetch.mockResolvedValueOnce(jsonResponse(body)) |
| 207 | + |
| 208 | + await expect( |
| 209 | + snowflakeSelectorAttachments['snowflake.tables'].execute(tableArgs()) |
| 210 | + ).rejects.toMatchObject({ |
| 211 | + name: 'SelectorOptionsUnavailableError', |
| 212 | + message: 'Options unavailable', |
| 213 | + status: 502, |
| 214 | + }) |
| 215 | + expect(mockFetch).toHaveBeenCalledTimes(1) |
| 216 | + }) |
| 217 | +}) |
0 commit comments