|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { generatePermissionForecast } from './permission-forecast'; |
| 3 | +import type { PermissionClass } from './keyword.types'; |
| 4 | + |
| 5 | +describe('generatePermissionForecast', () => { |
| 6 | + // ── Mode base classes ────────────────────────────────────────── |
| 7 | + |
| 8 | + describe('mode base permission classes', () => { |
| 9 | + it('PLAN mode → read-only only', () => { |
| 10 | + const forecast = generatePermissionForecast('PLAN', 'design auth feature'); |
| 11 | + expect(forecast.permissionClasses).toEqual(['read-only']); |
| 12 | + }); |
| 13 | + |
| 14 | + it('ACT mode → read-only + repo-write', () => { |
| 15 | + const forecast = generatePermissionForecast('ACT', 'implement login'); |
| 16 | + expect(forecast.permissionClasses).toContain('read-only'); |
| 17 | + expect(forecast.permissionClasses).toContain('repo-write'); |
| 18 | + }); |
| 19 | + |
| 20 | + it('EVAL mode → read-only only (no prompt signals)', () => { |
| 21 | + const forecast = generatePermissionForecast('EVAL', 'check code quality'); |
| 22 | + expect(forecast.permissionClasses).toEqual(['read-only']); |
| 23 | + }); |
| 24 | + |
| 25 | + it('AUTO mode → read-only + repo-write + external', () => { |
| 26 | + const forecast = generatePermissionForecast('AUTO', 'add login feature'); |
| 27 | + expect(forecast.permissionClasses).toContain('read-only'); |
| 28 | + expect(forecast.permissionClasses).toContain('repo-write'); |
| 29 | + expect(forecast.permissionClasses).toContain('external'); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + // ── Prompt-signal enrichment ─────────────────────────────────── |
| 34 | + |
| 35 | + describe('prompt-signal enrichment', () => { |
| 36 | + it('ship-related prompt adds repo-write + external and Ship bundle', () => { |
| 37 | + const forecast = generatePermissionForecast('ACT', 'ship the changes to GitHub'); |
| 38 | + expect(forecast.permissionClasses).toContain('repo-write'); |
| 39 | + expect(forecast.permissionClasses).toContain('external'); |
| 40 | + const ship = forecast.approvalBundles.find(b => b.name === 'Ship changes'); |
| 41 | + expect(ship).toBeDefined(); |
| 42 | + expect(ship!.actions).toContain('git push'); |
| 43 | + expect(ship!.actions).toContain('gh pr create'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('test-related prompt adds Run checks bundle', () => { |
| 47 | + const forecast = generatePermissionForecast('ACT', 'run tests and lint'); |
| 48 | + const checks = forecast.approvalBundles.find(b => b.name === 'Run checks'); |
| 49 | + expect(checks).toBeDefined(); |
| 50 | + expect(checks!.actions).toContain('yarn test'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('install-related prompt adds network class and Install bundle', () => { |
| 54 | + const forecast = generatePermissionForecast('ACT', 'install lodash package'); |
| 55 | + expect(forecast.permissionClasses).toContain('network'); |
| 56 | + const install = forecast.approvalBundles.find(b => b.name === 'Install dependencies'); |
| 57 | + expect(install).toBeDefined(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('delete-related prompt adds destructive class', () => { |
| 61 | + const forecast = generatePermissionForecast('ACT', 'delete the old migration files'); |
| 62 | + expect(forecast.permissionClasses).toContain('destructive'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('review prompt in EVAL mode adds external class and Review bundle', () => { |
| 66 | + const forecast = generatePermissionForecast('EVAL', 'review PR 1234'); |
| 67 | + expect(forecast.permissionClasses).toContain('external'); |
| 68 | + const review = forecast.approvalBundles.find(b => b.name === 'Review PR'); |
| 69 | + expect(review).toBeDefined(); |
| 70 | + expect(review!.actions).toContain('gh pr review'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('review prompt in ACT mode does NOT add Review bundle', () => { |
| 74 | + const forecast = generatePermissionForecast('ACT', 'review the code'); |
| 75 | + const review = forecast.approvalBundles.find(b => b.name === 'Review PR'); |
| 76 | + expect(review).toBeUndefined(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + // ── Implicit bundles ─────────────────────────────────────────── |
| 81 | + |
| 82 | + describe('implicit bundles', () => { |
| 83 | + it('ACT mode with no signal patterns → Code changes bundle', () => { |
| 84 | + const forecast = generatePermissionForecast('ACT', 'implement user dashboard'); |
| 85 | + const codeChanges = forecast.approvalBundles.find(b => b.name === 'Code changes'); |
| 86 | + expect(codeChanges).toBeDefined(); |
| 87 | + expect(codeChanges!.permissionClass).toBe('repo-write'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('ACT mode with explicit ship signal → no Code changes bundle (has Ship instead)', () => { |
| 91 | + const forecast = generatePermissionForecast('ACT', 'ship the feature'); |
| 92 | + const codeChanges = forecast.approvalBundles.find(b => b.name === 'Code changes'); |
| 93 | + expect(codeChanges).toBeUndefined(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // ── Permission summary ───────────────────────────────────────── |
| 98 | + |
| 99 | + describe('permissionSummary', () => { |
| 100 | + it('is a human-readable string', () => { |
| 101 | + const forecast = generatePermissionForecast('PLAN', 'design API'); |
| 102 | + expect(typeof forecast.permissionSummary).toBe('string'); |
| 103 | + expect(forecast.permissionSummary.length).toBeGreaterThan(0); |
| 104 | + }); |
| 105 | + |
| 106 | + it('includes mode name', () => { |
| 107 | + const forecast = generatePermissionForecast('PLAN', 'design API'); |
| 108 | + expect(forecast.permissionSummary).toContain('PLAN'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('includes highest permission class', () => { |
| 112 | + const forecast = generatePermissionForecast('ACT', 'ship to GitHub'); |
| 113 | + expect(forecast.permissionSummary).toContain('external'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('includes bundle names when bundles present', () => { |
| 117 | + const forecast = generatePermissionForecast('ACT', 'ship and run tests'); |
| 118 | + expect(forecast.permissionSummary).toContain('Ship changes'); |
| 119 | + expect(forecast.permissionSummary).toContain('Run checks'); |
| 120 | + }); |
| 121 | + |
| 122 | + it('no bundle names when no bundles', () => { |
| 123 | + const forecast = generatePermissionForecast('PLAN', 'design API'); |
| 124 | + expect(forecast.permissionSummary).not.toContain('—'); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + // ── Permission class ordering ────────────────────────────────── |
| 129 | + |
| 130 | + describe('class ordering', () => { |
| 131 | + it('returns classes in canonical order: read-only < repo-write < network < destructive < external', () => { |
| 132 | + const forecast = generatePermissionForecast( |
| 133 | + 'ACT', |
| 134 | + 'delete files, install packages, and ship', |
| 135 | + ); |
| 136 | + const expected: PermissionClass[] = [ |
| 137 | + 'read-only', |
| 138 | + 'repo-write', |
| 139 | + 'network', |
| 140 | + 'destructive', |
| 141 | + 'external', |
| 142 | + ]; |
| 143 | + expect(forecast.permissionClasses).toEqual(expected); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments