import test from 'node:test' import assert from 'node:assert/strict' import { mkdtemp, rm, stat } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { pathToFileURL } from 'node:url' import * as esbuild from 'esbuild' async function importBundledOnlineHelpers() { const tempDir = await mkdtemp(join(tmpdir(), 'agent-recharge-online-')) const bundlePath = join(tempDir, 'agentRechargeOnline.mjs') await esbuild.build({ entryPoints: ['src/views/finance/agent-recharge/agentRechargeOnline.ts'], bundle: true, outfile: bundlePath, format: 'esm', platform: 'node', absWorkingDir: process.cwd(), plugins: [ { name: 'alias-src', setup(build) { build.onResolve({ filter: /^@\// }, async (args) => { const resolvedPath = join(process.cwd(), 'src', args.path.slice(2)) try { if ((await stat(resolvedPath)).isDirectory()) return { path: join(resolvedPath, 'index.ts') } } catch { // Let esbuild report the original resolution failure. } return { path: resolvedPath } }) } } ] }) const module = await import(pathToFileURL(bundlePath).href) return { module, cleanup: () => rm(tempDir, { recursive: true, force: true }) } } test('online recharge helpers convert yuan to integer fen and identify terminal states', async () => { const { module, cleanup } = await importBundledOnlineHelpers() try { assert.equal(module.amountYuanToFen(100.01), 10001) assert.equal(module.isOnlineRechargeTerminal(1), false) assert.equal(module.isOnlineRechargeTerminal(2), false) assert.equal(module.isOnlineRechargeTerminal(3), true) assert.equal(module.isOnlineRechargeTerminal(6), true) } finally { await cleanup() } }) test('online recharge request IDs are unique and namespaced', async () => { const { module, cleanup } = await importBundledOnlineHelpers() try { const first = module.createOnlineRechargeRequestId() const second = module.createOnlineRechargeRequestId() assert.match(first, /^recharge-/) assert.match(second, /^recharge-/) assert.notEqual(first, second) } finally { await cleanup() } })