|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { LinearGraphitiBridge } from '../linear-graphiti-bridge.js'; |
| 3 | +import type { LinearWebhookPayload } from '../../linear/webhook.js'; |
| 4 | + |
| 5 | +vi.mock('../../../core/monitoring/logger.js', () => ({ |
| 6 | + logger: { |
| 7 | + info: vi.fn(), |
| 8 | + debug: vi.fn(), |
| 9 | + warn: vi.fn(), |
| 10 | + error: vi.fn(), |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +function makePayload( |
| 15 | + overrides: Partial<LinearWebhookPayload> = {} |
| 16 | +): LinearWebhookPayload { |
| 17 | + return { |
| 18 | + action: 'create', |
| 19 | + createdAt: new Date().toISOString(), |
| 20 | + type: 'Issue', |
| 21 | + url: 'https://linear.app/test/issue/STA-1', |
| 22 | + webhookId: 'wh-1', |
| 23 | + webhookTimestamp: Date.now(), |
| 24 | + data: { |
| 25 | + id: 'issue-1', |
| 26 | + identifier: 'STA-1', |
| 27 | + title: 'Fix the bug', |
| 28 | + state: { id: 's1', name: 'In Progress', type: 'started' }, |
| 29 | + priority: 2, |
| 30 | + assignee: { id: 'u1', name: 'Alice', email: 'alice@test.com' }, |
| 31 | + team: { id: 't1', key: 'STA', name: 'Stack Team' }, |
| 32 | + labels: [{ id: 'l1', name: 'bug', color: '#ff0000' }], |
| 33 | + updatedAt: new Date().toISOString(), |
| 34 | + }, |
| 35 | + ...overrides, |
| 36 | + }; |
| 37 | +} |
| 38 | + |
| 39 | +function mockClient(bridge: LinearGraphitiBridge) { |
| 40 | + const client = { |
| 41 | + upsertEpisode: vi.fn().mockResolvedValue({ id: 'ep-1' }), |
| 42 | + upsertEntities: vi |
| 43 | + .fn() |
| 44 | + .mockResolvedValue({ ids: ['iss-1', 'per-1', 'team-1', 'lbl-1'] }), |
| 45 | + upsertRelations: vi.fn().mockResolvedValue({ ids: ['r1', 'r2', 'r3'] }), |
| 46 | + getStatus: vi.fn(), |
| 47 | + queryTemporal: vi.fn(), |
| 48 | + }; |
| 49 | + (bridge as any).client = client; |
| 50 | + return client; |
| 51 | +} |
| 52 | + |
| 53 | +describe('LinearGraphitiBridge', () => { |
| 54 | + let bridge: LinearGraphitiBridge; |
| 55 | + let client: ReturnType<typeof mockClient>; |
| 56 | + |
| 57 | + beforeEach(() => { |
| 58 | + bridge = new LinearGraphitiBridge({ endpoint: 'http://localhost:9999' }); |
| 59 | + client = mockClient(bridge); |
| 60 | + }); |
| 61 | + |
| 62 | + // ── Episode creation ── |
| 63 | + |
| 64 | + describe('episode creation', () => { |
| 65 | + it('creates episode for create action', async () => { |
| 66 | + await bridge.processWebhook(makePayload({ action: 'create' })); |
| 67 | + |
| 68 | + expect(client.upsertEpisode).toHaveBeenCalledOnce(); |
| 69 | + const ep = client.upsertEpisode.mock.calls[0][0]; |
| 70 | + expect(ep.type).toBe('linear_issue_create'); |
| 71 | + expect(ep.source).toBe('linear'); |
| 72 | + expect(ep.content.identifier).toBe('STA-1'); |
| 73 | + expect(ep.content.title).toBe('Fix the bug'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('creates episode for update action', async () => { |
| 77 | + await bridge.processWebhook(makePayload({ action: 'update' })); |
| 78 | + |
| 79 | + const ep = client.upsertEpisode.mock.calls[0][0]; |
| 80 | + expect(ep.type).toBe('linear_issue_update'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('creates episode for remove action', async () => { |
| 84 | + await bridge.processWebhook(makePayload({ action: 'remove' })); |
| 85 | + |
| 86 | + const ep = client.upsertEpisode.mock.calls[0][0]; |
| 87 | + expect(ep.type).toBe('linear_issue_remove'); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + // ── Entity extraction ── |
| 92 | + |
| 93 | + describe('entity extraction', () => { |
| 94 | + it('upserts Issue, Person, Team, and Label entities', async () => { |
| 95 | + await bridge.processWebhook(makePayload()); |
| 96 | + |
| 97 | + expect(client.upsertEntities).toHaveBeenCalledOnce(); |
| 98 | + const entities = client.upsertEntities.mock.calls[0][0]; |
| 99 | + expect(entities).toHaveLength(4); |
| 100 | + expect(entities[0].type).toBe('Issue'); |
| 101 | + expect(entities[0].name).toBe('STA-1'); |
| 102 | + expect(entities[1].type).toBe('Person'); |
| 103 | + expect(entities[1].name).toBe('Alice'); |
| 104 | + expect(entities[2].type).toBe('Team'); |
| 105 | + expect(entities[2].name).toBe('Stack Team'); |
| 106 | + expect(entities[3].type).toBe('Label'); |
| 107 | + expect(entities[3].name).toBe('bug'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('skips entities on remove action', async () => { |
| 111 | + await bridge.processWebhook(makePayload({ action: 'remove' })); |
| 112 | + |
| 113 | + expect(client.upsertEntities).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('handles missing assignee', async () => { |
| 117 | + const payload = makePayload(); |
| 118 | + payload.data.assignee = undefined; |
| 119 | + client.upsertEntities.mockResolvedValue({ |
| 120 | + ids: ['iss-1', 'team-1', 'lbl-1'], |
| 121 | + }); |
| 122 | + |
| 123 | + await bridge.processWebhook(payload); |
| 124 | + |
| 125 | + const entities = client.upsertEntities.mock.calls[0][0]; |
| 126 | + expect(entities.find((e: any) => e.type === 'Person')).toBeUndefined(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('handles missing team', async () => { |
| 130 | + const payload = makePayload(); |
| 131 | + payload.data.team = undefined; |
| 132 | + client.upsertEntities.mockResolvedValue({ |
| 133 | + ids: ['iss-1', 'per-1', 'lbl-1'], |
| 134 | + }); |
| 135 | + |
| 136 | + await bridge.processWebhook(payload); |
| 137 | + |
| 138 | + const entities = client.upsertEntities.mock.calls[0][0]; |
| 139 | + expect(entities.find((e: any) => e.type === 'Team')).toBeUndefined(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('handles missing labels', async () => { |
| 143 | + const payload = makePayload(); |
| 144 | + payload.data.labels = undefined; |
| 145 | + client.upsertEntities.mockResolvedValue({ |
| 146 | + ids: ['iss-1', 'per-1', 'team-1'], |
| 147 | + }); |
| 148 | + |
| 149 | + await bridge.processWebhook(payload); |
| 150 | + |
| 151 | + const entities = client.upsertEntities.mock.calls[0][0]; |
| 152 | + expect(entities.find((e: any) => e.type === 'Label')).toBeUndefined(); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + // ── Relation creation ── |
| 157 | + |
| 158 | + describe('relation creation', () => { |
| 159 | + it('creates ASSIGNED_TO, BELONGS_TO, HAS_LABEL relations', async () => { |
| 160 | + await bridge.processWebhook(makePayload()); |
| 161 | + |
| 162 | + expect(client.upsertRelations).toHaveBeenCalledOnce(); |
| 163 | + const relations = client.upsertRelations.mock.calls[0][0]; |
| 164 | + expect(relations).toHaveLength(3); |
| 165 | + expect(relations[0].type).toBe('ASSIGNED_TO'); |
| 166 | + expect(relations[0].fromId).toBe('iss-1'); |
| 167 | + expect(relations[0].toId).toBe('per-1'); |
| 168 | + expect(relations[1].type).toBe('BELONGS_TO'); |
| 169 | + expect(relations[1].toId).toBe('team-1'); |
| 170 | + expect(relations[2].type).toBe('HAS_LABEL'); |
| 171 | + expect(relations[2].toId).toBe('lbl-1'); |
| 172 | + }); |
| 173 | + |
| 174 | + it('skips relations on remove action', async () => { |
| 175 | + await bridge.processWebhook(makePayload({ action: 'remove' })); |
| 176 | + |
| 177 | + expect(client.upsertRelations).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | + |
| 180 | + it('skips upsertRelations when no relations exist', async () => { |
| 181 | + const payload = makePayload(); |
| 182 | + payload.data.assignee = undefined; |
| 183 | + payload.data.team = undefined; |
| 184 | + payload.data.labels = undefined; |
| 185 | + client.upsertEntities.mockResolvedValue({ ids: ['iss-1'] }); |
| 186 | + |
| 187 | + await bridge.processWebhook(payload); |
| 188 | + |
| 189 | + expect(client.upsertRelations).not.toHaveBeenCalled(); |
| 190 | + }); |
| 191 | + }); |
| 192 | + |
| 193 | + // ── Error resilience ── |
| 194 | + |
| 195 | + describe('error resilience', () => { |
| 196 | + it('catches errors without propagating', async () => { |
| 197 | + client.upsertEpisode.mockRejectedValue(new Error('network fail')); |
| 198 | + |
| 199 | + // Should not throw |
| 200 | + await bridge.processWebhook(makePayload()); |
| 201 | + |
| 202 | + expect(client.upsertEpisode).toHaveBeenCalledOnce(); |
| 203 | + }); |
| 204 | + |
| 205 | + it('catches entity upsert errors without propagating', async () => { |
| 206 | + client.upsertEntities.mockRejectedValue(new Error('entity fail')); |
| 207 | + |
| 208 | + await bridge.processWebhook(makePayload()); |
| 209 | + |
| 210 | + expect(client.upsertEpisode).toHaveBeenCalledOnce(); |
| 211 | + expect(client.upsertEntities).toHaveBeenCalledOnce(); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments