media-write-performance.test.mjs
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠ breaking
10 days ago
| 1 | /** |
| 2 | * Tier 6 — PERFORMANCE: propose/apply budgets; lookups not vault scans. |
| 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 | |
| 10 | import { handleMediaLinkProposeRequest } from '../lib/attachments/attachment-write.mjs'; |
| 11 | import { getActiveConsent } from '../lib/attachments/media-import-consent.mjs'; |
| 12 | import { getEnabledConnector } from '../lib/attachments/media-connector-policy.mjs'; |
| 13 | import { listAttachments } from '../lib/attachments/attachment-store.mjs'; |
| 14 | import { createProposal } from '../hub/proposals-store.mjs'; |
| 15 | import { |
| 16 | approveMediaProposal, |
| 17 | buildMediaWriteFixture, |
| 18 | grantActiveConsent, |
| 19 | sampleLinkProposeBody, |
| 20 | } from './fixtures/media/write-helpers.mjs'; |
| 21 | |
| 22 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 23 | const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-media-write-perf'); |
| 24 | |
| 25 | describe('media write — performance', () => { |
| 26 | beforeEach(() => { |
| 27 | fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 28 | delete process.env.MEDIA_EXTERNAL_LINK_ENABLED; |
| 29 | delete process.env.MEDIA_ATTACH_ENABLED; |
| 30 | }); |
| 31 | afterEach(() => { |
| 32 | delete process.env.MEDIA_EXTERNAL_LINK_ENABLED; |
| 33 | delete process.env.MEDIA_ATTACH_ENABLED; |
| 34 | }); |
| 35 | |
| 36 | it('propose + apply p95 under budget; consent/connector lookup O(1)', async () => { |
| 37 | process.env.MEDIA_EXTERNAL_LINK_ENABLED = '1'; |
| 38 | const fx = buildMediaWriteFixture(path.join(tmpRoot, 'perf')); |
| 39 | const consentId = grantActiveConsent(fx.dataDir, fx.vaultId, 'gdrive'); |
| 40 | |
| 41 | const t0 = performance.now(); |
| 42 | for (let i = 0; i < 50; i += 1) { |
| 43 | getEnabledConnector(fx.dataDir, fx.vaultId, 'gdrive'); |
| 44 | getActiveConsent(fx.dataDir, fx.vaultId, 'gdrive', 'personal'); |
| 45 | } |
| 46 | const lookupMs = performance.now() - t0; |
| 47 | assert.ok(lookupMs < 200, `lookups took ${lookupMs}ms`); |
| 48 | |
| 49 | const proposeTimes = []; |
| 50 | for (let i = 0; i < 20; i += 1) { |
| 51 | const start = performance.now(); |
| 52 | const result = await handleMediaLinkProposeRequest({ |
| 53 | dataDir: fx.dataDir, |
| 54 | vaultId: fx.vaultId, |
| 55 | cliScopes: ['personal', 'project', 'org'], |
| 56 | body: sampleLinkProposeBody({ consent_id: consentId, opaque_ref: `ref-${i}` }), |
| 57 | intent: `perf-${i}`, |
| 58 | createProposal, |
| 59 | }); |
| 60 | proposeTimes.push(performance.now() - start); |
| 61 | assert.equal(result.ok, true); |
| 62 | approveMediaProposal(fx.dataDir, result.payload.proposal_id, fx.vaultPath); |
| 63 | } |
| 64 | proposeTimes.sort((a, b) => a - b); |
| 65 | const p95 = proposeTimes[Math.floor(proposeTimes.length * 0.95)] ?? proposeTimes.at(-1); |
| 66 | assert.ok(p95 < 500, `propose p95 ${p95}ms`); |
| 67 | |
| 68 | const listStart = performance.now(); |
| 69 | listAttachments(fx.dataDir, fx.vaultPath, fx.vaultId, { |
| 70 | effectiveScope: 'personal', |
| 71 | filterScopes: new Set(['personal', 'project', 'org']), |
| 72 | source: 'connector_ref', |
| 73 | }); |
| 74 | const listMs = performance.now() - listStart; |
| 75 | assert.ok(listMs < 1000, `list join took ${listMs}ms`); |
| 76 | }); |
| 77 | }); |
File History
1 commit
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf
mirror: GitHub Phase A durable MCP OAuth (#270)
Human
minor
⚠
10 days ago