'use strict'; const assert = require('node:assert/strict'); const crypto = require('node:crypto'); const test = require('node:test'); const { A10_OBSERVER, KSTUFF_REVISION } = require('../../src/services/ps5-p15-r2-qualification'); const { createP15R2QualificationService } = require('../../src/services/ps5-p15-r2-qualification-service'); function installedComplete() { return Object.fromEntries([ 'scanCoverageComplete', 'scanCurrent', 'titleIdExact', 'contentIdExact', 'versionExact', 'applicationDrmTypeExpected', 'sonyManagedInstallation', 'appPkgPresent', 'appPkgFullRead', 'appPkgSizeCoherent', 'appPkgHashStable', 'paramJsonPresent', 'paramJsonMatchesReference', 'sourceManifestEbootPresent', 'manifestCoverageComplete', 'materiallyComplete', 'ghostRegistrationAbsent', 'mountRedirectAbsent', ].map(key => [key, true]).concat([['installedObserved', true]])); } function exactEvidence(overrides = {}) { const tree = 'c'.repeat(40); return { repository: { commit: 'a'.repeat(40), tree, workingTreeDigest: crypto.createHash('sha256').update(`git-tree:${tree}`).digest('hex') }, firmware: '9.60', installedContent: installedComplete(), runtime: { production: { status: 'exact', lane: 'production', identityExact: true }, ps5uploadRevision: A10_OBSERVER.baseRevision, engineSha256: A10_OBSERVER.engineSha256, payloadSha256: A10_OBSERVER.payloadSha256, identityExact: true, kstuffRevision: KSTUFF_REVISION, kstuffIdentityExact: true, }, observer: { ...A10_OBSERVER, identityExact: true }, foreground: { inventoryComplete: true, shellUiPid: 110, runningBigAppId: -1, applicationProcesses: [] }, preflightMatrix: [ 'production deployment', 'firmware', 'host PPSA artifact', 'installed PPSA', 'production PS5Upload', 'A10 observer runtime', 'kstuff', 'ShellUI observer', 'BigApp observer', 'process observer', 'klog observer', 'foreground clean', ].map(gate => ({ gate, result: 'PASS', reason: null })), ...overrides, }; } function memoryStore() { let value = null; return { load: async () => structuredClone(value), save: async next => { value = structuredClone(next); } }; } test('service remains blocked and does not call launch for unproven evidence', async () => { let launchCalls = 0; const service = createP15R2QualificationService({ stateStore: memoryStore(), evidenceProvider: { collect: async () => ({ installedContent: { installedObserved: true } }) }, launchProvider: { launch: async () => { launchCalls += 1; } }, }); const state = await service.qualify(); assert.equal(state.budget.state, 'blocked'); assert.equal(state.budget.used, 0); await assert.rejects(() => service.launch({ confirmation: 'LAUNCH VERIFY PPSA99099', operationId: 'p15-r2-test-1' }), { code: 'P15_R2_LAUNCH_BLOCKED' }); assert.equal(launchCalls, 0); }); test('read-only preflight renders every R4 gate without persisting or arming state', async () => { const store = memoryStore(); const service = createP15R2QualificationService({ stateStore: store, evidenceProvider: { collect: async input => { assert.equal(input.mode, 'preflight'); return exactEvidence(); } }, launchProvider: { launch: async () => ({}) }, }); const before = await service.status(); const preflight = await service.preflight(); const after = await service.status(); assert.equal(preflight.schema, 'chimera.ps5-p15-r2-preflight.v1'); assert.equal(preflight.allGatesPass, true); assert.equal(preflight.matrix.length, 12); assert.deepEqual(preflight.matrix.map(row => row.result), Array(12).fill('PASS')); assert.deepEqual(after, before); assert.equal(after.profile, null); assert.equal(after.budget.state, 'blocked'); }); test('qualification fails closed when the explicit R4 matrix is not fully green', async () => { const evidence = exactEvidence(); evidence.preflightMatrix[10] = { gate: 'klog observer', result: 'FAIL', reason: 'fresh_tcp_9081_identity_unproven' }; const service = createP15R2QualificationService({ stateStore: memoryStore(), evidenceProvider: { collect: async () => evidence }, launchProvider: { launch: async () => ({}) }, }); const state = await service.qualify(); assert.equal(state.profile, null); assert.equal(state.budget.state, 'blocked'); assert.ok(state.readiness.reasons.includes('preflight_matrix_unproven')); }); test('service arms only from internally collected exact evidence', async () => { const service = createP15R2QualificationService({ stateStore: memoryStore(), evidenceProvider: { collect: async () => exactEvidence() }, launchProvider: { launch: async () => ({}) }, }); const state = await service.qualify({ clientEvidence: { identityExact: true } }); assert.equal(state.budget.state, 'armed'); assert.equal(state.budget.used, 0); const preflight = await service.preflightLaunch(); assert.equal(preflight.observerLane, 'a10_transition'); }); test('prelaunch identity drift blocks before consuming the one-run budget', async () => { let calls = 0; const provider = { collect: async () => calls++ === 0 ? exactEvidence() : exactEvidence({ firmware: 'unknown' }) }; const service = createP15R2QualificationService({ stateStore: memoryStore(), evidenceProvider: provider, launchProvider: { launch: async () => ({}) }, }); await service.qualify(); await assert.rejects(() => service.launch({ confirmation: 'LAUNCH VERIFY PPSA99099', operationId: 'p15-r2-test-2' }), { code: 'P15_R2_PRELAUNCH_DRIFT' }); assert.equal((await service.status()).budget.used, 0); }); test('provider ambiguity leaves budget consumed and impossible to rearm', async () => { const service = createP15R2QualificationService({ stateStore: memoryStore(), evidenceProvider: { collect: async () => exactEvidence() }, launchProvider: { launch: async () => { throw Object.assign(new Error('timeout'), { code: 'ETIMEDOUT' }); } }, }); await service.qualify(); await assert.rejects(() => service.launch({ confirmation: 'LAUNCH VERIFY PPSA99099', operationId: 'p15-r2-test-3' }), { code: 'P15_R2_LAUNCH_OUTCOME_AMBIGUOUS' }); const state = await service.status(); assert.equal(state.budget.used, 1); assert.equal(state.budget.state, 'consumed'); assert.equal(state.lastResult.classification, 'dispatch_started_outcome_ambiguous'); assert.equal((await service.qualify()).budget.state, 'consumed'); }); test('concurrent launch requests consume once and dispatch only once', async () => { let launchCalls = 0; const service = createP15R2QualificationService({ stateStore: memoryStore(), evidenceProvider: { collect: async () => exactEvidence() }, idFactory: () => 'p15-r2-server-owned-operation', launchProvider: { launch: async () => { launchCalls += 1; await new Promise(resolve => setImmediate(resolve)); return { lncAccepted: true, preRunningBigAppId: -1, samples: [] }; }, }, }); await service.qualify(); const results = await Promise.allSettled([ service.launch({ confirmation: 'LAUNCH VERIFY PPSA99099' }), service.launch({ confirmation: 'LAUNCH VERIFY PPSA99099' }), ]); assert.equal(results.filter(result => result.status === 'fulfilled').length, 1); assert.equal(results.filter(result => result.status === 'rejected').length, 1); assert.equal(launchCalls, 1); assert.equal((await service.status()).budget.used, 1); });