|
| 1 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 2 | +import { tmpdir } from 'node:os'; |
| 3 | +import { join } from 'pathslash'; |
| 4 | +import type { Plugin, ResolvedConfig } from 'vite'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vite-plus/test'; |
| 6 | + |
| 7 | +type MockCloudflarePluginConfig = { |
| 8 | + config: (resolved: Record<string, unknown>) => Record<string, unknown>; |
| 9 | +}; |
| 10 | + |
| 11 | +vi.mock('@cloudflare/vite-plugin', () => ({ |
| 12 | + cloudflare(pluginConfig: MockCloudflarePluginConfig): Array<Plugin> { |
| 13 | + return [{ name: 'mock:cloudflare', api: { pluginConfig } } as Plugin]; |
| 14 | + }, |
| 15 | +})); |
| 16 | + |
| 17 | +const roots: Array<string> = []; |
| 18 | + |
| 19 | +function project(framework = false): string { |
| 20 | + const root = mkdtempSync(join(tmpdir(), 'void-pg-bindings-')); |
| 21 | + roots.push(root); |
| 22 | + writeFileSync(join(root, 'void.json'), JSON.stringify({ database: 'pg' })); |
| 23 | + writeFileSync( |
| 24 | + join(root, 'package.json'), |
| 25 | + JSON.stringify({ |
| 26 | + type: 'module', |
| 27 | + devDependencies: framework ? { '@sveltejs/kit': '2.70.3' } : {}, |
| 28 | + }), |
| 29 | + ); |
| 30 | + const route = framework ? 'src/routes/+page.server.ts' : 'routes/api/users.ts'; |
| 31 | + mkdirSync(join(root, framework ? 'src/routes' : 'routes/api'), { recursive: true }); |
| 32 | + writeFileSync(join(root, route), "import { db } from 'void/db'; export const GET = () => db;"); |
| 33 | + return root; |
| 34 | +} |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + vi.restoreAllMocks(); |
| 38 | + for (const root of roots.splice(0)) { |
| 39 | + rmSync(root, { recursive: true, force: true }); |
| 40 | + } |
| 41 | +}); |
| 42 | + |
| 43 | +describe('PostgreSQL binding generation', () => { |
| 44 | + it('does not emit a D1 placeholder for a Void app using void/db', async () => { |
| 45 | + const root = project(); |
| 46 | + vi.spyOn(process, 'cwd').mockReturnValue(root); |
| 47 | + const { voidPlugin } = await import('../../src/index.ts'); |
| 48 | + const cloudflarePlugin = voidPlugin().find((plugin) => plugin.name === 'mock:cloudflare'); |
| 49 | + if (!cloudflarePlugin) { |
| 50 | + throw new Error('Expected Cloudflare plugin.'); |
| 51 | + } |
| 52 | + const config = (cloudflarePlugin.api as { pluginConfig: MockCloudflarePluginConfig }) |
| 53 | + .pluginConfig.config; |
| 54 | + |
| 55 | + const patch = config({}); |
| 56 | + expect(patch.d1_databases).toBeUndefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('does not write a D1 placeholder into a SvelteKit project using void/db', async () => { |
| 60 | + const root = project(true); |
| 61 | + vi.spyOn(process, 'cwd').mockReturnValue(root); |
| 62 | + const { voidPlugin } = await import('../../src/index.ts'); |
| 63 | + const syncPlugin = voidPlugin().find((plugin) => plugin.name === 'void:wrangler-sync'); |
| 64 | + const hook = syncPlugin?.configResolved; |
| 65 | + const handler = typeof hook === 'function' ? hook : hook?.handler; |
| 66 | + if (!handler) { |
| 67 | + throw new Error('Expected Wrangler sync plugin.'); |
| 68 | + } |
| 69 | + handler.call({} as never, { command: 'serve' } as ResolvedConfig); |
| 70 | + |
| 71 | + const config = JSON.parse(readFileSync(join(root, 'wrangler.jsonc'), 'utf8')) as Record< |
| 72 | + string, |
| 73 | + unknown |
| 74 | + >; |
| 75 | + expect(config.d1_databases).toBeUndefined(); |
| 76 | + }); |
| 77 | +}); |
0 commit comments