|
| 1 | +import type { AwsDeploymentTarget } from '../../../../../schema'; |
| 2 | +import { AwsTargetsSchema } from '../../../../../schema'; |
| 3 | +import { |
| 4 | + AssignTargetsPanel, |
| 5 | + type EnvironmentAssignments, |
| 6 | + buildAwsTargetsConfig, |
| 7 | + buildEnvironmentsSection, |
| 8 | +} from '../AssignTargetsPanel'; |
| 9 | +import { render } from 'ink-testing-library'; |
| 10 | +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises'; |
| 11 | +import * as os from 'node:os'; |
| 12 | +import * as path from 'node:path'; |
| 13 | +import React from 'react'; |
| 14 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 15 | + |
| 16 | +const flush = (ms = 50) => new Promise(resolve => setTimeout(resolve, ms)); |
| 17 | + |
| 18 | +const targetA: AwsDeploymentTarget = { name: 'dev-a', account: '111111111111', region: 'us-west-2' }; |
| 19 | +const targetB: AwsDeploymentTarget = { name: 'dev-b', account: '222222222222', region: 'us-east-1' }; |
| 20 | +const targetC: AwsDeploymentTarget = { name: 'prod-a', account: '333333333333', region: 'us-east-1' }; |
| 21 | + |
| 22 | +describe('AssignTargetsPanel (UI)', () => { |
| 23 | + it('renders header columns for each environment and rows for each target', () => { |
| 24 | + const { lastFrame } = render( |
| 25 | + <AssignTargetsPanel |
| 26 | + targets={[targetA, targetB, targetC]} |
| 27 | + envNames={['dev', 'prod']} |
| 28 | + onConfirm={() => undefined} |
| 29 | + onCancel={() => undefined} |
| 30 | + /> |
| 31 | + ); |
| 32 | + const frame = lastFrame() ?? ''; |
| 33 | + expect(frame).toMatch(/Assign targets to environments:/); |
| 34 | + expect(frame).toMatch(/dev/); |
| 35 | + expect(frame).toMatch(/prod/); |
| 36 | + expect(frame).toMatch(/dev-a/); |
| 37 | + expect(frame).toMatch(/dev-b/); |
| 38 | + expect(frame).toMatch(/prod-a/); |
| 39 | + }); |
| 40 | + |
| 41 | + it('toggles the cell at the cursor with Space and surfaces it to onConfirm', async () => { |
| 42 | + const onConfirm = vi.fn(); |
| 43 | + const { stdin } = render( |
| 44 | + <AssignTargetsPanel |
| 45 | + targets={[targetA, targetB]} |
| 46 | + envNames={['dev', 'prod']} |
| 47 | + onConfirm={onConfirm} |
| 48 | + onCancel={() => undefined} |
| 49 | + /> |
| 50 | + ); |
| 51 | + // Cursor starts at (env=dev, target=dev-a). Toggle on. |
| 52 | + stdin.write(' '); |
| 53 | + await flush(); |
| 54 | + // ↓ to dev-b, toggle on. |
| 55 | + stdin.write('\u001B[B'); |
| 56 | + await flush(); |
| 57 | + stdin.write(' '); |
| 58 | + await flush(); |
| 59 | + // → to prod, ↑ back to dev-a... easier: from dev-b, → to prod (still at dev-b row), toggle on. |
| 60 | + stdin.write('\u001B[C'); |
| 61 | + await flush(); |
| 62 | + stdin.write(' '); |
| 63 | + await flush(); |
| 64 | + stdin.write('\r'); |
| 65 | + await flush(); |
| 66 | + expect(onConfirm).toHaveBeenCalledTimes(1); |
| 67 | + const result = onConfirm.mock.calls[0]![0] as EnvironmentAssignments; |
| 68 | + expect(Array.from(result.dev ?? [])).toEqual(['dev-a', 'dev-b']); |
| 69 | + expect(Array.from(result.prod ?? [])).toEqual(['dev-b']); |
| 70 | + }); |
| 71 | + |
| 72 | + it('cancels via Esc', async () => { |
| 73 | + const onCancel = vi.fn(); |
| 74 | + const onConfirm = vi.fn(); |
| 75 | + const { stdin } = render( |
| 76 | + <AssignTargetsPanel targets={[targetA]} envNames={['dev']} onConfirm={onConfirm} onCancel={onCancel} /> |
| 77 | + ); |
| 78 | + stdin.write('\u001B'); |
| 79 | + await flush(); |
| 80 | + expect(onCancel).toHaveBeenCalledTimes(1); |
| 81 | + expect(onConfirm).not.toHaveBeenCalled(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('renders an empty-targets fallback message when no targets are defined', () => { |
| 85 | + const { lastFrame } = render( |
| 86 | + <AssignTargetsPanel |
| 87 | + targets={[]} |
| 88 | + envNames={['dev', 'prod']} |
| 89 | + onConfirm={() => undefined} |
| 90 | + onCancel={() => undefined} |
| 91 | + /> |
| 92 | + ); |
| 93 | + expect(lastFrame() ?? '').toMatch(/No targets defined yet/); |
| 94 | + }); |
| 95 | + |
| 96 | + it('renders a no-environments fallback when envNames is empty', () => { |
| 97 | + const { lastFrame } = render( |
| 98 | + <AssignTargetsPanel targets={[targetA]} envNames={[]} onConfirm={() => undefined} onCancel={() => undefined} /> |
| 99 | + ); |
| 100 | + expect(lastFrame() ?? '').toMatch(/No environments to assign/); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe('buildEnvironmentsSection (serialization)', () => { |
| 105 | + it('serializes assignments into the schema-shaped environments map', () => { |
| 106 | + const assignments: EnvironmentAssignments = { |
| 107 | + dev: new Set(['dev-a', 'dev-b']), |
| 108 | + prod: new Set(['prod-a']), |
| 109 | + }; |
| 110 | + expect(buildEnvironmentsSection(assignments)).toEqual({ |
| 111 | + dev: { targets: ['dev-a', 'dev-b'] }, |
| 112 | + prod: { targets: ['prod-a'] }, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('drops environments with zero assigned targets', () => { |
| 117 | + const assignments: EnvironmentAssignments = { |
| 118 | + dev: new Set(['dev-a']), |
| 119 | + empty: new Set(), |
| 120 | + }; |
| 121 | + expect(buildEnvironmentsSection(assignments)).toEqual({ |
| 122 | + dev: { targets: ['dev-a'] }, |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns undefined when every environment is empty', () => { |
| 127 | + const assignments: EnvironmentAssignments = { dev: new Set(), prod: new Set() }; |
| 128 | + expect(buildEnvironmentsSection(assignments)).toBeUndefined(); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +describe('buildAwsTargetsConfig (schema-validated)', () => { |
| 133 | + it('produces an object that AwsTargetsSchema accepts (incl. cross-validation)', () => { |
| 134 | + const config = buildAwsTargetsConfig([targetA, targetB, targetC], { |
| 135 | + dev: new Set(['dev-a', 'dev-b']), |
| 136 | + prod: new Set(['prod-a']), |
| 137 | + }); |
| 138 | + // buildAwsTargetsConfig internally calls AwsTargetsSchema.parse, so we |
| 139 | + // re-validate here as a sanity check that the returned object is stable. |
| 140 | + const reparsed = AwsTargetsSchema.parse(config); |
| 141 | + expect(reparsed.targets).toHaveLength(3); |
| 142 | + expect(reparsed.environments?.dev?.targets).toEqual(['dev-a', 'dev-b']); |
| 143 | + expect(reparsed.environments?.prod?.targets).toEqual(['prod-a']); |
| 144 | + }); |
| 145 | + |
| 146 | + it('omits the environments field when no env has any targets', () => { |
| 147 | + const config = buildAwsTargetsConfig([targetA], { dev: new Set() }); |
| 148 | + expect(config.environments).toBeUndefined(); |
| 149 | + expect(() => AwsTargetsSchema.parse(config)).not.toThrow(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('throws on cross-validation when an environment references an unknown target', () => { |
| 153 | + expect(() => |
| 154 | + buildAwsTargetsConfig([targetA], { |
| 155 | + dev: new Set(['dev-a', 'missing-target']), |
| 156 | + }) |
| 157 | + ).toThrowError(/unknown target "missing-target"/); |
| 158 | + }); |
| 159 | + |
| 160 | + it('round-trips through aws-targets.json on disk and re-validates with AwsTargetsSchema', async () => { |
| 161 | + const tmpDir = path.join(os.tmpdir(), `assign-targets-${Date.now()}-${Math.random().toString(36).slice(2)}`); |
| 162 | + await mkdir(tmpDir, { recursive: true }); |
| 163 | + try { |
| 164 | + const filePath = path.join(tmpDir, 'aws-targets.json'); |
| 165 | + const config = buildAwsTargetsConfig([targetA, targetB, targetC], { |
| 166 | + dev: new Set(['dev-a', 'dev-b']), |
| 167 | + prod: new Set(['prod-a']), |
| 168 | + }); |
| 169 | + await writeFile(filePath, JSON.stringify(config, null, 2)); |
| 170 | + |
| 171 | + const raw = await readFile(filePath, 'utf8'); |
| 172 | + const parsed = AwsTargetsSchema.parse(JSON.parse(raw)); |
| 173 | + expect(parsed.targets.map(t => t.name)).toEqual(['dev-a', 'dev-b', 'prod-a']); |
| 174 | + expect(Object.keys(parsed.environments ?? {})).toEqual(['dev', 'prod']); |
| 175 | + } finally { |
| 176 | + await rm(tmpDir, { recursive: true, force: true }); |
| 177 | + } |
| 178 | + }); |
| 179 | +}); |
| 180 | + |
| 181 | +afterEach(() => { |
| 182 | + vi.restoreAllMocks(); |
| 183 | +}); |
| 184 | + |
| 185 | +beforeEach(() => { |
| 186 | + // No-op; placeholder for symmetry with other test files. |
| 187 | +}); |
0 commit comments