|
| 1 | +import type { CreateEntity, CreateRelation, DeleteEntity, DeleteRelation } from '@geoprotocol/grc-20'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import { createGeoClient } from '../client.js'; |
| 4 | +import { RANK_VOTES_RELATION_TYPE } from '../core/ids/system.js'; |
| 5 | +import { toGrcId } from '../id-utils.js'; |
| 6 | +import { defineGeoNetworkConfig } from '../networks.js'; |
| 7 | + |
| 8 | +const RANK_ID = 'b1dc6e5c63e143bab3d4755b251a4ea1'; |
| 9 | +const MOVIE_1 = 'f47ac10b58cc4372a5670e02b2c3d479'; |
| 10 | +const MOVIE_2 = '550e8400e29b41d4a716446655440000'; |
| 11 | +const SPACE_ID = 'd4bc2f205e2d415e971eb0b9fbf6b6fc'; |
| 12 | +const EXISTING_REL_1 = 'aaaaaaaa11114111811aaaaaaaaaaaaa'; |
| 13 | +const EXISTING_REL_2 = 'bbbbbbbb22224222822bbbbbbbbbbbbb'; |
| 14 | +const EXISTING_VOTE_1 = 'cccccccc33334333833ccccccccccccc'; |
| 15 | +const EXISTING_VOTE_2 = 'dddddddd44444444844ddddddddddddd'; |
| 16 | + |
| 17 | +function customNetwork() { |
| 18 | + return defineGeoNetworkConfig({ |
| 19 | + id: 'LOCAL', |
| 20 | + name: 'Local Geo', |
| 21 | + apiOrigin: 'http://localhost:3000', |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +function voteRelations(ops: Array<{ type: string }>) { |
| 26 | + return ops.filter( |
| 27 | + (op): op is CreateRelation => |
| 28 | + op.type === 'createRelation' && |
| 29 | + (op as CreateRelation).relationType.every((byte, index) => byte === toGrcId(RANK_VOTES_RELATION_TYPE)[index]), |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +describe('geo.ranks', () => { |
| 34 | + describe('update', () => { |
| 35 | + it('fetches existing vote relations then supersedes them with new votes', async () => { |
| 36 | + const fetch = vi.fn<typeof globalThis.fetch>().mockResolvedValue( |
| 37 | + new Response( |
| 38 | + JSON.stringify({ |
| 39 | + data: { |
| 40 | + entity: { |
| 41 | + relationsList: [ |
| 42 | + { id: EXISTING_REL_1, entityId: EXISTING_VOTE_1 }, |
| 43 | + { id: EXISTING_REL_2, entityId: EXISTING_VOTE_2 }, |
| 44 | + ], |
| 45 | + }, |
| 46 | + }, |
| 47 | + }), |
| 48 | + ), |
| 49 | + ); |
| 50 | + const geo = createGeoClient({ network: customNetwork(), fetch }); |
| 51 | + |
| 52 | + const result = await geo.ranks.update({ |
| 53 | + rankId: RANK_ID, |
| 54 | + rankType: 'ORDINAL', |
| 55 | + votes: [{ entityId: MOVIE_2, spaceId: SPACE_ID }], |
| 56 | + }); |
| 57 | + |
| 58 | + expect(fetch).toHaveBeenCalledWith('http://localhost:3000/graphql', expect.objectContaining({ method: 'POST' })); |
| 59 | + |
| 60 | + // Two relation deletes for the fetched relations |
| 61 | + const deletes = result.ops.filter((op): op is DeleteRelation => op.type === 'deleteRelation'); |
| 62 | + expect(deletes).toHaveLength(2); |
| 63 | + expect(deletes[0]?.id).toEqual(toGrcId(EXISTING_REL_1)); |
| 64 | + expect(deletes[1]?.id).toEqual(toGrcId(EXISTING_REL_2)); |
| 65 | + |
| 66 | + // Each superseded vote also deletes its reified vote entity |
| 67 | + const entityDeletes = result.ops.filter((op): op is DeleteEntity => op.type === 'deleteEntity'); |
| 68 | + expect(entityDeletes).toHaveLength(2); |
| 69 | + expect(entityDeletes[0]?.id).toEqual(toGrcId(EXISTING_VOTE_1)); |
| 70 | + expect(entityDeletes[1]?.id).toEqual(toGrcId(EXISTING_VOTE_2)); |
| 71 | + |
| 72 | + // One new vote relation + entity |
| 73 | + expect(voteRelations(result.ops)).toHaveLength(1); |
| 74 | + const voteEntity = result.ops.find((op): op is CreateEntity => op.type === 'createEntity'); |
| 75 | + expect(voteEntity).toBeDefined(); |
| 76 | + expect(result.voteIds).toHaveLength(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('emits only new votes when the rank has no existing votes', async () => { |
| 80 | + const fetch = vi |
| 81 | + .fn<typeof globalThis.fetch>() |
| 82 | + .mockResolvedValue(new Response(JSON.stringify({ data: { entity: { relationsList: [] } } }))); |
| 83 | + const geo = createGeoClient({ network: customNetwork(), fetch }); |
| 84 | + |
| 85 | + const result = await geo.ranks.update({ |
| 86 | + rankId: RANK_ID, |
| 87 | + rankType: 'ORDINAL', |
| 88 | + votes: [{ entityId: MOVIE_1, spaceId: SPACE_ID }], |
| 89 | + }); |
| 90 | + |
| 91 | + expect(result.ops.some(op => op.type === 'deleteRelation')).toBe(false); |
| 92 | + expect(voteRelations(result.ops)).toHaveLength(1); |
| 93 | + }); |
| 94 | + |
| 95 | + it('throws when the rank is not found', async () => { |
| 96 | + const fetch = vi |
| 97 | + .fn<typeof globalThis.fetch>() |
| 98 | + .mockResolvedValue(new Response(JSON.stringify({ data: { entity: null } }))); |
| 99 | + const geo = createGeoClient({ network: customNetwork(), fetch }); |
| 100 | + |
| 101 | + await expect( |
| 102 | + geo.ranks.update({ |
| 103 | + rankId: RANK_ID, |
| 104 | + rankType: 'ORDINAL', |
| 105 | + votes: [{ entityId: MOVIE_1, spaceId: SPACE_ID }], |
| 106 | + }), |
| 107 | + ).rejects.toThrow(`Rank ${RANK_ID} not found`); |
| 108 | + }); |
| 109 | + |
| 110 | + it('throws when the rankId is invalid', async () => { |
| 111 | + const fetch = vi.fn<typeof globalThis.fetch>(); |
| 112 | + const geo = createGeoClient({ network: customNetwork(), fetch }); |
| 113 | + |
| 114 | + await expect( |
| 115 | + geo.ranks.update({ |
| 116 | + rankId: 'invalid', |
| 117 | + rankType: 'ORDINAL', |
| 118 | + votes: [{ entityId: MOVIE_1, spaceId: SPACE_ID }], |
| 119 | + }), |
| 120 | + ).rejects.toThrow('Invalid id: "invalid" for `rankId` in `updateRank`'); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments