2026-10-18 08:49:30 +08:00
import test from 'node:test' ; import assert from 'node:assert/strict' ;
import { mkdtemp , mkdir , readFile , writeFile } from 'node:fs/promises' ; import path from 'node:path' ; import os from 'node:os' ;
import { verify , X509Certificate , createHash , createPrivateKey } from 'node:crypto' ; import { createRequire } from 'node:module' ;
import { publishUpdate } from '../scripts/publish-update.mjs' ;
// Generate temporary test credentials in the OS temp area, never production keys.
const require = createRequire ( import . meta . url ) ;
const certs = require ( '@expo/code-signing-certificates' ) ;
test ( 'publisher signs the exact body, hashes assets, isolates environment, and republishes rollback as a new release' , async ( ) => {
const root = await mkdtemp ( path . join ( os . tmpdir ( ) , 'cmvr-publish-' ) ) ;
const keys = certs . generateKeyPair ( ) ;
const certificate = certs . generateSelfSignedCodeSigningCertificate ( { keyPair : keys , validityNotBefore : new Date ( Date . now ( ) - 60000 ) , validityNotAfter : new Date ( Date . now ( ) + 86400000 ) , commonName : 'CMVR test only' } ) ;
const keyFile = path . join ( root , 'key.pem' ) , certificateFile = path . join ( root , 'cert.pem' ) ;
await writeFile ( keyFile , certs . convertKeyPairToPEM ( keys ) . privateKeyPEM ) ; await writeFile ( certificateFile , certs . convertCertificateToCertificatePEM ( certificate ) ) ;
const exportDir = path . join ( root , 'export' ) ; await mkdir ( exportDir ) ;
await writeFile ( path . join ( exportDir , 'index.hbc' ) , 'test compiled bundle' ) ; await writeFile ( path . join ( exportDir , 'asset.png' ) , 'test image' ) ;
await writeFile ( path . join ( exportDir , 'metadata.json' ) , JSON . stringify ( { fileMetadata : { android : { bundle : 'index.hbc' , assets : [ { path : 'asset.png' , ext : 'png' } ] } } } ) ) ;
const options = { root : path . join ( root , 'data' ) , exportDir , origin : 'http://192.168.28.10' , environment : 'dev' , runtimeVersion : 'native-test' , keyFile , certificateFile ,
expoConfig : { runtimeVersion : 'native-test' , extra : { environment : 'dev' } } } ;
const first = await publishUpdate ( options ) ; const body = await readFile ( path . join ( options . root , 'releases' , first . id , 'manifest.json' ) ) ;
const sig = await readFile ( path . join ( options . root , 'releases' , first . id , 'signature.txt' ) , 'utf8' ) ;
assert . equal ( verify ( 'RSA-SHA256' , body , new X509Certificate ( await readFile ( certificateFile ) ) . publicKey , Buffer . from ( /sig="([^"]+)"/ . exec ( sig ) [ 1 ] , 'base64' ) ) , true ) ;
assert . equal ( first . launchAsset . hash , createHash ( 'sha256' ) . update ( 'test compiled bundle' ) . digest ( 'base64url' ) ) ;
assert . equal ( first . assets [ 0 ] . fileExtension , '.png' ) ; assert . match ( first . assets [ 0 ] . url , /^http:\/\/192\.168\.28\.10\/assets\/[a-f0-9]{64}$/ ) ;
2026-10-30 15:02:54 +08:00
await writeFile ( path . join ( exportDir , 'update-notes.json' ) , JSON . stringify ( [ '隐藏批次号' , '更新界面更简洁' ] ) ) ;
const second = await publishUpdate ( options ) ; assert . notEqual ( second . id , first . id ) ; assert . equal ( second . launchAsset . hash , first . launchAsset . hash ) ; assert . deepEqual ( second . metadata . releaseNotes , [ '隐藏批次号' , '更新界面更简洁' ] ) ;
2026-10-18 08:49:30 +08:00
assert . equal ( JSON . parse ( await readFile ( path . join ( options . root , 'dev/android/native-test/current.json' ) ) ) . id , second . id ) ;
await assert . rejects ( publishUpdate ( { ... options , environment : 'prod' } ) , /environment mismatch/ ) ;
await assert . rejects ( publishUpdate ( { ... options , runtimeVersion : 'incompatible' } ) , /runtime mismatch/ ) ;
await assert . rejects ( publishUpdate ( { ... options , origin : 'https://example.com' } ) , /内网/ ) ;
const encryptedFile = path . join ( root , 'encrypted.pem' ) ;
await writeFile ( encryptedFile , createPrivateKey ( await readFile ( keyFile ) ) . export ( { type : 'pkcs8' , format : 'pem' , cipher : 'aes-256-cbc' , passphrase : 'test-only-passphrase' } ) ) ;
const protectedRelease = await publishUpdate ( { ... options , keyFile : encryptedFile , keyPassphrase : 'test-only-passphrase' } ) ;
assert . equal ( protectedRelease . runtimeVersion , 'native-test' ) ;
await assert . rejects ( publishUpdate ( { ... options , keyFile : encryptedFile , keyPassphrase : 'incorrect' } ) ) ;
await writeFile ( path . join ( exportDir , 'metadata.json' ) , JSON . stringify ( { fileMetadata : { android : { bundle : '../key.pem' , assets : [ ] } } } ) ) ;
await assert . rejects ( publishUpdate ( options ) , /escapes/ ) ;
} ) ;