import { readFileSync, symlinkSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { getWorkerBundleDir, getWorkerConfigPath } from '@cloudflare/build-output-utils'; import { cloudflareVersionMessageModule, cloudflareVersionMessageReference, encodeCloudflareVersionMessage, } from '@void/deploy-cloudflare'; import { expect, it } from 'vite-plus/test'; import { withCloudflareVersionMessage } from '../../src/cli/cf-version-message.ts'; import { output } from '../helpers/cf-version-fixture.ts'; const message = encodeCloudflareVersionMessage(['δΎ‹'.repeat(1000)]); const name = cloudflareVersionMessageModule(cloudflareVersionMessageReference(message)!); it.each( process.platform === 'win32' ? ['declared', 'unlisted'] : ['declared', 'unlisted', 'symlink'], )('preserves a %s module with a conflicting recovery name', async (kind) => { const prepared = await output(); try { const path = join(getWorkerBundleDir(prepared.directory), name); const configPath = getWorkerConfigPath(prepared.directory); if (kind === 'symlink') { symlinkSync(configPath, path); } else { writeFileSync(path, 'application text'); if (kind === 'declared') { const config = JSON.parse(readFileSync(configPath, 'utf8')); config.manifest.modules[name] = { type: 'text' }; writeFileSync(configPath, JSON.stringify(config)); } } const original = readFileSync(path, 'utf8'); await expect( withCloudflareVersionMessage(prepared.directory, { message }, () => { throw new Error('Unexpected upload'); }), ).rejects.toThrow(name); expect(readFileSync(path, 'utf8')).toBe(original); } finally { await prepared.dispose(); } });