calendar-oauth-connector-stress.test.mjs
95 lines 3.1 KB
Raw
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor ⚠ breaking 10 days ago
1 /**
2 * Tier 4 — STRESS: many calendars/events, rate limit, 410 resync.
3 */
4 import { describe, it, beforeEach, afterEach } from 'node:test';
5 import assert from 'node:assert/strict';
6 import fs from 'node:fs';
7 import path from 'node:path';
8 import { fileURLToPath } from 'node:url';
9 import {
10 handleBeginGoogleConnector,
11 handleGoogleConnectorCallback,
12 handleSyncGoogleConnector,
13 createFakeGoogleClient,
14 findPendingConnectorByState,
15 CONNECTOR_SYNC_RATE_LIMIT_MS,
16 } from '../lib/calendar/google-oauth-connector.mjs';
17
18 const __dirname = path.dirname(fileURLToPath(import.meta.url));
19 const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-calendar-oauth-stress');
20 const RETURN_URL = 'https://app.scooling.local:5174/settings/calendar/connect/return';
21 const testEnv = {
22 GOOGLE_CALENDAR_OAUTH_CLIENT_ID: 'cid',
23 GOOGLE_CALENDAR_OAUTH_CLIENT_SECRET: 'sec',
24 KNOWTATION_CALENDAR_OAUTH_SECRET: 's'.repeat(40),
25 CALENDAR_OAUTH_REDIRECT_URI: 'https://hub.local/callback',
26 SCOOLING_RETURN_URL_ALLOWLIST: RETURN_URL,
27 };
28
29 describe('calendar oauth stress', () => {
30 let dataDir;
31
32 beforeEach(() => {
33 fs.rmSync(tmpRoot, { recursive: true, force: true });
34 dataDir = path.join(tmpRoot, 'data');
35 fs.mkdirSync(dataDir, { recursive: true });
36 });
37
38 afterEach(() => {
39 fs.rmSync(tmpRoot, { recursive: true, force: true });
40 });
41
42 it('handles many calendars and respects sync rate limit', async () => {
43 const calendars = Array.from({ length: 20 }, (_, i) => ({ id: `cal-${i}`, summary: `Cal ${i}` }));
44 const eventsByCalendar = Object.fromEntries(
45 calendars.map((c) => [c.id, {
46 items: Array.from({ length: 5 }, (_, j) => ({
47 id: `${c.id}-evt-${j}`,
48 summary: `Event ${j}`,
49 start: { dateTime: '2026-06-18T17:00:00Z' },
50 end: { dateTime: '2026-06-18T18:00:00Z' },
51 })),
52 nextSyncToken: `token-${c.id}`,
53 }]),
54 );
55 const googleClient = createFakeGoogleClient({ calendars, eventsByCalendar });
56 const begin = handleBeginGoogleConnector({
57 dataDir,
58 vaultId: 'default',
59 body: { provider: 'google', return_url: RETURN_URL },
60 env: testEnv,
61 authorizedOverride: true,
62 });
63 const state = new URL(begin.payload.authorization_url).searchParams.get('state');
64 const located = findPendingConnectorByState(dataDir, state);
65 await handleGoogleConnectorCallback({
66 dataDir,
67 query: { code: 'c', state: located.connector.oauth_pending.state },
68 googleClient,
69 env: testEnv,
70 authorizedOverride: true,
71 });
72 const t0 = Date.now() + CONNECTOR_SYNC_RATE_LIMIT_MS + 1;
73 const first = await handleSyncGoogleConnector({
74 dataDir,
75 vaultId: 'default',
76 connectorId: begin.payload.connector_id,
77 googleClient,
78 env: testEnv,
79 now: t0,
80 authorizedOverride: true,
81 });
82 assert.equal(first.ok, true);
83 const second = await handleSyncGoogleConnector({
84 dataDir,
85 vaultId: 'default',
86 connectorId: begin.payload.connector_id,
87 googleClient,
88 env: testEnv,
89 now: t0 + 1000,
90 authorizedOverride: true,
91 });
92 assert.equal(second.ok, false);
93 assert.equal(second.code, 'RATE_LIMITED');
94 });
95 });
File History 1 commit
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor 10 days ago