|
| 1 | +import { |
| 2 | + AUXILLARIES_CONTRACT_VERSION, |
| 3 | + assertAuxillariesJsonSafe, |
| 4 | + buildAuxillaryDataPayload, |
| 5 | + buildAuxillariesRecoveryRun, |
| 6 | + normalizeAuxillaryPane, |
| 7 | + parseAuxillariesContractSnapshot, |
| 8 | + toAuxillariesJsonSafe, |
| 9 | + validateAuxillariesContractSnapshot, |
| 10 | +} from '../auxillaries-contract'; |
| 11 | + |
| 12 | +describe('Auxillaries package route contracts', () => { |
| 13 | + it('builds package-owned Auxillaries state with source-safe route payload compatibility', () => { |
| 14 | + const payload = buildAuxillaryDataPayload({ |
| 15 | + profile: { |
| 16 | + id: 'user-1', |
| 17 | + username: 'operator', |
| 18 | + display_name: 'Operator', |
| 19 | + company_name: 'Bitcode', |
| 20 | + role: 'admin', |
| 21 | + wallet_binding: { |
| 22 | + address: 'tb1pauxcontract', |
| 23 | + provider: 'leather', |
| 24 | + status: 'verified', |
| 25 | + boundAt: '2026-05-21T00:00:00.000Z', |
| 26 | + }, |
| 27 | + protectedSource: 'never visible to support UI', |
| 28 | + settings: { |
| 29 | + service_role_key: 'service-role-secret', |
| 30 | + }, |
| 31 | + }, |
| 32 | + githubConnection: { |
| 33 | + provider: 'github', |
| 34 | + login: 'bitcode', |
| 35 | + installationId: 123, |
| 36 | + access_token: 'ghp_secret', |
| 37 | + rawPrompt: 'private prompt body', |
| 38 | + }, |
| 39 | + walletConnectionStatus: { |
| 40 | + connected: true, |
| 41 | + valid: true, |
| 42 | + provider: 'leather', |
| 43 | + address: 'tb1pauxcontract', |
| 44 | + verificationState: 'verified', |
| 45 | + metadata: { |
| 46 | + private_key: 'wallet-secret', |
| 47 | + connectedAt: '2026-05-21T00:00:00.000Z', |
| 48 | + }, |
| 49 | + }, |
| 50 | + repositoryConnectionStatus: { |
| 51 | + connected: true, |
| 52 | + valid: true, |
| 53 | + provider: 'github', |
| 54 | + username: 'bitcode', |
| 55 | + metadata: { |
| 56 | + token: 'provider-token', |
| 57 | + }, |
| 58 | + }, |
| 59 | + repositories: [ |
| 60 | + { |
| 61 | + fullName: 'bitcode/core', |
| 62 | + owner: { |
| 63 | + username: 'bitcode', |
| 64 | + type: 'organization', |
| 65 | + }, |
| 66 | + protected_source: 'repository source', |
| 67 | + }, |
| 68 | + ], |
| 69 | + repositoryInventorySource: 'stored_repository_inventory', |
| 70 | + btdBalance: 128, |
| 71 | + btcFeeBalance: 0.04, |
| 72 | + recentBtdAssetPacks: [ |
| 73 | + { |
| 74 | + assetPackId: 'asset-pack-1', |
| 75 | + rangeStart: 1, |
| 76 | + rangeEndExclusive: 3, |
| 77 | + }, |
| 78 | + ], |
| 79 | + modelPreferences: { |
| 80 | + preferred_model: 'gpt-4.1', |
| 81 | + }, |
| 82 | + onboardedSteps: ['btd', 'connects', 'profile', 'interfaces'], |
| 83 | + }); |
| 84 | + |
| 85 | + expect(payload.isOnboardingComplete).toBe(true); |
| 86 | + expect(payload.onboardedPanes).toEqual(['wallet', 'externals', 'profile', 'interfaces']); |
| 87 | + expect(payload.organizations).toEqual(['bitcode']); |
| 88 | + expect(payload.profileState.kind).toBe('AuxillariesProfileState'); |
| 89 | + expect(payload.connectionReadiness[0]).toMatchObject({ |
| 90 | + provider: 'github', |
| 91 | + connected: true, |
| 92 | + valid: true, |
| 93 | + credentialPosture: 'present_source_safe', |
| 94 | + }); |
| 95 | + expect(payload.walletBtdPaneState.signerPosture).toMatchObject({ |
| 96 | + ready: true, |
| 97 | + state: 'verified', |
| 98 | + }); |
| 99 | + expect(payload.organizationAuthority.policyDecision).toBe('allowed'); |
| 100 | + expect(payload.auxillariesContract.contractVersion).toBe(AUXILLARIES_CONTRACT_VERSION); |
| 101 | + expect(validateAuxillariesContractSnapshot(payload.auxillariesContract)).toEqual({ |
| 102 | + valid: true, |
| 103 | + errors: [], |
| 104 | + }); |
| 105 | + expect(parseAuxillariesContractSnapshot(payload.auxillariesContract).contractRoot) |
| 106 | + .toBe(payload.auxillariesContract.contractRoot); |
| 107 | + expect(JSON.stringify(payload)).not.toContain('ghp_secret'); |
| 108 | + expect(JSON.stringify(payload)).not.toContain('service-role-secret'); |
| 109 | + expect(JSON.stringify(payload)).not.toContain('wallet-secret'); |
| 110 | + expect(JSON.stringify(payload)).not.toContain('repository source'); |
| 111 | + expect(JSON.stringify(payload)).not.toContain('private prompt body'); |
| 112 | + expect(assertAuxillariesJsonSafe(payload)).toBeUndefined(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('emits pane diagnostics for incomplete profile, missing provider, and missing wallet state', () => { |
| 116 | + const payload = buildAuxillaryDataPayload({ |
| 117 | + profile: null, |
| 118 | + githubConnection: null, |
| 119 | + walletConnectionStatus: null, |
| 120 | + repositoryConnectionStatus: null, |
| 121 | + repositories: [], |
| 122 | + repositoryInventorySource: null, |
| 123 | + btdBalance: 0, |
| 124 | + btcFeeBalance: null, |
| 125 | + recentBtdAssetPacks: [], |
| 126 | + modelPreferences: null, |
| 127 | + onboardedSteps: [], |
| 128 | + }); |
| 129 | + |
| 130 | + const blockerIds = payload.readinessDiagnostics.map((diagnostic) => diagnostic.blockerId); |
| 131 | + expect(payload.profileState.accountReadiness).toBe('blocked'); |
| 132 | + expect(payload.connectionReadiness[0].requiredRepairAction).toBe('connect_provider'); |
| 133 | + expect(payload.walletBtdPaneState.signerPosture.requiredAction).toBe('connect_wallet'); |
| 134 | + expect(blockerIds).toEqual(expect.arrayContaining([ |
| 135 | + 'profile.missing', |
| 136 | + 'profile.identity_missing', |
| 137 | + 'connects.github.connect_provider', |
| 138 | + 'wallet.binding_missing', |
| 139 | + ])); |
| 140 | + expect(payload.auxillariesContract.contractRoot).toMatch(/^[0-9a-f]{64}$/); |
| 141 | + }); |
| 142 | + |
| 143 | + it('normalizes pane aliases and redacts known secret or protected-source keys', () => { |
| 144 | + const safe = toAuxillariesJsonSafe({ |
| 145 | + access_token: 'secret-token', |
| 146 | + privatePrompt: 'reasoning prompt', |
| 147 | + protectedSource: 'asset pack source', |
| 148 | + repositoryInventorySource: 'stored_repository_inventory', |
| 149 | + }) as Record<string, unknown>; |
| 150 | + |
| 151 | + expect(normalizeAuxillaryPane('connects')).toBe('externals'); |
| 152 | + expect(normalizeAuxillaryPane('btd')).toBe('wallet'); |
| 153 | + expect(safe.access_token).toBe('[redacted]'); |
| 154 | + expect(safe.privatePrompt).toBe('[protected-source-redacted]'); |
| 155 | + expect(safe.protectedSource).toBe('[protected-source-redacted]'); |
| 156 | + expect(safe.repositoryInventorySource).toBe('stored_repository_inventory'); |
| 157 | + expect(() => assertAuxillariesJsonSafe(safe)).not.toThrow(); |
| 158 | + }); |
| 159 | + |
| 160 | + it('parses contract snapshots and rejects malformed route contract objects', () => { |
| 161 | + const recoveryRun = buildAuxillariesRecoveryRun({ |
| 162 | + targetPane: 'externals', |
| 163 | + repairAction: 'reauthorize_provider', |
| 164 | + beforeReadinessRoot: 'before-root', |
| 165 | + afterReadinessRoot: 'after-root', |
| 166 | + executionId: 'execution-1', |
| 167 | + outcome: 'succeeded', |
| 168 | + }); |
| 169 | + const payload = buildAuxillaryDataPayload({ |
| 170 | + profile: { id: 'user-2', username: 'reviewer' }, |
| 171 | + githubConnection: { provider: 'github' }, |
| 172 | + walletConnectionStatus: null, |
| 173 | + repositoryConnectionStatus: { connected: false, valid: false, provider: 'github' }, |
| 174 | + repositories: [], |
| 175 | + repositoryInventorySource: null, |
| 176 | + btdBalance: 0, |
| 177 | + btcFeeBalance: null, |
| 178 | + recentBtdAssetPacks: [], |
| 179 | + modelPreferences: null, |
| 180 | + onboardedSteps: ['profile'], |
| 181 | + }); |
| 182 | + const snapshot = { |
| 183 | + ...payload.auxillariesContract, |
| 184 | + recoveryRuns: [recoveryRun], |
| 185 | + }; |
| 186 | + |
| 187 | + expect(parseAuxillariesContractSnapshot(snapshot).recoveryRuns[0].recoveryRoot) |
| 188 | + .toMatch(/^[0-9a-f]{64}$/); |
| 189 | + expect(validateAuxillariesContractSnapshot({ kind: 'wrong' })).toMatchObject({ |
| 190 | + valid: false, |
| 191 | + }); |
| 192 | + expect(() => parseAuxillariesContractSnapshot({ kind: 'wrong' })).toThrow( |
| 193 | + /Invalid Auxillaries contract snapshot/, |
| 194 | + ); |
| 195 | + }); |
| 196 | +}); |
0 commit comments