const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); function loadStep11(fetchImpl) { const source = fs.readFileSync('background/steps/get-plus-link.js', 'utf8'); const scope = {}; new Function('self', 'fetch', `${source}; return self.MultiPageBackgroundStep11;`)(scope, fetchImpl); return scope.MultiPageBackgroundStep11; } test('step 6 fetches ChatGPT session token and retries payurl checkout', async () => { const fetchCalls = []; const eventsOrder = []; const fetchImpl = async (url, options = {}) => { eventsOrder.push(`fetch:${url}`); fetchCalls.push({ url, options }); if (url === 'https://chatgpt.com/api/auth/session') { return { ok: true, status: 200, text: async () => JSON.stringify({ accessToken: 'session-token' }), }; } if (url === 'https://payurl.ark2.cn/api/checkout' && fetchCalls.filter((call) => call.url === url).length === 1) { return { ok: false, status: 502, text: async () => JSON.stringify({ error: 'temporary' }), }; } if (url === 'https://payurl.ark2.cn/api/checkout') { return { ok: true, status: 200, text: async () => JSON.stringify({ checkout_session_id: 'cs_test_123', url: 'https://pay.openai.com/c/pay/cs_test_123#token', chatgpt_checkout_url: 'https://chatgpt.com/checkout/openai_llc/cs_test_123', openai_payurl: 'https://pay.openai.com/c/pay/cs_test_123#token', }), }; } throw new Error(`unexpected fetch: ${url}`); }; const api = loadStep11(fetchImpl); const events = { logs: [], completed: null, navigatedUrl: '', }; const executor = api.createStep11Executor({ addLog: async (message, level = 'info') => events.logs.push({ message, level }), chrome: { scripting: { executeScript: async () => { throw new Error('page fallback should not be used when background session fetch succeeds'); }, }, }, completeStepFromBackground: async (step, payload) => { events.completed = { step, payload }; }, getTabId: async () => 123, reuseOrCreateTab: async (source, url) => { events.navigatedUrl = url; assert.equal(source, 'signup-page'); }, waitForTabStableComplete: async (tabId, options) => { eventsOrder.push('wait-stable'); assert.equal(tabId, 123); assert.equal(options.stableMs, 1500); return { id: tabId, status: 'complete', url: 'https://chatgpt.com/' }; }, }); await executor.executeStep11(); const payurlCalls = fetchCalls.filter((call) => call.url === 'https://payurl.ark2.cn/api/checkout'); assert.equal(payurlCalls.length, 2); assert.deepEqual(eventsOrder.slice(0, 2), [ 'wait-stable', 'fetch:https://chatgpt.com/api/auth/session', ]); assert.equal(events.navigatedUrl, 'https://pay.openai.com/c/pay/cs_test_123#token'); assert.deepEqual(events.completed, { step: 6, payload: { checkoutUrl: 'https://pay.openai.com/c/pay/cs_test_123#token', checkoutSessionId: 'cs_test_123', chatgptCheckoutUrl: 'https://chatgpt.com/checkout/openai_llc/cs_test_123', openaiPayUrl: 'https://pay.openai.com/c/pay/cs_test_123#token', }, }); const payload = JSON.parse(payurlCalls[1].options.body); assert.equal(payload.token, 'session-token'); assert.equal(payload.plan, 'plus'); assert.equal(payload.checkout_ui_mode, 'hosted'); assert.equal(payload.promo_code, 'STRIPEATLASGPT4BIZ050126'); assert.equal(payload.workspace_name, 'linux-do'); assert.equal(payload.seat_quantity, 2); });