'use strict'; const assert = require('node:assert/strict'); const test = require('node:test'); const { createPs5FpkgControlCandidateService } = require('../../src/services/ps5-fpkg-control-candidate-service'); const HASH = character => character.repeat(64); function launchVerifiedOperation(overrides = {}) { return { id: 'operation-qualified', titleId: 'PPSA01234', packageSetId: 12, status: 'launch_verified', launchVerified: true, productionRestorable: true, packageIdentitySha256: HASH('1'), installedObservedAt: 1_500, updatedAt: 12_200, evidence: { source: 'post-install-console-scan', scanRunId: 'scan-qualified' }, components: [{ componentId: 'base', type: 'base', sha256: HASH('2'), size: 4096 }], launchEvidence: { schema: 'chimera.ps5-fpkg-launch-evidence.v2', runtimeFingerprint: HASH('3'), preflightCompletedAt: 1_000, requestedAt: 2_000, runtime: { ready: true, capabilities: { debugPackageRuntime: true, fpkgEnableActive: true, kernelPatchReady: true, packageInstallerReady: true, launchServiceReady: true, processObservationReady: true, syslogObservationReady: true, foregroundClean: true, }, runtime: { foregroundGameProcesses: [] }, artifacts: [{ componentId: 'base', titleId: 'PPSA01234', sha256: HASH('2'), size: 4096 }], }, request: { accepted: true, titleId: 'PPSA01234', route: '/api/ps5/app/launch', clientAutomaticRetry: false }, boundaries: { verifyImage: 'passed', pfsMount: 'passed', executable: 'reached', process: 'stable', basis: 'stable-exact-title-process' }, rawEvidence: [ { observedAt: 2_100, summary: { observed: true, processListTruncated: false, process: { titleId: 'PPSA01234', pid: 321 } } }, { observedAt: 12_100, summary: { observed: true, processListTruncated: false, process: { titleId: 'PPSA01234', pid: 321 } } }, ], }, ...overrides, }; } function installedObservedOperation(overrides = {}) { return { id: 'operation-installed', titleId: 'PPSA05678', packageSetId: 56, status: 'installed_observed', packageIdentitySha256: HASH('5'), installedObservedAt: 1_400, updatedAt: 1_450, evidence: { source: 'post-install-console-scan', scanRunId: 'scan-installed' }, components: [{ componentId: 'base', type: 'base', sha256: HASH('6'), size: 4096 }], ...overrides, }; } function databaseFixture() { const calls = []; return { calls, runQuery(sql) { calls.push(sql); if (sql.includes('FROM console_installs')) return [ { title_id: 'PPSA01234', name: 'Working homebrew', installed_version: '1.00', install_state: 'observed', format: 'ps5-fih-debug-package', last_seen_at: 1_500 }, { title_id: 'PPSA05678', name: 'Installed unknown', installed_version: '1.00', install_state: 'observed', format: 'ps5-fih-debug-package', last_seen_at: 1_400 }, ]; if (sql.includes('FROM package_sets')) return [ { package_set_id: 90, title_id: 'PPSA09999', name: 'Local candidate', platform: 'ps5', manifest_json: JSON.stringify({ packageFormat: { container: 'FIH' } }), component_id: 900, component_type: 'base', size_bytes: 8192, sha256: HASH('4'), metadata_json: JSON.stringify({ absolutePath: 'C:\\private\\candidate.pkg' }) }, ]; throw new Error(`Unexpected query: ${sql}`); }, }; } test('cached candidate report ranks qualified, installed and install-preflight lanes without a scan', () => { const db = databaseFixture(); const service = createPs5FpkgControlCandidateService({ db, listOperations: () => [launchVerifiedOperation(), installedObservedOperation()], now: () => 20_000, }); const report = service.listCached(); assert.equal(report.schema, 'chimera.p15-a8-control-candidates.v1'); assert.equal(report.source, 'cached-only-no-console-scan'); assert.equal(report.counts.qualifiedFullRoute, 1); assert.equal(report.counts.installedNeedsQualification, 1); assert.equal(report.counts.installPreflightRequired, 1); assert.equal(report.candidates[0].titleId, 'PPSA01234'); assert.equal(report.candidates[0].state, 'qualified_full_fpkg_route'); assert.match(report.candidates[0].qualificationDigest, /^[a-f0-9]{64}$/); assert.equal(report.candidates.find(item => item.titleId === 'PPSA05678').state, 'installed_launch_qualification_required'); assert.equal(report.candidates.find(item => item.titleId === 'PPSA09999').state, 'install_preflight_required'); assert.equal(report.grantsLiveAction, false); assert.equal(JSON.stringify(report).includes('C:\\private'), false); assert.equal(db.calls.length, 2); }); test('same-title console presence without artifact-bound install evidence is rejected', () => { const db = databaseFixture(); const report = createPs5FpkgControlCandidateService({ db, listOperations: () => [launchVerifiedOperation()], now: () => 20_000, }).listCached(); const candidate = report.candidates.find(item => item.titleId === 'PPSA05678'); assert.equal(candidate.installed, true); assert.equal(candidate.state, 'installed_launch_evidence_rejected'); assert.equal(candidate.reasonCode, 'P15_A8_CONTROL_INSTALLED_ARTIFACT_IDENTITY_UNPROVEN'); assert.equal(candidate.nextAction, 'select-distinct-title-or-establish-artifact-bound-install-evidence'); assert.equal(report.counts.installedNeedsQualification, 0); assert.equal(report.counts.rejected, 1); }); test('post-scan evidence must identify the same cached console observation', () => { const db = databaseFixture(); const stale = installedObservedOperation({ installedObservedAt: 1_399 }); const report = createPs5FpkgControlCandidateService({ db, listOperations: () => [launchVerifiedOperation(), stale], now: () => 20_000, }).listCached(); const candidate = report.candidates.find(item => item.titleId === 'PPSA05678'); assert.equal(candidate.state, 'installed_launch_evidence_rejected'); assert.equal(candidate.reasonCode, 'P15_A8_CONTROL_INSTALLED_ARTIFACT_IDENTITY_UNPROVEN'); }); test('launch success flags do not qualify mismatched or incomplete artifact evidence', () => { const db = databaseFixture(); const mismatched = launchVerifiedOperation(); mismatched.launchEvidence.runtime.artifacts[0].sha256 = HASH('9'); const report = createPs5FpkgControlCandidateService({ db, listOperations: () => [mismatched], now: () => 20_000, }).listCached(); const candidate = report.candidates.find(item => item.titleId === 'PPSA01234'); assert.equal(candidate.state, 'installed_launch_evidence_rejected'); assert.equal(candidate.reasonCode, 'P15_A8_CONTROL_ARTIFACT_EVIDENCE_MISMATCH'); assert.equal(candidate.qualificationDigest, null); }); test('candidate report rejects corrupt cached rows and never invents installed state', () => { const db = { runQuery(sql) { if (sql.includes('FROM console_installs')) return [{ title_id: 'not-a-title', install_state: 'observed' }]; if (sql.includes('FROM package_sets')) return [{ package_set_id: 1, title_id: 'PPSA12345', platform: 'ps5', manifest_json: '{broken' }]; return []; }, }; const report = createPs5FpkgControlCandidateService({ db, listOperations: () => [] }).listCached(); assert.deepEqual(report.candidates, []); assert.deepEqual(report.counts, { qualifiedFullRoute: 0, installedNeedsQualification: 0, installPreflightRequired: 0, rejected: 0 }); });