|
| 1 | +import { createHash } from 'node:crypto'; |
| 2 | +import { writeFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { afterEach, expect, it, vi } from 'vite-plus/test'; |
| 5 | +import { importD1SqlFile } from '../../src/cli/cf-d1-import.ts'; |
| 6 | +import { temporaryDirectory } from '../helpers/cloudflare-operation-process.ts'; |
| 7 | + |
| 8 | +const sql = "INSERT INTO messages VALUES ('synthetic');\n"; |
| 9 | +const etag = createHash('md5').update(sql).digest('hex'); |
| 10 | +const headers = { Authorization: 'Bearer synthetic-token' }; |
| 11 | +const endpoint = 'https://api.example.com/import'; |
| 12 | + |
| 13 | +function fixture() { |
| 14 | + const file = join(temporaryDirectory('void-d1-import-'), 'input.sql'); |
| 15 | + writeFileSync(file, sql); |
| 16 | + return { file, headers, endpoint }; |
| 17 | +} |
| 18 | +function response(result: unknown) { |
| 19 | + return Response.json({ success: true, result }); |
| 20 | +} |
| 21 | + |
| 22 | +afterEach(() => vi.unstubAllGlobals()); |
| 23 | + |
| 24 | +it('resumes an already uploaded import without uploading or ingesting twice', async () => { |
| 25 | + const fetch = vi |
| 26 | + .fn<typeof globalThis.fetch>() |
| 27 | + .mockResolvedValueOnce(response({ at_bookmark: 'current', success: true, status: 'active' })) |
| 28 | + .mockResolvedValueOnce(response({ status: 'complete', success: true })); |
| 29 | + vi.stubGlobal('fetch', fetch); |
| 30 | + await importD1SqlFile(fixture()); |
| 31 | + expect(fetch.mock.calls.map(([, init]) => JSON.parse(String(init?.body)))).toEqual([ |
| 32 | + { action: 'init', etag }, |
| 33 | + { action: 'poll', current_bookmark: 'current' }, |
| 34 | + ]); |
| 35 | +}); |
| 36 | + |
| 37 | +it.each([ |
| 38 | + { |
| 39 | + stage: 'initial status', |
| 40 | + responses: [response({ status: 'error', error: 'Import unavailable' })], |
| 41 | + error: 'Import unavailable', |
| 42 | + }, |
| 43 | + { |
| 44 | + stage: 'init', |
| 45 | + responses: [ |
| 46 | + Response.json( |
| 47 | + { success: false, errors: [{ message: 'Permission denied' }] }, |
| 48 | + { status: 403 }, |
| 49 | + ), |
| 50 | + ], |
| 51 | + error: 'Permission denied', |
| 52 | + }, |
| 53 | + { |
| 54 | + stage: 'upload', |
| 55 | + responses: [ |
| 56 | + response({ filename: 'input.sql', upload_url: 'https://uploads.example.com/sql' }), |
| 57 | + new Response('', { headers: { etag: 'wrong' } }), |
| 58 | + ], |
| 59 | + error: 'verification', |
| 60 | + }, |
| 61 | + { |
| 62 | + stage: 'ingest', |
| 63 | + responses: [ |
| 64 | + response({ filename: 'input.sql', upload_url: 'https://uploads.example.com/sql' }), |
| 65 | + new Response('', { headers: { etag } }), |
| 66 | + response({ status: 'error', success: false, error: 'Invalid SQL' }), |
| 67 | + ], |
| 68 | + error: 'Invalid SQL', |
| 69 | + }, |
| 70 | + { |
| 71 | + stage: 'poll', |
| 72 | + responses: [ |
| 73 | + response({ at_bookmark: 'current', status: 'active' }), |
| 74 | + response({ status: 'error', error: 'Import stopped' }), |
| 75 | + ], |
| 76 | + error: 'Import stopped', |
| 77 | + }, |
| 78 | + { |
| 79 | + stage: 'status', |
| 80 | + responses: [ |
| 81 | + response({ at_bookmark: 'current', status: 'active' }), |
| 82 | + response({ status: 'complete' }), |
| 83 | + ], |
| 84 | + error: 'invalid D1 import status', |
| 85 | + }, |
| 86 | +])('fails closed at $stage without resubmitting SQL', async ({ responses, error }) => { |
| 87 | + const fetch = vi.fn<typeof globalThis.fetch>(); |
| 88 | + for (const value of responses) { |
| 89 | + fetch.mockResolvedValueOnce(value); |
| 90 | + } |
| 91 | + vi.stubGlobal('fetch', fetch); |
| 92 | + await expect(importD1SqlFile(fixture())).rejects.toThrow(error); |
| 93 | + expect(fetch).toHaveBeenCalledTimes(responses.length); |
| 94 | +}); |
| 95 | + |
| 96 | +it('rejects non-HTTPS upload URLs without sending SQL or credentials', async () => { |
| 97 | + const fetch = vi.fn<typeof globalThis.fetch>().mockResolvedValueOnce( |
| 98 | + response({ |
| 99 | + filename: 'input.sql', |
| 100 | + upload_url: 'http://uploads.example.com/sql', |
| 101 | + }), |
| 102 | + ); |
| 103 | + vi.stubGlobal('fetch', fetch); |
| 104 | + await expect(importD1SqlFile(fixture())).rejects.toThrow('insecure'); |
| 105 | + expect(fetch).toHaveBeenCalledTimes(1); |
| 106 | +}); |
0 commit comments