| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- #!/usr/bin/env node
- 'use strict';
- const assert = require('assert/strict');
- const { qiweiSubscriptionStatus } = require('../mcp/src/tools/qiwei-subscription-run');
- const { qiweiLoginStart } = require('../mcp/src/tools/qiwei-login-run');
- const originalFetch = global.fetch;
- let mode = 'paid';
- let requests = [];
- function response(data) {
- return new Response(JSON.stringify({ code: 200, data }), {
- status: 200,
- headers: { 'Content-Type': 'application/json' },
- });
- }
- async function main() {
- global.fetch = async rawUrl => {
- const url = new URL(String(rawUrl));
- requests.push(url.pathname);
- if (url.pathname.endsWith('/trial/status')) {
- return mode === 'trial'
- ? response({ active: true, state: 'active', seats: 1, credentialReady: true, expireAt: '2099-01-01T00:00:00.000Z' })
- : response({ active: true, state: 'unavailable', seats: null, credentialReady: false, expireAt: '2099-02-01T00:00:00.000Z' });
- }
- if (url.pathname.endsWith('/subscribe/status')) {
- return mode === 'trial'
- ? response({ subscribed: false, seats: null, usedSeats: 0 })
- : response({ subscribed: true, state: 'active', seats: 1, usedSeats: 1, expireAt: '2099-02-01T00:00:00.000Z' });
- }
- if (url.pathname.endsWith('/login/start')) {
- return response({ uid: 'fixture-trial-uid', loginQrcodeBase64Data: '' });
- }
- throw new Error(`Unexpected fixture request: ${url.pathname}`);
- };
- const common = {
- authToken: 'r:fixture-session',
- apiBase: 'https://fixture.example/api/qiwei',
- };
- mode = 'paid';
- requests = [];
- const paid = await qiweiSubscriptionStatus(common);
- assert.equal(paid.summary.subscribed, true);
- assert.equal(paid.summary.trialActive, false);
- assert.equal(paid.summary.source, 'subscription');
- mode = 'trial';
- requests = [];
- const trial = await qiweiSubscriptionStatus(common);
- assert.equal(trial.summary.subscribed, true);
- assert.equal(trial.summary.trialActive, true);
- assert.equal(trial.summary.source, 'trial');
- assert.equal(trial.summary.seats, 1);
- requests = [];
- const login = await qiweiLoginStart({
- ...common,
- uid: 'fixture-trial-uid',
- flowUi: false,
- persistConfig: false,
- openBrowser: false,
- });
- assert.equal(login.status, 'ok');
- assert.equal(requests.filter(path => path.endsWith('/trial/status')).length, 1);
- assert.equal(requests.filter(path => path.endsWith('/subscribe/status')).length, 0);
- assert.equal(requests.filter(path => path.endsWith('/login/start')).length, 1);
- process.stdout.write(`${JSON.stringify({
- status: 'ok',
- checks: 12,
- coverage: [
- 'paid_entitlement_is_not_labeled_trial',
- 'trial_entitlement_is_labeled_trial',
- 'trial_seat_passes_login_preflight',
- ],
- }, null, 2)}\n`);
- }
- main()
- .catch(error => {
- process.stderr.write(`${error.stack || error.message}\n`);
- process.exitCode = 1;
- })
- .finally(() => {
- global.fetch = originalFetch;
- });
|