|
| 1 | +import { assert } from './test-utils/deps-node.js'; |
| 2 | +import { Contact } from '../ts/appTemplates/Contact.ts'; |
| 3 | +import { CollectorClient } from '../ts/appTemplates/CollectorClient.ts'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Unit tests for access update request functionality. |
| 7 | + * Tests the data structures and Contact-level getters. |
| 8 | + * Integration tests (actual API calls) are in apptemplates.test.js. |
| 9 | + */ |
| 10 | + |
| 11 | +// ---- Test helpers ---- // |
| 12 | + |
| 13 | +function makeSource (overrides = {}) { |
| 14 | + return { |
| 15 | + remoteUsername: 'dr-alice', |
| 16 | + displayName: 'Dr. Alice', |
| 17 | + chatStreams: null, |
| 18 | + appStreamId: null, |
| 19 | + permissions: [{ streamId: 'health', level: 'read' }], |
| 20 | + status: 'Active', |
| 21 | + type: 'collector', |
| 22 | + accessId: 'acc-1', |
| 23 | + ...overrides |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function makeMockCollectorClient (overrides = {}) { |
| 28 | + return { |
| 29 | + status: 'Active', |
| 30 | + pendingUpdate: null, |
| 31 | + key: 'dr-alice:a-form1', |
| 32 | + requesterUsername: 'dr-alice', |
| 33 | + hasChatFeature: false, |
| 34 | + ...overrides |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +describe('[AUPD] Access Update Requests', function () { |
| 39 | + // ---- Contact.hasPendingUpdate ---- // |
| 40 | + |
| 41 | + describe('[AUCT] Contact.hasPendingUpdate', function () { |
| 42 | + it('[AU01] should return false when no collectorClients', () => { |
| 43 | + const c = new Contact('dr-alice', 'Dr. Alice'); |
| 44 | + c.addSource(makeSource()); |
| 45 | + assert.equal(c.hasPendingUpdate, false); |
| 46 | + }); |
| 47 | + |
| 48 | + it('[AU02] should return false when no pending updates', () => { |
| 49 | + const c = new Contact('dr-alice', 'Dr. Alice'); |
| 50 | + c.addSource(makeSource()); |
| 51 | + c.addCollectorClient(makeMockCollectorClient()); |
| 52 | + assert.equal(c.hasPendingUpdate, false); |
| 53 | + }); |
| 54 | + |
| 55 | + it('[AU03] should return true when a collectorClient has pendingUpdate', () => { |
| 56 | + const c = new Contact('dr-alice', 'Dr. Alice'); |
| 57 | + c.addSource(makeSource()); |
| 58 | + c.addCollectorClient(makeMockCollectorClient({ |
| 59 | + pendingUpdate: { |
| 60 | + eventId: 'evt-1', |
| 61 | + content: { |
| 62 | + version: 0, |
| 63 | + targetAccessName: 'dr-alice:a-form1', |
| 64 | + action: 'update-permissions', |
| 65 | + permissions: [{ streamId: 'diary', level: 'read' }] |
| 66 | + } |
| 67 | + } |
| 68 | + })); |
| 69 | + assert.equal(c.hasPendingUpdate, true); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + // ---- Contact.pendingUpdateClients ---- // |
| 74 | + |
| 75 | + describe('[AUPC] Contact.pendingUpdateClients', function () { |
| 76 | + it('[AU04] should return empty array when no pending updates', () => { |
| 77 | + const c = new Contact('dr-alice', 'Dr. Alice'); |
| 78 | + c.addCollectorClient(makeMockCollectorClient()); |
| 79 | + c.addCollectorClient(makeMockCollectorClient({ key: 'dr-alice:a-form2' })); |
| 80 | + assert.equal(c.pendingUpdateClients.length, 0); |
| 81 | + }); |
| 82 | + |
| 83 | + it('[AU05] should return only clients with pending updates', () => { |
| 84 | + const c = new Contact('dr-alice', 'Dr. Alice'); |
| 85 | + c.addCollectorClient(makeMockCollectorClient()); |
| 86 | + c.addCollectorClient(makeMockCollectorClient({ |
| 87 | + key: 'dr-alice:a-form2', |
| 88 | + pendingUpdate: { |
| 89 | + eventId: 'evt-2', |
| 90 | + content: { |
| 91 | + version: 0, |
| 92 | + targetAccessName: 'dr-alice:a-form2', |
| 93 | + action: 'update-permissions', |
| 94 | + permissions: [{ streamId: 'diary', level: 'contribute' }] |
| 95 | + } |
| 96 | + } |
| 97 | + })); |
| 98 | + assert.equal(c.pendingUpdateClients.length, 1); |
| 99 | + assert.equal(c.pendingUpdateClients[0].key, 'dr-alice:a-form2'); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + // ---- CollectorClient.pendingUpdate field ---- // |
| 104 | + |
| 105 | + describe('[AUCC] CollectorClient.pendingUpdate', function () { |
| 106 | + it('[AU06] should default to null', () => { |
| 107 | + // pendingUpdate is a plain field, tested via mock |
| 108 | + const cc = makeMockCollectorClient(); |
| 109 | + assert.equal(cc.pendingUpdate, null); |
| 110 | + }); |
| 111 | + |
| 112 | + it('[AU07] should hold update request data when set', () => { |
| 113 | + const updateReq = { |
| 114 | + eventId: 'evt-1', |
| 115 | + content: { |
| 116 | + version: 0, |
| 117 | + targetAccessName: 'dr-alice:a-form1', |
| 118 | + action: 'update-permissions', |
| 119 | + permissions: [ |
| 120 | + { streamId: 'health', level: 'read' }, |
| 121 | + { streamId: 'diary', level: 'contribute' } |
| 122 | + ], |
| 123 | + features: { chat: { type: 'user' } }, |
| 124 | + message: 'I\'d like to also access your diary entries.' |
| 125 | + } |
| 126 | + }; |
| 127 | + const cc = makeMockCollectorClient({ pendingUpdate: updateReq }); |
| 128 | + assert.equal(cc.pendingUpdate.eventId, 'evt-1'); |
| 129 | + assert.equal(cc.pendingUpdate.content.action, 'update-permissions'); |
| 130 | + assert.equal(cc.pendingUpdate.content.permissions.length, 2); |
| 131 | + assert.equal(cc.pendingUpdate.content.message, 'I\'d like to also access your diary entries.'); |
| 132 | + assert.deepEqual(cc.pendingUpdate.content.features, { chat: { type: 'user' } }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + // ---- AccessUpdateRequestContent structure ---- // |
| 137 | + |
| 138 | + describe('[AURC] AccessUpdateRequestContent schema', function () { |
| 139 | + it('[AU08] should validate minimal update request content', () => { |
| 140 | + const content = { |
| 141 | + version: 0, |
| 142 | + targetAccessName: 'dr-alice:a-form1', |
| 143 | + action: 'update-permissions', |
| 144 | + permissions: [{ streamId: 'health', level: 'manage' }] |
| 145 | + }; |
| 146 | + assert.equal(content.version, 0); |
| 147 | + assert.equal(content.targetAccessName, 'dr-alice:a-form1'); |
| 148 | + assert.equal(content.action, 'update-permissions'); |
| 149 | + assert.equal(content.permissions.length, 1); |
| 150 | + assert.equal(content.permissions[0].streamId, 'health'); |
| 151 | + }); |
| 152 | + |
| 153 | + it('[AU09] should support add-feature action', () => { |
| 154 | + const content = { |
| 155 | + version: 0, |
| 156 | + targetAccessName: 'dr-alice:a-form1', |
| 157 | + action: 'add-feature', |
| 158 | + permissions: [{ streamId: 'health', level: 'read' }], |
| 159 | + features: { chat: { type: 'user' } }, |
| 160 | + message: 'Adding chat capability' |
| 161 | + }; |
| 162 | + assert.equal(content.action, 'add-feature'); |
| 163 | + assert.ok(content.features.chat); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + // ---- Multiple contacts with mixed update states ---- // |
| 168 | + |
| 169 | + describe('[AUMX] Mixed contacts with updates', function () { |
| 170 | + it('[AU10] should correctly report updates across multiple contacts', () => { |
| 171 | + const alice = new Contact('dr-alice', 'Dr. Alice'); |
| 172 | + alice.addSource(makeSource()); |
| 173 | + alice.addCollectorClient(makeMockCollectorClient({ |
| 174 | + pendingUpdate: { |
| 175 | + eventId: 'evt-1', |
| 176 | + content: { version: 0, targetAccessName: 'dr-alice:a-form1', action: 'update-permissions', permissions: [] } |
| 177 | + } |
| 178 | + })); |
| 179 | + |
| 180 | + const bob = new Contact('dr-bob', 'Dr. Bob'); |
| 181 | + bob.addSource(makeSource({ remoteUsername: 'dr-bob', displayName: 'Dr. Bob' })); |
| 182 | + bob.addCollectorClient(makeMockCollectorClient({ key: 'dr-bob:a-form1', pendingUpdate: null })); |
| 183 | + |
| 184 | + assert.equal(alice.hasPendingUpdate, true); |
| 185 | + assert.equal(bob.hasPendingUpdate, false); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments