|
| 1 | +import { assert } from './test-utils/deps-node.js'; |
| 2 | +import { ensureAppScope, pryvErrorCode } from '../ts/cmc/appScope.ts'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Unit tests for the Plan 60 B1 hoisted `cmcAppScope.ensureAppScope`. |
| 6 | + * |
| 7 | + * The helper wraps two `streams.create` calls (appScope + optional subPath) |
| 8 | + * and tolerates idempotency / permission errors against the `:_cmc:apps` |
| 9 | + * plugin-managed namespace. See `ts/cmc/appScope.ts` for the rationale. |
| 10 | + */ |
| 11 | + |
| 12 | +const APP_CODE = 'hds-collector'; |
| 13 | +const EXPECTED_APP_SCOPE = ':_cmc:apps:' + APP_CODE; |
| 14 | + |
| 15 | +function makeConn (responder) { |
| 16 | + const calls = []; |
| 17 | + return { |
| 18 | + calls, |
| 19 | + async apiOne (method, params) { |
| 20 | + calls.push({ method, params }); |
| 21 | + return responder(method, params, calls.length - 1); |
| 22 | + } |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function pryvError (id) { |
| 27 | + const e = new Error('pryv-api: ' + id); |
| 28 | + e.innerObject = { id }; |
| 29 | + return e; |
| 30 | +} |
| 31 | + |
| 32 | +describe('[CAS] cmcAppScope.ensureAppScope', function () { |
| 33 | + describe('[CASE] pryvErrorCode', function () { |
| 34 | + it('[CAS01] reads innerObject.id when present', () => { |
| 35 | + assert.equal(pryvErrorCode(pryvError('item-already-exists')), 'item-already-exists'); |
| 36 | + }); |
| 37 | + it('[CAS02] falls back to top-level id when no innerObject', () => { |
| 38 | + assert.equal(pryvErrorCode({ id: 'forbidden' }), 'forbidden'); |
| 39 | + }); |
| 40 | + it('[CAS03] returns undefined for unrelated values', () => { |
| 41 | + assert.equal(pryvErrorCode(new Error('x')), undefined); |
| 42 | + assert.equal(pryvErrorCode(null), undefined); |
| 43 | + assert.equal(pryvErrorCode(undefined), undefined); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('[CASH] happy paths', function () { |
| 48 | + it('[CAS10] provisions appScope only (no subPath)', async () => { |
| 49 | + const conn = makeConn(() => ({ stream: { id: EXPECTED_APP_SCOPE } })); |
| 50 | + const out = await ensureAppScope(conn, APP_CODE); |
| 51 | + assert.equal(out, EXPECTED_APP_SCOPE); |
| 52 | + assert.equal(conn.calls.length, 1); |
| 53 | + assert.equal(conn.calls[0].method, 'streams.create'); |
| 54 | + assert.deepEqual(conn.calls[0].params, { |
| 55 | + id: EXPECTED_APP_SCOPE, |
| 56 | + parentId: ':_cmc:apps', |
| 57 | + name: APP_CODE |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('[CAS11] provisions appScope + subPath', async () => { |
| 62 | + const conn = makeConn(() => ({ stream: {} })); |
| 63 | + const out = await ensureAppScope(conn, APP_CODE, 'abc123'); |
| 64 | + assert.equal(out, EXPECTED_APP_SCOPE + ':abc123'); |
| 65 | + assert.equal(conn.calls.length, 2); |
| 66 | + assert.deepEqual(conn.calls[1].params, { |
| 67 | + id: EXPECTED_APP_SCOPE + ':abc123', |
| 68 | + parentId: EXPECTED_APP_SCOPE, |
| 69 | + name: 'abc123' |
| 70 | + }); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('[CAST] tolerated errors', function () { |
| 75 | + it('[CAS20] tolerates item-already-exists on appScope', async () => { |
| 76 | + const conn = makeConn(() => { throw pryvError('item-already-exists'); }); |
| 77 | + const out = await ensureAppScope(conn, APP_CODE); |
| 78 | + assert.equal(out, EXPECTED_APP_SCOPE); |
| 79 | + }); |
| 80 | + |
| 81 | + it('[CAS21] tolerates forbidden on appScope (OAuth-narrow access)', async () => { |
| 82 | + const conn = makeConn(() => { throw pryvError('forbidden'); }); |
| 83 | + const out = await ensureAppScope(conn, APP_CODE); |
| 84 | + assert.equal(out, EXPECTED_APP_SCOPE); |
| 85 | + }); |
| 86 | + |
| 87 | + it('[CAS22] tolerates item-already-exists on subPath', async () => { |
| 88 | + const conn = makeConn((method, params, idx) => { |
| 89 | + if (idx === 0) return { stream: {} }; |
| 90 | + throw pryvError('item-already-exists'); |
| 91 | + }); |
| 92 | + const out = await ensureAppScope(conn, APP_CODE, 'abc123'); |
| 93 | + assert.equal(out, EXPECTED_APP_SCOPE + ':abc123'); |
| 94 | + assert.equal(conn.calls.length, 2); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('[CASR] rethrows on unrelated errors', function () { |
| 99 | + it('[CAS30] rethrows non-tolerated pryv error on appScope', async () => { |
| 100 | + const conn = makeConn(() => { throw pryvError('invalid-parameters-format'); }); |
| 101 | + await assert.rejects( |
| 102 | + ensureAppScope(conn, APP_CODE), |
| 103 | + (e) => e.innerObject?.id === 'invalid-parameters-format' |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('[CAS31] rethrows forbidden on subPath (only appScope tolerates forbidden)', async () => { |
| 108 | + const conn = makeConn((method, params, idx) => { |
| 109 | + if (idx === 0) return { stream: {} }; |
| 110 | + throw pryvError('forbidden'); |
| 111 | + }); |
| 112 | + await assert.rejects( |
| 113 | + ensureAppScope(conn, APP_CODE, 'abc123'), |
| 114 | + (e) => e.innerObject?.id === 'forbidden' |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('[CAS32] rethrows plain Errors with no pryv id', async () => { |
| 119 | + const conn = makeConn(() => { throw new Error('boom'); }); |
| 120 | + await assert.rejects(ensureAppScope(conn, APP_CODE), /boom/); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments