import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import test from 'node:test' function opener(wx) { const source = readFileSync(new URL('../utils/wechatOfficialAccount.js', import.meta.url), 'utf8') .replace(/^export /gm, '') return new Function('wx', `${source}; return openWechatOfficialAccount`)(wx) } test('profile API runs synchronously with original ID, never AppID', () => { const calls = [], events = [] const open = opener({ openOfficialAccountProfile(options) { calls.push(options) } }) open({ onUnavailable: () => events.push('fallback'), onComplete: () => events.push('complete') }) assert.equal(calls.length, 1) assert.equal(calls[0].username, 'gh_53e43e6f5c92') calls[0].success({ errMsg: 'openOfficialAccountProfile:ok' }) assert.deepEqual(events, ['complete']) }) for (const runtime of [undefined, {}, { openOfficialAccountProfile: null }]) { test(`unsupported runtime falls back without crashing: ${JSON.stringify(runtime)}`, () => { const events = [] opener(runtime)({ onUnavailable: () => events.push('fallback'), onComplete: () => events.push('complete') }) assert.deepEqual(events, ['fallback', 'complete']) }) } test('API failure provides fallback once even with repeated callbacks', () => { const events = [] let options opener({ openOfficialAccountProfile(value) { options = value } })({ onUnavailable: () => events.push('fallback'), onComplete: () => events.push('complete') }) options.fail({ errMsg: 'fail: no permission' }) options.fail({ errMsg: 'fail' }) options.success({}) assert.deepEqual(events, ['fallback', 'complete']) }) test('synchronous native exception releases busy state and falls back', () => { const events = [] opener({ openOfficialAccountProfile() { throw new Error('unsupported') } })({ onUnavailable: () => events.push('fallback'), onComplete: () => events.push('complete') }) assert.deepEqual(events, ['fallback', 'complete']) })