| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /** Publish only the immutable, credential-free onboarding asset; never publish the app. */
- import { recordProgress } from './lib/project-recovery.mjs';
- import fs from 'node:fs';
- import { createHash } from 'node:crypto';
- import assert from 'node:assert/strict';
- const out = 'test-results/new-account-20261128';
- const privateDir = '.local-backup/new-account-20261128';
- const stage = process.argv.find(x => x.startsWith('--stage='))?.slice(8) || 'status';
- const selectedPackage = process.argv.find(x => x.startsWith('--package-dir='))?.slice(14);
- const packageDir = selectedPackage || out + '/onboarding-package';
- let lock, state;
- const file = out + '/state.json';
- const save = () => { fs.writeFileSync(file + '.next', JSON.stringify(state, null, 2)); fs.renameSync(file + '.next', file); };
- const note = text => recordProgress(process.cwd(), `[${out}] ${text}`);
- try {
- if (stage === 'upload') { lock = fs.openSync(out + '/runner.lock', 'wx'); fs.writeFileSync(lock, JSON.stringify({ pid: process.pid, stage: 'publish-bundle' })); }
- state = JSON.parse(fs.readFileSync(file));
- let bundle = selectedPackage ? JSON.parse(fs.readFileSync(packageDir + '/receipt.json')) : undefined;
- if (bundle) assert.match(bundle.sha256, /^[a-f0-9]{64}$/);
- const actionKey = bundle ? 'publishBundle_' + bundle.sha256.slice(0, 16) : 'publishBundle';
- const suffix = bundle ? '-' + bundle.sha256.slice(0, 16) : '';
- if (stage === 'status') {
- const action = state.actions[actionKey];
- if (!action?.publicUrl) console.log(JSON.stringify({ uploaded: false, action }));
- else {
- const response = await fetch(action.publicUrl, { signal: AbortSignal.timeout(30000) });
- const digest = response.ok ? createHash('sha256').update(Buffer.from(await response.arrayBuffer())).digest('hex') : null;
- console.log(JSON.stringify({ http: response.status, sha256: digest, expectedSha256: action.sha256, matches: digest === action.sha256 }));
- }
- } else if (stage === 'upload') {
- assert.ok(!state.actions[actionKey]?.attempted, '上传已有记录,先 status 核实,禁止盲目重复 PUT');
- if (selectedPackage) {
- assert.ok(Object.entries(state.actions).some(([key, a]) => key.startsWith('onboardingLinux') && a.exitCode === 0 && a.confirmedAt && a.bundleSha256 === bundle.sha256), '尚无与本包摘要一致的同镜像回归');
- assert.equal(state.actions.localCandidate?.bundleSha256, bundle.sha256, '本包不是当前已安装候选版本');
- assert.ok(state.actions.trialCleanOutput?.confirmedAt, '当前修复尚无原会话真机纯文字回验');
- assert.equal(state.actions.trialCleanOutput.bundleSha256, bundle.sha256, '真机纯文字回验不属于当前包摘要');
- assert.ok(!Object.entries(state.actions).some(([key, a]) => key.startsWith('publishBundle') && a.attempted && a.sha256 === bundle.sha256), '同摘要上传已有记录,先只读核实');
- } else assert.equal(state.actions.onboardingLinux20?.exitCode, 0, '同镜像接入回归尚未通过');
- assert.ok(state.actions.trialChatReady?.proof, '真实 APK 文字往返尚未完成');
- assert.ok(state.actions.trialFilesV3?.proof, '完整字节图片与成果真实往返尚未完成');
- bundle ??= JSON.parse(fs.readFileSync(packageDir + '/receipt.json'));
- const bytes = fs.readFileSync(bundle.archive);
- assert.equal(createHash('sha256').update(bytes).digest('hex'), bundle.sha256);
- const account = JSON.parse(fs.readFileSync(privateDir + '/account.json'));
- assert.equal(account.userId, state.userId);
- const filename = `fmode-onboarding-${bundle.version}-${bundle.sha256.slice(0,16)}.tar.gz`;
- state.actions[actionKey] = { attempted: true, at: new Date().toISOString(), target: filename, sha256: bundle.sha256, bytes: bytes.length };
- save(); note(`准备通过既有上传接口保存无凭据通用包 ${filename},SHA256=${bundle.sha256};不发布 APK、不更改其他资源。`);
- const signed = await fetch('https://server.fmode.cn/api/functions', { method: 'POST', headers: {
- 'Content-Type': 'application/json', 'X-Parse-Application-Id': 'ncloudmaster', 'X-Parse-Session-Token': account.token },
- body: JSON.stringify({ id: 'AlP56LCKFm', token: account.token, params: { filename, mimeType: 'application/gzip', size: bytes.length } }), signal: AbortSignal.timeout(30000) });
- assert.ok(signed.ok); const ticket = await signed.json();
- assert.equal(ticket.code, 200, ticket.msg); assert.ok(ticket.uploadUrl && ticket.publicUrl && ticket.key);
- fs.writeFileSync(privateDir + '/bundle-upload-ticket' + suffix + '.json', JSON.stringify(ticket, null, 2), { flag: 'wx' });
- Object.assign(state.actions[actionKey], { publicUrl: ticket.publicUrl, objectKey: ticket.key, phase: 'before-put' }); save();
- const response = await fetch(ticket.uploadUrl, { method: 'PUT', headers: { 'Content-Type': 'application/gzip', ...(ticket.headers || {}) }, body: bytes, signal: AbortSignal.timeout(60000) });
- state.actions[actionKey].putHttp = response.status; save(); assert.ok(response.ok, '通用包 PUT 未确认成功');
- const readback = await fetch(ticket.publicUrl, { signal: AbortSignal.timeout(30000) }); assert.ok(readback.ok, '上传后公开对象暂不可读,请用 status 只读核实');
- const remote = Buffer.from(await readback.arrayBuffer()); const digest = createHash('sha256').update(remote).digest('hex');
- assert.equal(digest, bundle.sha256, '公开对象摘要不一致');
- const published = { version: bundle.version, image: 'fmode/agent-node:0.21.3', url: ticket.publicUrl, sha256: digest, bytes: remote.length };
- fs.writeFileSync('cloud/onboarding/bundle.json', JSON.stringify(published, null, 2) + '\n');
- state.actions[actionKey].confirmedAt = new Date().toISOString(); state.actions[actionKey].receipt = published; save();
- fs.writeFileSync(out + '/bundle-published' + suffix + '-receipt.json', JSON.stringify(published, null, 2), { flag: 'wx' });
- note(`通用包已上传并下载回验,HTTP=${response.status}/${readback.status},SHA256=${digest},回执 ${out}/bundle-published${suffix}-receipt.json;云函数尚未更新。`);
- console.log(JSON.stringify(published));
- } else throw Error('Unknown stage: ' + stage);
- } catch (error) {
- if (lock !== undefined && state) { state.lastFailure = { stage: 'publish-bundle', at: new Date().toISOString(), message: error.message }; save(); note(`未完成:${error.message},先只读核实,不重复上传。`); }
- throw error;
- } finally { if (lock !== undefined) { fs.closeSync(lock); fs.unlinkSync(out + '/runner.lock'); } }
|