|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { CellType, type AppClient } from '@holochain/client'; |
| 3 | +import { loadGroupAnchors } from './anchors'; |
| 4 | + |
| 5 | +const dna = new Uint8Array([1, 2, 3]); |
| 6 | +const me = new Uint8Array([9, 9, 9]); |
| 7 | +const groupHash = new Uint8Array([5, 5, 5]); |
| 8 | + |
| 9 | +const groupRecord = { |
| 10 | + signed_action: { hashed: { hash: groupHash, content: { header: { author: me, timestamp: 1 } } } }, |
| 11 | + entry: { Present: { entry: { name: 'Pilot' } } } |
| 12 | +}; |
| 13 | + |
| 14 | +function anchorRecord(name: string, ndoDna: Uint8Array, identity: Uint8Array) { |
| 15 | + return { |
| 16 | + signed_action: { hashed: { hash: new Uint8Array([7]), content: { header: { author: me, timestamp: 1 } } } }, |
| 17 | + entry: { |
| 18 | + Present: { |
| 19 | + entry: { name, ndo_dna_hash: ndoDna, identity_action_hash: identity, network_seed: `seed_${name}` } |
| 20 | + } |
| 21 | + } |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +function client(calls: Record<string, unknown>) { |
| 26 | + const callZome = vi.fn(async (request: { fn_name: string }) => calls[request.fn_name]); |
| 27 | + const appInfo = vi.fn(async () => ({ |
| 28 | + cell_info: { |
| 29 | + group: [{ type: CellType.Provisioned, value: { cell_id: [dna, me], dna_modifiers: { network_seed: 's' } } }] |
| 30 | + } |
| 31 | + })); |
| 32 | + return { callZome, appInfo, instance: { callZome, appInfo } as unknown as AppClient }; |
| 33 | +} |
| 34 | + |
| 35 | +describe('loadGroupAnchors', () => { |
| 36 | + it('reads the NDO anchors of the instance group cell through the applet client', async () => { |
| 37 | + const { instance, callZome } = client({ |
| 38 | + get_my_group: groupRecord, |
| 39 | + get_ndo_anchors: [anchorRecord('Seed library', new Uint8Array([1]), new Uint8Array([2]))] |
| 40 | + }); |
| 41 | + const anchors = await loadGroupAnchors(instance); |
| 42 | + expect(anchors.map((a) => a.name)).toEqual(['Seed library']); |
| 43 | + const fns = callZome.mock.calls.map(([request]) => (request as { fn_name: string }).fn_name); |
| 44 | + expect(fns).toEqual(['get_my_group', 'get_ndo_anchors']); |
| 45 | + const first = callZome.mock.calls[0]?.[0] as unknown as { cell_id: unknown; zome_name: string }; |
| 46 | + expect(first.zome_name).toBe('zome_group'); |
| 47 | + expect(first.cell_id).toEqual([dna, me]); |
| 48 | + }); |
| 49 | + |
| 50 | + it('skips anchors that lack clone coordinates', async () => { |
| 51 | + const { instance } = client({ |
| 52 | + get_my_group: groupRecord, |
| 53 | + get_ndo_anchors: [ |
| 54 | + anchorRecord('Good', new Uint8Array([1]), new Uint8Array([2])), |
| 55 | + { signed_action: groupRecord.signed_action, entry: { Present: { entry: { name: 'Broken' } } } } |
| 56 | + ] |
| 57 | + }); |
| 58 | + expect((await loadGroupAnchors(instance)).map((a) => a.name)).toEqual(['Good']); |
| 59 | + }); |
| 60 | + |
| 61 | + it('answers nothing before the group profile has synced', async () => { |
| 62 | + const { instance, callZome } = client({ get_my_group: null }); |
| 63 | + expect(await loadGroupAnchors(instance)).toEqual([]); |
| 64 | + expect(callZome).toHaveBeenCalledTimes(1); |
| 65 | + }); |
| 66 | + |
| 67 | + it('answers nothing when the app has no group cell', async () => { |
| 68 | + const instance = { appInfo: async () => ({ cell_info: {} }), callZome: vi.fn() } as unknown as AppClient; |
| 69 | + expect(await loadGroupAnchors(instance)).toEqual([]); |
| 70 | + }); |
| 71 | +}); |
0 commit comments