daily-checkin-http-gateway.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { describe, expect, it, vi } from 'vitest';
  2. import {
  3. SigninClaimResult,
  4. SigninDayStatus,
  5. SigninPanelScene,
  6. type ClaimSigninData,
  7. type SigninPanel,
  8. } from '@mavis/shared/daily-signin';
  9. import { TuiDailyCheckinHttpGateway } from '../../src/checkin/http-gateway.js';
  10. function panel(status = SigninDayStatus.Claimable): SigninPanel {
  11. return {
  12. scene: SigninPanelScene.Active,
  13. days: Array.from({ length: 7 }, (_, index) => ({
  14. day_no: index + 1,
  15. points: (index + 1) * 10,
  16. status: index === 1 ? status : SigninDayStatus.Upcoming,
  17. is_today: index === 1,
  18. })),
  19. };
  20. }
  21. function claim(): ClaimSigninData {
  22. return {
  23. claim_id: 'claim-1',
  24. claim_result: SigninClaimResult.Claimed,
  25. day_no: 2,
  26. points: 20,
  27. expire_at_ms: 1_800_000_000_000,
  28. panel: panel(SigninDayStatus.Claimed),
  29. };
  30. }
  31. function ok(data: unknown): Response {
  32. return new Response(JSON.stringify({ base_resp: { status_code: 0 }, data }), {
  33. status: 200,
  34. headers: { 'Content-Type': 'application/json' },
  35. });
  36. }
  37. describe('TuiDailyCheckinHttpGateway', () => {
  38. it('uses the signed public status and claim endpoints', async () => {
  39. const fetchImpl = vi
  40. .fn<typeof fetch>()
  41. .mockResolvedValueOnce(ok(panel()))
  42. .mockResolvedValueOnce(ok(claim()));
  43. const gateway = new TuiDailyCheckinHttpGateway({
  44. appVersion: '0.2.4',
  45. authContextGetter: () => ({ accessToken: 'token-1', realUserID: 'user-1' }),
  46. fetchImpl,
  47. origin: 'https://example.test',
  48. region: () => 'cn',
  49. nowMs: () => 1_800_000_000_000,
  50. });
  51. await gateway.getSigninPanel();
  52. await gateway.claimSignin();
  53. const [statusUrl, statusInit] = fetchImpl.mock.calls[0] ?? [];
  54. expect(String(statusUrl)).toContain('/minimax-cloud/api/v1/signin/status?');
  55. expect(String(statusUrl)).toContain('user_id=user-1');
  56. expect(statusInit).toEqual(
  57. expect.objectContaining({
  58. method: 'GET',
  59. headers: expect.objectContaining({
  60. Authorization: 'Bearer token-1',
  61. yy: expect.any(String),
  62. 'x-signature': expect.any(String),
  63. }),
  64. }),
  65. );
  66. const [claimUrl, claimInit] = fetchImpl.mock.calls[1] ?? [];
  67. expect(String(claimUrl)).toContain('/minimax-cloud/api/v1/signin/claim?');
  68. expect(claimInit).toEqual(expect.objectContaining({ method: 'POST', body: '{}' }));
  69. });
  70. it('refreshes managed auth once after an HTTP 401', async () => {
  71. const fetchImpl = vi
  72. .fn<typeof fetch>()
  73. .mockResolvedValueOnce(new Response('', { status: 401 }))
  74. .mockResolvedValueOnce(ok(panel()));
  75. const resolver = vi.fn(async () => ({ accessToken: 'token-2', realUserID: 'user-1' }));
  76. const gateway = new TuiDailyCheckinHttpGateway({
  77. appVersion: '0.2.4',
  78. authContextGetter: () => ({ accessToken: 'token-1', realUserID: 'user-1' }),
  79. authContextResolver: resolver,
  80. fetchImpl,
  81. origin: 'https://example.test',
  82. });
  83. await expect(gateway.getSigninPanel()).resolves.toEqual(panel());
  84. expect(resolver).toHaveBeenCalledWith(
  85. expect.objectContaining({ forceRefresh: true, signal: expect.any(AbortSignal) }),
  86. );
  87. expect(fetchImpl).toHaveBeenCalledTimes(2);
  88. });
  89. it('refuses to claim when the account changes after the status request', async () => {
  90. let realUserID = 'user-1';
  91. const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue(ok(panel()));
  92. const gateway = new TuiDailyCheckinHttpGateway({
  93. appVersion: '0.2.4',
  94. authContextGetter: () => ({ accessToken: 'token-1', realUserID }),
  95. fetchImpl,
  96. origin: 'https://example.test',
  97. });
  98. await gateway.getSigninPanel();
  99. realUserID = 'user-2';
  100. await expect(gateway.claimSignin()).rejects.toThrow('account changed');
  101. expect(fetchImpl).toHaveBeenCalledOnce();
  102. });
  103. it('discards a late status response after the active account changes', async () => {
  104. let realUserID = 'user-1';
  105. let release!: (response: Response) => void;
  106. const fetchImpl = vi.fn<typeof fetch>(
  107. async () =>
  108. await new Promise<Response>((resolve) => {
  109. release = resolve;
  110. }),
  111. );
  112. const gateway = new TuiDailyCheckinHttpGateway({
  113. appVersion: '0.2.4',
  114. authContextGetter: () => ({ accessToken: 'token-1', realUserID }),
  115. fetchImpl,
  116. origin: 'https://example.test',
  117. });
  118. const request = gateway.getSigninPanel();
  119. await vi.waitFor(() => expect(fetchImpl).toHaveBeenCalledOnce());
  120. realUserID = 'user-2';
  121. release(ok(panel()));
  122. await expect(request).rejects.toThrow('account changed');
  123. });
  124. it('preserves the empty claim body when authentication refreshes', async () => {
  125. const fetchImpl = vi
  126. .fn<typeof fetch>()
  127. .mockResolvedValueOnce(ok(panel()))
  128. .mockResolvedValueOnce(new Response('', { status: 401 }))
  129. .mockResolvedValueOnce(ok(claim()));
  130. const gateway = new TuiDailyCheckinHttpGateway({
  131. appVersion: '0.2.4',
  132. authContextGetter: () => ({ accessToken: 'token-1', realUserID: 'user-1' }),
  133. authContextResolver: async () => ({ accessToken: 'token-2', realUserID: 'user-1' }),
  134. fetchImpl,
  135. origin: 'https://example.test',
  136. });
  137. await gateway.getSigninPanel();
  138. await gateway.claimSignin();
  139. expect(fetchImpl).toHaveBeenCalledTimes(3);
  140. for (const [, init] of fetchImpl.mock.calls.slice(1)) {
  141. expect(init).toMatchObject({
  142. method: 'POST',
  143. body: '{}',
  144. });
  145. }
  146. });
  147. });