| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import test from 'node:test';
- import assert from 'node:assert/strict';
- import fs from 'node:fs';
- import os from 'node:os';
- import path from 'node:path';
- import { createHash } from 'node:crypto';
- import { spawnSync } from 'node:child_process';
- const windows = { skip: process.platform !== 'win32' };
- function fixture(t, batch) {
- const root = fs.mkdtempSync(path.join(os.tmpdir(), 'fmode-apk-test-'));
- t.after(() => { assert.equal(path.dirname(root), os.tmpdir()); assert.ok(path.basename(root).startsWith('fmode-apk-test-')); fs.rmSync(root, { recursive: true, force: true }); });
- const put = (name, text) => { const file = path.join(root, name); fs.mkdirSync(path.dirname(file), { recursive: true }); fs.writeFileSync(file, text); };
- put('scripts/build-acceptance-apk.ps1', fs.readFileSync(new URL('./build-acceptance-apk.ps1', import.meta.url)));
- put('sdk/platforms/android-36/android.jar', 'fixture');
- put('bad-sdk/exists.txt', 'a different SDK without android-36');
- put('docs/00-tracking/active-task.json', JSON.stringify({ ledger: 'test-results/active/state.json', environment: { androidSdk: path.join(root, 'sdk') } }));
- put('test-results/active/state.json', '{}');
- put('android/app/build/outputs/apk/debug/app-debug.apk', 'old APK must not be claimed as rebuilt');
- if (batch) put('android/gradlew.bat', batch);
- return { root, put, apk: path.join(root, 'android/app/build/outputs/apk/debug/app-debug.apk'), receipt: path.join(root, 'test-results/active/apk-fixture-receipt.json') };
- }
- function run(f) {
- return spawnSync('powershell.exe', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', path.join(f.root, 'scripts/build-acceptance-apk.ps1'), '-NativeOnly', '-Tag', 'fixture'], { cwd: f.root, env: { ...process.env, ANDROID_HOME: path.join(f.root, 'bad-sdk') }, encoding: 'utf8', timeout: 30000, windowsHide: true });
- }
- test('configured SDK takes precedence and successful native build receipt matches actual bytes', windows, t => {
- const f = fixture(t, '@echo off\r\necho rebuilt-fixture>android\\app\\build\\outputs\\apk\\debug\\app-debug.apk\r\nexit /b 0\r\n');
- const r = run(f); assert.equal(r.status, 0, r.stdout + r.stderr);
- const receipt = JSON.parse(fs.readFileSync(f.receipt, 'utf8').replace(/^\uFEFF/, ''));
- assert.equal(receipt.sdk, path.join(f.root, 'sdk'));
- assert.equal(receipt.webBuildAndSync, false);
- assert.equal(receipt.sha256.toLowerCase(), createHash('sha256').update(fs.readFileSync(f.apk)).digest('hex'));
- assert.match(fs.readFileSync(f.apk, 'utf8'), /rebuilt-fixture/);
- });
- test('missing native executable never produces a receipt for an old APK', windows, t => {
- const f = fixture(t); const before = fs.readFileSync(f.apk);
- const r = run(f); assert.notEqual(r.status, 0); assert.ok(!fs.existsSync(f.receipt));
- assert.match(r.stdout + r.stderr, /gradlew\.bat/);
- assert.ok(!(r.stdout + r.stderr).includes('ParserError'));
- assert.deepEqual(fs.readFileSync(f.apk), before);
- });
- test('nonzero native exit never produces a success receipt', windows, t => {
- const f = fixture(t, '@echo off\r\nexit /b 7\r\n');
- const r = run(f); assert.notEqual(r.status, 0); assert.ok(!fs.existsSync(f.receipt));
- assert.ok(fs.existsSync(path.join(f.root, 'test-results/active/android-gradle-fixture.log')));
- });
- test('existing evidence is preserved and no build is repeated', windows, t => {
- const f = fixture(t, '@echo off\r\necho must-not-run>ran.txt\r\nexit /b 0\r\n');
- f.put('test-results/active/apk-fixture-receipt.json', 'existing receipt');
- const r = run(f); assert.notEqual(r.status, 0);
- assert.ok(!(r.stdout + r.stderr).includes('ParserError'));
- assert.equal(fs.readFileSync(f.receipt, 'utf8'), 'existing receipt'); assert.ok(!fs.existsSync(path.join(f.root, 'ran.txt')));
- });
|