calendar-oauth-connector-performance.test.mjs
93 lines 3.1 KB
Raw
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor ⚠ breaking 10 days ago
1 /**
2 * Tier 6 — PERFORMANCE: sync + decrypt within budget.
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 createFakeGoogleClient,
13 findPendingConnectorByState,
14 } from '../lib/calendar/google-oauth-connector.mjs';
15 import { queryStoredEvents } from '../lib/calendar/event-store.mjs';
16 import { oauthTokenVaultRoundTrip } from '../lib/calendar/oauth-token-vault.mjs';
17
18 const __dirname = path.dirname(fileURLToPath(import.meta.url));
19 const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-calendar-oauth-perf');
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: 'p'.repeat(40),
25 CALENDAR_OAUTH_REDIRECT_URI: 'https://hub.local/callback',
26 SCOOLING_RETURN_URL_ALLOWLIST: RETURN_URL,
27 };
28
29 describe('calendar oauth performance', () => {
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('initial sync of 100 events completes under 2s', async () => {
43 const items = Array.from({ length: 100 }, (_, i) => ({
44 id: `evt-${i}`,
45 summary: `E ${i}`,
46 start: { dateTime: '2026-06-18T17:00:00Z' },
47 end: { dateTime: '2026-06-18T18:00:00Z' },
48 }));
49 const googleClient = createFakeGoogleClient({
50 eventsByCalendar: { primary: { items, nextSyncToken: 't1' } },
51 });
52 const t0 = performance.now();
53 const begin = handleBeginGoogleConnector({
54 dataDir,
55 vaultId: 'default',
56 body: { provider: 'google', return_url: RETURN_URL },
57 env: testEnv,
58 authorizedOverride: true,
59 });
60 const state = new URL(begin.payload.authorization_url).searchParams.get('state');
61 const located = findPendingConnectorByState(dataDir, state);
62 await handleGoogleConnectorCallback({
63 dataDir,
64 query: { code: 'c', state: located.connector.oauth_pending.state },
65 googleClient,
66 env: testEnv,
67 authorizedOverride: true,
68 });
69 const elapsed = performance.now() - t0;
70 assert.ok(elapsed < 2000, `elapsed ${elapsed}ms`);
71 const events = queryStoredEvents(dataDir, 'default', {
72 fromIso: '2026-01-01T00:00:00.000Z',
73 toIso: '2027-01-01T00:00:00.000Z',
74 });
75 assert.equal(events.length, 100);
76 });
77
78 it('20 vault round-trips under 1500ms', () => {
79 const secret = 'q'.repeat(40);
80 const payload = {
81 refresh_token: 'rt',
82 scope: 'openid',
83 token_type: 'Bearer',
84 obtained_at: '2026-07-05T00:00:00.000Z',
85 account_sub: 'sub',
86 };
87 const t0 = performance.now();
88 for (let i = 0; i < 20; i += 1) {
89 oauthTokenVaultRoundTrip(secret, payload);
90 }
91 assert.ok(performance.now() - t0 < 1500);
92 });
93 });
File History 1 commit
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor 10 days ago