|
| 1 | +import type { DevframeDefinition } from '../types/devframe' |
| 2 | +import { existsSync } from 'node:fs' |
| 3 | +import fs from 'node:fs/promises' |
| 4 | +import { resolve } from 'pathe' |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | +import { createBuild } from './build' |
| 7 | + |
| 8 | +// Minimal stub definition |
| 9 | +function stubDefinition(overrides: Partial<DevframeDefinition> = {}): DevframeDefinition { |
| 10 | + return { |
| 11 | + id: 'test-devframe', |
| 12 | + setup: vi.fn(async () => {}), |
| 13 | + cli: { distDir: '/tmp/test-spa-dist' }, |
| 14 | + ...overrides, |
| 15 | + } as any |
| 16 | +} |
| 17 | + |
| 18 | +describe('createBuild', () => { |
| 19 | + const outDir = resolve('/tmp/devframe-build-test-out') |
| 20 | + const distDir = resolve('/tmp/test-spa-dist') |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + // Create a fake SPA dist directory with an index.html |
| 24 | + await fs.mkdir(distDir, { recursive: true }) |
| 25 | + await fs.writeFile(resolve(distDir, 'index.html'), '<html></html>') |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(async () => { |
| 29 | + await fs.rm(outDir, { recursive: true, force: true }) |
| 30 | + await fs.rm(distDir, { recursive: true, force: true }) |
| 31 | + }) |
| 32 | + |
| 33 | + it('throws when no distDir is provided', async () => { |
| 34 | + const d = stubDefinition({ cli: undefined }) |
| 35 | + await expect(createBuild(d, { outDir })).rejects.toThrow('no distDir') |
| 36 | + }) |
| 37 | + |
| 38 | + it('creates output directory', async () => { |
| 39 | + const d = stubDefinition() |
| 40 | + await createBuild(d, { outDir, distDir }) |
| 41 | + expect(existsSync(outDir)).toBe(true) |
| 42 | + }) |
| 43 | + |
| 44 | + it('copies SPA dist into outDir', async () => { |
| 45 | + const d = stubDefinition() |
| 46 | + await createBuild(d, { outDir, distDir }) |
| 47 | + expect(existsSync(resolve(outDir, 'index.html'))).toBe(true) |
| 48 | + }) |
| 49 | + |
| 50 | + it('writes __connection.json with backend: static', async () => { |
| 51 | + const d = stubDefinition() |
| 52 | + await createBuild(d, { outDir, distDir }) |
| 53 | + const meta = JSON.parse(await fs.readFile(resolve(outDir, '__connection.json'), 'utf-8')) |
| 54 | + expect(meta.backend).toBe('static') |
| 55 | + }) |
| 56 | + |
| 57 | + it('creates __rpc-dump directory', async () => { |
| 58 | + const d = stubDefinition() |
| 59 | + await createBuild(d, { outDir, distDir }) |
| 60 | + expect(existsSync(resolve(outDir, '__rpc-dump'))).toBe(true) |
| 61 | + }) |
| 62 | + |
| 63 | + it('writes __rpc-dump/index.json manifest', async () => { |
| 64 | + const d = stubDefinition() |
| 65 | + await createBuild(d, { outDir, distDir }) |
| 66 | + expect(existsSync(resolve(outDir, '__rpc-dump/index.json'))).toBe(true) |
| 67 | + }) |
| 68 | + |
| 69 | + it('writes spa-loader.json when d.spa is defined', async () => { |
| 70 | + const d = stubDefinition({ spa: { loader: 'query' } } as any) |
| 71 | + await createBuild(d, { outDir, distDir, base: '/app/' }) |
| 72 | + const loader = JSON.parse(await fs.readFile(resolve(outDir, 'spa-loader.json'), 'utf-8')) |
| 73 | + expect(loader.version).toBe(1) |
| 74 | + expect(loader.mode).toBe('query') |
| 75 | + expect(loader.base).toBe('/app/') |
| 76 | + }) |
| 77 | + |
| 78 | + it('does not write spa-loader.json when d.spa is undefined', async () => { |
| 79 | + const d = stubDefinition() |
| 80 | + await createBuild(d, { outDir, distDir }) |
| 81 | + expect(existsSync(resolve(outDir, 'spa-loader.json'))).toBe(false) |
| 82 | + }) |
| 83 | + |
| 84 | + it('removes existing outDir before building', async () => { |
| 85 | + await fs.mkdir(outDir, { recursive: true }) |
| 86 | + await fs.writeFile(resolve(outDir, 'stale.txt'), 'old') |
| 87 | + const d = stubDefinition() |
| 88 | + await createBuild(d, { outDir, distDir }) |
| 89 | + expect(existsSync(resolve(outDir, 'stale.txt'))).toBe(false) |
| 90 | + }) |
| 91 | + |
| 92 | + it('calls d.setup with build mode context', async () => { |
| 93 | + const setup = vi.fn(async () => {}) |
| 94 | + const d = stubDefinition({ setup }) |
| 95 | + await createBuild(d, { outDir, distDir }) |
| 96 | + expect(setup).toHaveBeenCalledOnce() |
| 97 | + }) |
| 98 | +}) |
0 commit comments