trial-entitlement-smoke-test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env node
  2. 'use strict';
  3. const assert = require('assert/strict');
  4. const { qiweiSubscriptionStatus } = require('../mcp/src/tools/qiwei-subscription-run');
  5. const { qiweiLoginStart } = require('../mcp/src/tools/qiwei-login-run');
  6. const originalFetch = global.fetch;
  7. let mode = 'paid';
  8. let requests = [];
  9. function response(data) {
  10. return new Response(JSON.stringify({ code: 200, data }), {
  11. status: 200,
  12. headers: { 'Content-Type': 'application/json' },
  13. });
  14. }
  15. async function main() {
  16. global.fetch = async rawUrl => {
  17. const url = new URL(String(rawUrl));
  18. requests.push(url.pathname);
  19. if (url.pathname.endsWith('/trial/status')) {
  20. return mode === 'trial'
  21. ? response({ active: true, state: 'active', seats: 1, credentialReady: true, expireAt: '2099-01-01T00:00:00.000Z' })
  22. : response({ active: true, state: 'unavailable', seats: null, credentialReady: false, expireAt: '2099-02-01T00:00:00.000Z' });
  23. }
  24. if (url.pathname.endsWith('/subscribe/status')) {
  25. return mode === 'trial'
  26. ? response({ subscribed: false, seats: null, usedSeats: 0 })
  27. : response({ subscribed: true, state: 'active', seats: 1, usedSeats: 1, expireAt: '2099-02-01T00:00:00.000Z' });
  28. }
  29. if (url.pathname.endsWith('/login/start')) {
  30. return response({ uid: 'fixture-trial-uid', loginQrcodeBase64Data: '' });
  31. }
  32. throw new Error(`Unexpected fixture request: ${url.pathname}`);
  33. };
  34. const common = {
  35. authToken: 'r:fixture-session',
  36. apiBase: 'https://fixture.example/api/qiwei',
  37. };
  38. mode = 'paid';
  39. requests = [];
  40. const paid = await qiweiSubscriptionStatus(common);
  41. assert.equal(paid.summary.subscribed, true);
  42. assert.equal(paid.summary.trialActive, false);
  43. assert.equal(paid.summary.source, 'subscription');
  44. mode = 'trial';
  45. requests = [];
  46. const trial = await qiweiSubscriptionStatus(common);
  47. assert.equal(trial.summary.subscribed, true);
  48. assert.equal(trial.summary.trialActive, true);
  49. assert.equal(trial.summary.source, 'trial');
  50. assert.equal(trial.summary.seats, 1);
  51. requests = [];
  52. const login = await qiweiLoginStart({
  53. ...common,
  54. uid: 'fixture-trial-uid',
  55. flowUi: false,
  56. persistConfig: false,
  57. openBrowser: false,
  58. });
  59. assert.equal(login.status, 'ok');
  60. assert.equal(requests.filter(path => path.endsWith('/trial/status')).length, 1);
  61. assert.equal(requests.filter(path => path.endsWith('/subscribe/status')).length, 0);
  62. assert.equal(requests.filter(path => path.endsWith('/login/start')).length, 1);
  63. process.stdout.write(`${JSON.stringify({
  64. status: 'ok',
  65. checks: 12,
  66. coverage: [
  67. 'paid_entitlement_is_not_labeled_trial',
  68. 'trial_entitlement_is_labeled_trial',
  69. 'trial_seat_passes_login_preflight',
  70. ],
  71. }, null, 2)}\n`);
  72. }
  73. main()
  74. .catch(error => {
  75. process.stderr.write(`${error.stack || error.message}\n`);
  76. process.exitCode = 1;
  77. })
  78. .finally(() => {
  79. global.fetch = originalFetch;
  80. });