|
| 1 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 2 | +import { mkdtempSync, rmSync, existsSync, readFileSync } from 'node:fs'; |
| 3 | +import { join } from 'node:path'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import { scaffoldProject, isProjectScaffolded, copySkills } from './projectScaffolder.js'; |
| 6 | +import type { Session } from '@robcost/shared-types'; |
| 7 | +import { createDefaultAgentStates } from '@robcost/shared-types'; |
| 8 | + |
| 9 | +import type { GameEngine } from '@robcost/shared-types'; |
| 10 | + |
| 11 | +/** Creates a minimal session object pointing to a temp directory. */ |
| 12 | +function createTestSession(engine: GameEngine = 'phaser'): Session { |
| 13 | + const tempDir = mkdtempSync(join(tmpdir(), 'gameforge-test-')); |
| 14 | + const projectPath = join(tempDir, 'game'); |
| 15 | + return { |
| 16 | + id: 'test-session-id', |
| 17 | + createdAt: Date.now(), |
| 18 | + updatedAt: Date.now(), |
| 19 | + status: 'scaffolding', |
| 20 | + engine, |
| 21 | + genre: 'platformer', |
| 22 | + projectPath, |
| 23 | + vitePort: null, |
| 24 | + viteUrl: null, |
| 25 | + gdd: null, |
| 26 | + conversationHistory: [], |
| 27 | + agentStates: createDefaultAgentStates(), |
| 28 | + qaResults: [], |
| 29 | + iterationCount: 0, |
| 30 | + totalCostUsd: 0, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +let tempDirs: string[] = []; |
| 35 | + |
| 36 | +afterEach(() => { |
| 37 | + for (const dir of tempDirs) { |
| 38 | + rmSync(dir, { recursive: true, force: true }); |
| 39 | + } |
| 40 | + tempDirs = []; |
| 41 | +}); |
| 42 | + |
| 43 | +describe('scaffoldProject', () => { |
| 44 | + it('copies template files to the project directory', async () => { |
| 45 | + const session = createTestSession(); |
| 46 | + tempDirs.push(join(session.projectPath, '..')); |
| 47 | + |
| 48 | + await scaffoldProject(session, { skipInstall: true }); |
| 49 | + |
| 50 | + expect(existsSync(join(session.projectPath, 'package.json'))).toBe(true); |
| 51 | + expect(existsSync(join(session.projectPath, 'src', 'main.ts'))).toBe(true); |
| 52 | + expect(existsSync(join(session.projectPath, 'vite.config.ts'))).toBe(true); |
| 53 | + expect(existsSync(join(session.projectPath, 'index.html'))).toBe(true); |
| 54 | + }); |
| 55 | + |
| 56 | + it('copied package.json has phaser dependency', async () => { |
| 57 | + const session = createTestSession(); |
| 58 | + tempDirs.push(join(session.projectPath, '..')); |
| 59 | + |
| 60 | + await scaffoldProject(session, { skipInstall: true }); |
| 61 | + |
| 62 | + const pkg = JSON.parse( |
| 63 | + readFileSync(join(session.projectPath, 'package.json'), 'utf-8') |
| 64 | + ); |
| 65 | + expect(pkg.dependencies.phaser).toBeDefined(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('copies scene files', async () => { |
| 69 | + const session = createTestSession(); |
| 70 | + tempDirs.push(join(session.projectPath, '..')); |
| 71 | + |
| 72 | + await scaffoldProject(session, { skipInstall: true }); |
| 73 | + |
| 74 | + expect( |
| 75 | + existsSync(join(session.projectPath, 'src', 'scenes', 'BootScene.ts')) |
| 76 | + ).toBe(true); |
| 77 | + expect( |
| 78 | + existsSync(join(session.projectPath, 'src', 'scenes', 'MainScene.ts')) |
| 79 | + ).toBe(true); |
| 80 | + }); |
| 81 | + |
| 82 | + it('scaffolds threejs-starter template for threejs engine', async () => { |
| 83 | + const session = createTestSession('threejs'); |
| 84 | + tempDirs.push(join(session.projectPath, '..')); |
| 85 | + |
| 86 | + await scaffoldProject(session, { skipInstall: true }); |
| 87 | + |
| 88 | + expect(existsSync(join(session.projectPath, 'package.json'))).toBe(true); |
| 89 | + expect(existsSync(join(session.projectPath, 'src', 'main.ts'))).toBe(true); |
| 90 | + |
| 91 | + const pkg = JSON.parse( |
| 92 | + readFileSync(join(session.projectPath, 'package.json'), 'utf-8') |
| 93 | + ); |
| 94 | + expect(pkg.dependencies.three).toBeDefined(); |
| 95 | + expect(pkg.dependencies.phaser).toBeUndefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('scaffolds phaser-starter template for phaser engine', async () => { |
| 99 | + const session = createTestSession('phaser'); |
| 100 | + tempDirs.push(join(session.projectPath, '..')); |
| 101 | + |
| 102 | + await scaffoldProject(session, { skipInstall: true }); |
| 103 | + |
| 104 | + const pkg = JSON.parse( |
| 105 | + readFileSync(join(session.projectPath, 'package.json'), 'utf-8') |
| 106 | + ); |
| 107 | + expect(pkg.dependencies.phaser).toBeDefined(); |
| 108 | + expect(pkg.dependencies.three).toBeUndefined(); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe('copySkills', () => { |
| 113 | + it('copies skill files into .claude/skills/ directory', async () => { |
| 114 | + const session = createTestSession(); |
| 115 | + tempDirs.push(join(session.projectPath, '..')); |
| 116 | + |
| 117 | + await scaffoldProject(session, { skipInstall: true }); |
| 118 | + |
| 119 | + expect(existsSync(join(session.projectPath, '.claude', 'skills'))).toBe(true); |
| 120 | + expect( |
| 121 | + existsSync(join(session.projectPath, '.claude', 'skills', 'phaser-development', 'SKILL.md')) |
| 122 | + ).toBe(true); |
| 123 | + expect( |
| 124 | + existsSync(join(session.projectPath, '.claude', 'skills', 'threejs-development', 'SKILL.md')) |
| 125 | + ).toBe(true); |
| 126 | + }); |
| 127 | + |
| 128 | + it('copies genre and asset reference files', async () => { |
| 129 | + const session = createTestSession(); |
| 130 | + tempDirs.push(join(session.projectPath, '..')); |
| 131 | + |
| 132 | + await scaffoldProject(session, { skipInstall: true }); |
| 133 | + |
| 134 | + expect( |
| 135 | + existsSync(join(session.projectPath, '.claude', 'skills', 'phaser-development', 'GENRES.md')) |
| 136 | + ).toBe(true); |
| 137 | + expect( |
| 138 | + existsSync(join(session.projectPath, '.claude', 'skills', 'phaser-development', 'ASSETS.md')) |
| 139 | + ).toBe(true); |
| 140 | + expect( |
| 141 | + existsSync(join(session.projectPath, '.claude', 'skills', 'threejs-development', 'ASSETS.md')) |
| 142 | + ).toBe(true); |
| 143 | + }); |
| 144 | + |
| 145 | + it('is idempotent — does not error on repeated calls', async () => { |
| 146 | + const session = createTestSession(); |
| 147 | + tempDirs.push(join(session.projectPath, '..')); |
| 148 | + |
| 149 | + await scaffoldProject(session, { skipInstall: true }); |
| 150 | + |
| 151 | + // Second call should not throw |
| 152 | + expect(() => copySkills(session.projectPath)).not.toThrow(); |
| 153 | + }); |
| 154 | +}); |
| 155 | + |
| 156 | +describe('isProjectScaffolded', () => { |
| 157 | + it('returns true after scaffolding', async () => { |
| 158 | + const session = createTestSession(); |
| 159 | + tempDirs.push(join(session.projectPath, '..')); |
| 160 | + |
| 161 | + await scaffoldProject(session, { skipInstall: true }); |
| 162 | + |
| 163 | + expect(isProjectScaffolded(session)).toBe(true); |
| 164 | + }); |
| 165 | + |
| 166 | + it('returns false for unscaffolded session', () => { |
| 167 | + const session = createTestSession(); |
| 168 | + tempDirs.push(join(session.projectPath, '..')); |
| 169 | + |
| 170 | + expect(isProjectScaffolded(session)).toBe(false); |
| 171 | + }); |
| 172 | +}); |
0 commit comments