media-write-stress.test.mjs
124 lines 4.1 KB
Raw
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor ⚠ breaking 10 days ago
1 /**
2 * Tier 4 — STRESS: burst refusals, concurrent duplicate links, boundary sizes.
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 { createProposal } from '../hub/proposals-store.mjs';
12 import {
13 approveMediaProposal,
14 buildMediaWriteFixture,
15 grantActiveConsent,
16 sampleLinkProposeBody,
17 } from './fixtures/media/write-helpers.mjs';
18
19 const __dirname = path.dirname(fileURLToPath(import.meta.url));
20 const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-media-write-stress');
21
22 describe('media write — stress', () => {
23 beforeEach(() => {
24 fs.rmSync(tmpRoot, { recursive: true, force: true });
25 delete process.env.MEDIA_EXTERNAL_LINK_ENABLED;
26 delete process.env.MEDIA_ATTACH_ENABLED;
27 });
28 afterEach(() => {
29 delete process.env.MEDIA_EXTERNAL_LINK_ENABLED;
30 delete process.env.MEDIA_ATTACH_ENABLED;
31 });
32
33 it('100 concurrent link proposes with gate off all refuse', async () => {
34 const fx = buildMediaWriteFixture(path.join(tmpRoot, 'off-burst'));
35 const results = await Promise.all(
36 Array.from({ length: 100 }, () =>
37 handleMediaLinkProposeRequest({
38 dataDir: fx.dataDir,
39 vaultId: fx.vaultId,
40 cliScopes: ['personal'],
41 body: sampleLinkProposeBody(),
42 intent: 'x',
43 createProposal,
44 }),
45 ),
46 );
47 assert.ok(results.every((r) => !r.ok && r.code === 'MEDIA_EXTERNAL_LINK_DISABLED'));
48 });
49
50 it('100 concurrent link proposes same ref → one live ref, rest 409 after first approve', async () => {
51 process.env.MEDIA_EXTERNAL_LINK_ENABLED = '1';
52 const fx = buildMediaWriteFixture(path.join(tmpRoot, 'dup'));
53 const consentId = grantActiveConsent(fx.dataDir, fx.vaultId, 'gdrive');
54 const body = sampleLinkProposeBody({ consent_id: consentId });
55
56 const first = await handleMediaLinkProposeRequest({
57 dataDir: fx.dataDir,
58 vaultId: fx.vaultId,
59 cliScopes: ['personal', 'project', 'org'],
60 body,
61 intent: 'first',
62 createProposal,
63 });
64 assert.equal(first.ok, true);
65 approveMediaProposal(fx.dataDir, first.payload.proposal_id, fx.vaultPath);
66
67 const rest = await Promise.all(
68 Array.from({ length: 99 }, (_, i) =>
69 handleMediaLinkProposeRequest({
70 dataDir: fx.dataDir,
71 vaultId: fx.vaultId,
72 cliScopes: ['personal', 'project', 'org'],
73 body,
74 intent: `dup-${i}`,
75 createProposal,
76 }),
77 ),
78 );
79 assert.ok(rest.every((r) => !r.ok && r.code === 'MEDIA_LINEAGE_CONFLICT'));
80 });
81
82 it('opaque_ref 256 chars ok; 257 rejected; large intent bounded', async () => {
83 process.env.MEDIA_EXTERNAL_LINK_ENABLED = '1';
84 const fx = buildMediaWriteFixture(path.join(tmpRoot, 'bounds'));
85 const consentId = grantActiveConsent(fx.dataDir, fx.vaultId, 'gdrive');
86 const ok256 = await handleMediaLinkProposeRequest({
87 dataDir: fx.dataDir,
88 vaultId: fx.vaultId,
89 cliScopes: ['personal'],
90 body: sampleLinkProposeBody({
91 consent_id: consentId,
92 opaque_ref: 'A'.repeat(256),
93 }),
94 intent: 'ok',
95 createProposal,
96 });
97 assert.equal(ok256.ok, true);
98
99 const bad257 = await handleMediaLinkProposeRequest({
100 dataDir: fx.dataDir,
101 vaultId: fx.vaultId,
102 cliScopes: ['personal'],
103 body: sampleLinkProposeBody({
104 consent_id: consentId,
105 opaque_ref: 'A'.repeat(257),
106 }),
107 intent: 'bad',
108 createProposal,
109 });
110 assert.equal(bad257.ok, false);
111 assert.equal(bad257.code, 'MEDIA_DRAFT_INVALID');
112
113 const bigIntent = await handleMediaLinkProposeRequest({
114 dataDir: fx.dataDir,
115 vaultId: fx.vaultId,
116 cliScopes: ['personal'],
117 body: sampleLinkProposeBody({ consent_id: consentId, opaque_ref: 'B'.repeat(32) }),
118 intent: 'x'.repeat(2001),
119 createProposal,
120 });
121 assert.equal(bigIntent.ok, false);
122 assert.equal(bigIntent.code, 'MEDIA_DRAFT_INVALID');
123 });
124 });
File History 1 commit
sha256:b5f647cb9c409f563d4671fe3fc05ddea01fabfed9b41fc11cb923588e1c1baf mirror: GitHub Phase A durable MCP OAuth (#270) Human minor 10 days ago