|
| 1 | +/* eslint-disable no-restricted-imports */ |
| 2 | +import {authFixture} from './auth.js' |
| 3 | +import * as path from 'path' |
| 4 | +import * as fs from 'fs' |
| 5 | +import {fileURLToPath} from 'url' |
| 6 | +import type {ExecResult} from './cli.js' |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url) |
| 9 | +const __dirname = path.dirname(__filename) |
| 10 | + |
| 11 | +const FIXTURE_DIR = path.join(__dirname, '../data/dawn-minimal') |
| 12 | + |
| 13 | +export interface ThemeScaffold { |
| 14 | + /** The directory containing the theme files */ |
| 15 | + themeDir: string |
| 16 | + /** Push theme to store, returns theme ID from output */ |
| 17 | + push(opts?: {unpublished?: boolean; themeName?: string}): Promise<{result: ExecResult; themeId?: string}> |
| 18 | + /** Pull theme from store by ID */ |
| 19 | + pull(themeId: string): Promise<ExecResult> |
| 20 | + /** List all themes on the store */ |
| 21 | + list(): Promise<{result: ExecResult; themes: {id: string; name: string; role: string}[]}> |
| 22 | + /** Delete a theme by ID */ |
| 23 | + delete(themeId: string): Promise<ExecResult> |
| 24 | + /** Rename a theme */ |
| 25 | + rename(themeId: string, newName: string): Promise<ExecResult> |
| 26 | + /** Duplicate a theme (via push with development flag) */ |
| 27 | + duplicate(themeId: string, newName: string): Promise<ExecResult> |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Recursively copies a directory. |
| 32 | + */ |
| 33 | +function copyDirRecursive(src: string, dest: string): void { |
| 34 | + fs.mkdirSync(dest, {recursive: true}) |
| 35 | + for (const entry of fs.readdirSync(src, {withFileTypes: true})) { |
| 36 | + const srcPath = path.join(src, entry.name) |
| 37 | + const destPath = path.join(dest, entry.name) |
| 38 | + if (entry.isDirectory()) { |
| 39 | + copyDirRecursive(srcPath, destPath) |
| 40 | + } else { |
| 41 | + fs.copyFileSync(srcPath, destPath) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Test-scoped fixture that copies the dawn-minimal fixture to a temp directory. |
| 48 | + * Provides helper methods for theme CRUD operations. |
| 49 | + * Depends on authLogin (worker-scoped) for OAuth session. |
| 50 | + */ |
| 51 | +export const themeScaffoldFixture = authFixture.extend<{themeScaffold: ThemeScaffold}>({ |
| 52 | + themeScaffold: async ({cli, env, authLogin: _authLogin}, use) => { |
| 53 | + const themeDir = fs.mkdtempSync(path.join(env.tempDir, 'theme-')) |
| 54 | + const createdThemeIds: string[] = [] |
| 55 | + const storeFqdn = env.storeFqdn |
| 56 | + |
| 57 | + // Copy fixture files recursively |
| 58 | + copyDirRecursive(FIXTURE_DIR, themeDir) |
| 59 | + |
| 60 | + const scaffold: ThemeScaffold = { |
| 61 | + themeDir, |
| 62 | + |
| 63 | + async push(opts = {}) { |
| 64 | + const themeName = opts.themeName ?? `e2e-test-${Date.now()}` |
| 65 | + const args = ['theme', 'push', '--store', storeFqdn, '--path', themeDir, '--theme', themeName] |
| 66 | + if (opts.unpublished !== false) { |
| 67 | + args.push('--unpublished') |
| 68 | + } |
| 69 | + // Add --json for parseable output |
| 70 | + args.push('--json') |
| 71 | + |
| 72 | + const result = await cli.exec(args, {timeout: 2 * 60 * 1000}) |
| 73 | + |
| 74 | + // Try to extract theme ID from JSON output |
| 75 | + let themeId: string | undefined |
| 76 | + try { |
| 77 | + const json = JSON.parse(result.stdout) |
| 78 | + if (json.theme?.id) { |
| 79 | + themeId = String(json.theme.id) |
| 80 | + createdThemeIds.push(themeId) |
| 81 | + } |
| 82 | + } catch (error) { |
| 83 | + // JSON parsing failed, try regex fallback |
| 84 | + if (!(error instanceof SyntaxError)) throw error |
| 85 | + const match = result.stdout.match(/theme[:\s]+(\d+)/i) ?? result.stderr.match(/theme[:\s]+(\d+)/i) |
| 86 | + if (match?.[1]) { |
| 87 | + themeId = match[1] |
| 88 | + createdThemeIds.push(themeId) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return {result, themeId} |
| 93 | + }, |
| 94 | + |
| 95 | + async pull(themeId: string) { |
| 96 | + return cli.exec(['theme', 'pull', '--store', storeFqdn, '--path', themeDir, '--theme', themeId], { |
| 97 | + timeout: 2 * 60 * 1000, |
| 98 | + }) |
| 99 | + }, |
| 100 | + |
| 101 | + async list() { |
| 102 | + const result = await cli.exec(['theme', 'list', '--store', storeFqdn, '--json'], {timeout: 60 * 1000}) |
| 103 | + const themes: {id: string; name: string; role: string}[] = [] |
| 104 | + |
| 105 | + try { |
| 106 | + const json = JSON.parse(result.stdout) |
| 107 | + if (Array.isArray(json)) { |
| 108 | + for (const theme of json) { |
| 109 | + themes.push({ |
| 110 | + id: String(theme.id), |
| 111 | + name: theme.name ?? '', |
| 112 | + role: theme.role ?? '', |
| 113 | + }) |
| 114 | + } |
| 115 | + } |
| 116 | + } catch (error) { |
| 117 | + // JSON parsing failed - return empty array |
| 118 | + if (!(error instanceof SyntaxError)) throw error |
| 119 | + } |
| 120 | + |
| 121 | + return {result, themes} |
| 122 | + }, |
| 123 | + |
| 124 | + async delete(themeId: string) { |
| 125 | + const result = await cli.exec(['theme', 'delete', '--store', storeFqdn, '--theme', themeId, '--force'], { |
| 126 | + timeout: 60 * 1000, |
| 127 | + }) |
| 128 | + // Remove from tracked IDs if successful |
| 129 | + const idx = createdThemeIds.indexOf(themeId) |
| 130 | + if (idx >= 0 && result.exitCode === 0) { |
| 131 | + createdThemeIds.splice(idx, 1) |
| 132 | + } |
| 133 | + return result |
| 134 | + }, |
| 135 | + |
| 136 | + async rename(themeId: string, newName: string) { |
| 137 | + return cli.exec(['theme', 'rename', '--store', storeFqdn, '--theme', themeId, '--name', newName], { |
| 138 | + timeout: 60 * 1000, |
| 139 | + }) |
| 140 | + }, |
| 141 | + |
| 142 | + async duplicate(themeId: string, newName: string) { |
| 143 | + // Pull the theme first, then push with new name |
| 144 | + const pullResult = await this.pull(themeId) |
| 145 | + if (pullResult.exitCode !== 0) { |
| 146 | + return pullResult |
| 147 | + } |
| 148 | + const {result} = await this.push({themeName: newName}) |
| 149 | + return result |
| 150 | + }, |
| 151 | + } |
| 152 | + |
| 153 | + await use(scaffold) |
| 154 | + |
| 155 | + // Teardown: delete all themes created during the test (parallel for speed) |
| 156 | + await Promise.all( |
| 157 | + createdThemeIds.map((themeId) => |
| 158 | + cli |
| 159 | + .exec(['theme', 'delete', '--store', storeFqdn, '--theme', themeId, '--force'], {timeout: 60 * 1000}) |
| 160 | + .catch(() => { |
| 161 | + // Best effort cleanup - don't fail teardown |
| 162 | + }), |
| 163 | + ), |
| 164 | + ) |
| 165 | + |
| 166 | + // Cleanup temp directory |
| 167 | + fs.rmSync(themeDir, {recursive: true, force: true}) |
| 168 | + }, |
| 169 | +}) |
0 commit comments