media-write-unit.test.mjs
sha256:baa800aedf841dbf32081aa7b2befa288ac33dfc7175ac55014c55c4d8c742f9
docs: move durable-auth freeze/evidence to local developmen…
Human
10 days ago
| 1 | /** |
| 2 | * Tier 1 — UNIT: regexes, deterministic ids, envelope stamps, gate defaults off. |
| 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 { |
| 11 | CONNECTOR_ID_RE, |
| 12 | OPAQUE_REF_RE, |
| 13 | CONSENT_ID_RE, |
| 14 | deriveLinkAttachmentId, |
| 15 | getMediaExternalLinkEnabled, |
| 16 | getMediaAttachEnabled, |
| 17 | handleMediaLinkProposeRequest, |
| 18 | MEDIA_PROPOSAL_SOURCE, |
| 19 | MEDIA_REVIEW_QUEUE, |
| 20 | } from '../lib/attachments/attachment-write.mjs'; |
| 21 | import { ATTACHMENT_ID_RE } from '../lib/attachments/attachment-store.mjs'; |
| 22 | import { createProposal, listProposals } from '../hub/proposals-store.mjs'; |
| 23 | import { |
| 24 | buildMediaWriteFixture, |
| 25 | grantActiveConsent, |
| 26 | sampleLinkProposeBody, |
| 27 | } from './fixtures/media/write-helpers.mjs'; |
| 28 | |
| 29 | const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 30 | const tmpRoot = path.join(__dirname, 'fixtures', 'tmp-media-write-unit'); |
| 31 | |
| 32 | describe('media write — unit', () => { |
| 33 | beforeEach(() => { |
| 34 | fs.rmSync(tmpRoot, { recursive: true, force: true }); |
| 35 | delete process.env.MEDIA_EXTERNAL_LINK_ENABLED; |
| 36 | delete process.env.MEDIA_ATTACH_ENABLED; |
| 37 | }); |
| 38 | afterEach(() => { |
| 39 | delete process.env.MEDIA_EXTERNAL_LINK_ENABLED; |
| 40 | delete process.env.MEDIA_ATTACH_ENABLED; |
| 41 | }); |
| 42 | |
| 43 | it('CONNECTOR_ID_RE / OPAQUE_REF_RE / CONSENT_ID_RE / ATTACHMENT_ID_RE accept canonical', () => { |
| 44 | assert.ok(CONNECTOR_ID_RE.test('gdrive')); |
| 45 | assert.ok(!CONNECTOR_ID_RE.test('GDrive')); |
| 46 | assert.ok(OPAQUE_REF_RE.test('1AbCd_efGhIjkLmnOpQrStU')); |
| 47 | assert.ok(!OPAQUE_REF_RE.test('a'.repeat(257))); |
| 48 | assert.ok(CONSENT_ID_RE.test('mic_0123456789abcdef')); |
| 49 | const linkId = deriveLinkAttachmentId('gdrive', 'handle123'); |
| 50 | assert.ok(ATTACHMENT_ID_RE.test(linkId)); |
| 51 | assert.match(linkId, /^att_link_[a-f0-9]{32}$/); |
| 52 | }); |
| 53 | |
| 54 | it('deriveLinkAttachmentId is deterministic', () => { |
| 55 | const a = deriveLinkAttachmentId('gdrive', 'same'); |
| 56 | const b = deriveLinkAttachmentId('gdrive', 'same'); |
| 57 | assert.equal(a, b); |
| 58 | assert.notEqual(a, deriveLinkAttachmentId('gdrive', 'other')); |
| 59 | }); |
| 60 | |
| 61 | it('both gates default off', () => { |
| 62 | const dir = path.join(tmpRoot, 'gates'); |
| 63 | fs.mkdirSync(dir, { recursive: true }); |
| 64 | assert.equal(getMediaExternalLinkEnabled(dir), false); |
| 65 | assert.equal(getMediaAttachEnabled(dir), false); |
| 66 | }); |
| 67 | |
| 68 | it('hub_media_write_policy.json enables both gates when env unset (2F-b-d-kn-d staging)', () => { |
| 69 | const dir = path.join(tmpRoot, 'policy-file'); |
| 70 | fs.mkdirSync(dir, { recursive: true }); |
| 71 | fs.writeFileSync( |
| 72 | path.join(dir, 'hub_media_write_policy.json'), |
| 73 | JSON.stringify({ media_external_link_enabled: true, media_attach_enabled: true }), |
| 74 | 'utf8', |
| 75 | ); |
| 76 | delete process.env.MEDIA_EXTERNAL_LINK_ENABLED; |
| 77 | delete process.env.MEDIA_ATTACH_ENABLED; |
| 78 | assert.equal(getMediaExternalLinkEnabled(dir), true); |
| 79 | assert.equal(getMediaAttachEnabled(dir), true); |
| 80 | }); |
| 81 | |
| 82 | it('propose envelope stamps source media + review_queue media-writes when gate on', async () => { |
| 83 | process.env.MEDIA_EXTERNAL_LINK_ENABLED = '1'; |
| 84 | const fx = buildMediaWriteFixture(path.join(tmpRoot, 'env')); |
| 85 | const consentId = grantActiveConsent(fx.dataDir, fx.vaultId, 'gdrive'); |
| 86 | const body = sampleLinkProposeBody({ consent_id: consentId }); |
| 87 | const result = await handleMediaLinkProposeRequest({ |
| 88 | dataDir: fx.dataDir, |
| 89 | vaultId: fx.vaultId, |
| 90 | cliScopes: ['personal', 'project', 'org'], |
| 91 | body, |
| 92 | intent: 'link board', |
| 93 | createProposal, |
| 94 | }); |
| 95 | assert.equal(result.ok, true); |
| 96 | assert.equal(result.payload.schema, 'knowtation.media_proposal/v0'); |
| 97 | assert.equal(result.payload.proposal_kind, 'media_external_link'); |
| 98 | assert.equal(result.payload.auto_approvable, false); |
| 99 | assert.equal(result.payload.status, 'proposed'); |
| 100 | assert.equal(result.payload.review_queue, MEDIA_REVIEW_QUEUE); |
| 101 | |
| 102 | const { proposals } = listProposals(fx.dataDir, { source: MEDIA_PROPOSAL_SOURCE }); |
| 103 | assert.equal(proposals.length, 1); |
| 104 | assert.equal(proposals[0].source, MEDIA_PROPOSAL_SOURCE); |
| 105 | assert.equal(proposals[0].review_queue, MEDIA_REVIEW_QUEUE); |
| 106 | assert.equal(proposals[0].media_meta.proposal_kind, 'media_external_link'); |
| 107 | assert.equal(proposals[0].media_meta.record_kind, 'media_external_link'); |
| 108 | }); |
| 109 | }); |
File History
1 commit
sha256:baa800aedf841dbf32081aa7b2befa288ac33dfc7175ac55014c55c4d8c742f9
docs: move durable-auth freeze/evidence to local developmen…
Human
10 days ago