|
| 1 | +import { describe, it, beforeEach, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + DefaultRequestHandler, |
| 5 | + InMemoryPushNotificationStore, |
| 6 | + InMemoryTaskStore, |
| 7 | + TaskStore, |
| 8 | +} from '../../../src/server/index.js'; |
| 9 | +import { |
| 10 | + AgentCard, |
| 11 | + Task, |
| 12 | + TaskPushNotificationConfig, |
| 13 | + TaskState, |
| 14 | +} from '../../../src/types/pb/a2a.js'; |
| 15 | +import { DefaultExecutionEventBusManager } from '../../../src/server/events/execution_event_bus_manager.js'; |
| 16 | +import { ServerCallContext } from '../../../src/server/context.js'; |
| 17 | +import { MockAgentExecutor } from '../mocks/agent-executor.mock.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Focused coverage for {@link DefaultRequestHandler.createTaskPushNotificationConfig} |
| 21 | + * per spec §3.1.7 ("Created configuration with assigned ID") and §5.1 |
| 22 | + * (functional equivalence across transports). |
| 23 | + * |
| 24 | + * The contract verified here: |
| 25 | + * |
| 26 | + * 1. **Id-less Create** — when `params.id` is empty the handler MUST |
| 27 | + * assign a server-side UUID and return the persisted record. Prior |
| 28 | + * to this fix the store's `id ||= taskId` fallback collapsed every |
| 29 | + * parameter-less Create onto a single row, silently overwriting |
| 30 | + * previous configs. |
| 31 | + * |
| 32 | + * 2. **Multiple id-less Creates** — must produce distinct entries, |
| 33 | + * each with its own UUID. Regression guard for the same |
| 34 | + * destructive-upsert path. |
| 35 | + * |
| 36 | + * 3. **Explicit id** — when the caller supplies a non-empty id the |
| 37 | + * handler MUST persist under that id and return the stored shape |
| 38 | + * (not the input params reference), so the caller observes any |
| 39 | + * normalization the store performed. |
| 40 | + * |
| 41 | + * 4. **List after multi-Create** — `listTaskPushNotificationConfigs` |
| 42 | + * returns every entry created above (id-less + explicit), proving |
| 43 | + * the records weren't merged. |
| 44 | + * |
| 45 | + * Mirrors a2a-go's UUIDv7-based store (`a2asrv/push/store.go:45-48`) and |
| 46 | + * a2a-python's parameter-less Create handling. |
| 47 | + */ |
| 48 | +describe('DefaultRequestHandler.createTaskPushNotificationConfig (§3.1.7, §5.1)', () => { |
| 49 | + let handler: DefaultRequestHandler; |
| 50 | + let taskStore: TaskStore; |
| 51 | + let pushNotificationStore: InMemoryPushNotificationStore; |
| 52 | + |
| 53 | + const agentCard: AgentCard = { |
| 54 | + name: 'Push Notification Agent', |
| 55 | + description: 'Test agent for §3.1.7 / §5.1 push-notification create', |
| 56 | + version: '1.0.0', |
| 57 | + provider: undefined, |
| 58 | + documentationUrl: '', |
| 59 | + supportedInterfaces: [ |
| 60 | + { |
| 61 | + url: 'http://localhost/a2a', |
| 62 | + protocolBinding: 'HTTP+JSON', |
| 63 | + tenant: '', |
| 64 | + protocolVersion: '1.0', |
| 65 | + }, |
| 66 | + ], |
| 67 | + capabilities: { |
| 68 | + extensions: [], |
| 69 | + streaming: true, |
| 70 | + pushNotifications: true, |
| 71 | + }, |
| 72 | + securitySchemes: {}, |
| 73 | + securityRequirements: [], |
| 74 | + defaultInputModes: ['text/plain'], |
| 75 | + defaultOutputModes: ['text/plain'], |
| 76 | + skills: [], |
| 77 | + signatures: [], |
| 78 | + }; |
| 79 | + |
| 80 | + const serverContext = new ServerCallContext(); |
| 81 | + |
| 82 | + beforeEach(async () => { |
| 83 | + taskStore = new InMemoryTaskStore(); |
| 84 | + pushNotificationStore = new InMemoryPushNotificationStore(); |
| 85 | + handler = new DefaultRequestHandler( |
| 86 | + agentCard, |
| 87 | + taskStore, |
| 88 | + new MockAgentExecutor(), |
| 89 | + new DefaultExecutionEventBusManager(), |
| 90 | + pushNotificationStore |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + const makeTask = (id: string): Task => ({ |
| 95 | + id, |
| 96 | + contextId: `ctx-${id}`, |
| 97 | + status: { state: TaskState.TASK_STATE_WORKING, message: undefined, timestamp: undefined }, |
| 98 | + artifacts: [], |
| 99 | + history: [], |
| 100 | + metadata: {}, |
| 101 | + }); |
| 102 | + |
| 103 | + const makeIdLessConfig = (taskId: string, url: string): TaskPushNotificationConfig => ({ |
| 104 | + tenant: '', |
| 105 | + taskId, |
| 106 | + id: '', |
| 107 | + url, |
| 108 | + token: '', |
| 109 | + authentication: undefined, |
| 110 | + }); |
| 111 | + |
| 112 | + // §RFC 4122 v4: 8-4-4-4-12 hex digits, version nibble 4. The handler |
| 113 | + // uses `uuid.v4`; this matcher catches accidental swaps to other id |
| 114 | + // schemes (e.g. taskId fallback) without coupling to library specifics. |
| 115 | + const UUIDV4_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; |
| 116 | + |
| 117 | + it('assigns a server-side UUID when params.id is empty and returns the persisted record', async () => { |
| 118 | + const taskId = 'task-idless-single'; |
| 119 | + await taskStore.save(makeTask(taskId), serverContext); |
| 120 | + |
| 121 | + const result = await handler.createTaskPushNotificationConfig( |
| 122 | + makeIdLessConfig(taskId, 'https://example.test/webhook-1'), |
| 123 | + serverContext |
| 124 | + ); |
| 125 | + |
| 126 | + expect(result.id).toMatch(UUIDV4_RE); |
| 127 | + expect(result.taskId).toBe(taskId); |
| 128 | + expect(result.url).toBe('https://example.test/webhook-1'); |
| 129 | + |
| 130 | + // Verify persistence: the record returned must be the one in the |
| 131 | + // store, not a reflection of the input. |
| 132 | + const stored = await pushNotificationStore.load(taskId, serverContext); |
| 133 | + expect(stored).toHaveLength(1); |
| 134 | + expect(stored[0].id).toBe(result.id); |
| 135 | + }); |
| 136 | + |
| 137 | + it('produces distinct UUIDs for two id-less Creates (no silent upsert)', async () => { |
| 138 | + // Regression guard for the old `id ||= taskId` store fallback: two |
| 139 | + // parameter-less Creates used to collapse onto a single row keyed by |
| 140 | + // taskId, destroying the first config. They must now coexist with |
| 141 | + // distinct server-assigned ids. |
| 142 | + const taskId = 'task-idless-multi'; |
| 143 | + await taskStore.save(makeTask(taskId), serverContext); |
| 144 | + |
| 145 | + const first = await handler.createTaskPushNotificationConfig( |
| 146 | + makeIdLessConfig(taskId, 'https://example.test/webhook-A'), |
| 147 | + serverContext |
| 148 | + ); |
| 149 | + const second = await handler.createTaskPushNotificationConfig( |
| 150 | + makeIdLessConfig(taskId, 'https://example.test/webhook-B'), |
| 151 | + serverContext |
| 152 | + ); |
| 153 | + |
| 154 | + expect(first.id).toMatch(UUIDV4_RE); |
| 155 | + expect(second.id).toMatch(UUIDV4_RE); |
| 156 | + expect(first.id).not.toBe(second.id); |
| 157 | + |
| 158 | + const stored = await pushNotificationStore.load(taskId, serverContext); |
| 159 | + expect(stored).toHaveLength(2); |
| 160 | + expect(stored.map((c) => c.id).sort()).toEqual([first.id, second.id].sort()); |
| 161 | + expect(stored.map((c) => c.url).sort()).toEqual([ |
| 162 | + 'https://example.test/webhook-A', |
| 163 | + 'https://example.test/webhook-B', |
| 164 | + ]); |
| 165 | + }); |
| 166 | + |
| 167 | + it('preserves an explicit id and returns a deep clone (caller mutations cannot reach the store)', async () => { |
| 168 | + const taskId = 'task-explicit-id'; |
| 169 | + await taskStore.save(makeTask(taskId), serverContext); |
| 170 | + |
| 171 | + const params: TaskPushNotificationConfig = { |
| 172 | + tenant: '', |
| 173 | + taskId, |
| 174 | + id: 'caller-chosen-id', |
| 175 | + url: 'https://example.test/webhook-explicit', |
| 176 | + token: 'shh', |
| 177 | + authentication: undefined, |
| 178 | + }; |
| 179 | + const result = await handler.createTaskPushNotificationConfig(params, serverContext); |
| 180 | + |
| 181 | + expect(result.id).toBe('caller-chosen-id'); |
| 182 | + expect(result.url).toBe('https://example.test/webhook-explicit'); |
| 183 | + |
| 184 | + // The returned object must be a deep clone, not the input reference — |
| 185 | + // caller-side mutations of the returned value must not reach the |
| 186 | + // store's internal entry. Same isolation guarantee `store.load()` |
| 187 | + // provides. |
| 188 | + expect(result).not.toBe(params); |
| 189 | + result.url = 'https://attacker.test/'; |
| 190 | + const stored = await pushNotificationStore.load(taskId, serverContext); |
| 191 | + expect(stored[0].url).toBe('https://example.test/webhook-explicit'); |
| 192 | + }); |
| 193 | + |
| 194 | + it('listTaskPushNotificationConfigs returns every entry after mixed id-less + explicit Creates', async () => { |
| 195 | + const taskId = 'task-list-after-multi'; |
| 196 | + await taskStore.save(makeTask(taskId), serverContext); |
| 197 | + |
| 198 | + const idless1 = await handler.createTaskPushNotificationConfig( |
| 199 | + makeIdLessConfig(taskId, 'https://example.test/wh-1'), |
| 200 | + serverContext |
| 201 | + ); |
| 202 | + const explicit = await handler.createTaskPushNotificationConfig( |
| 203 | + { |
| 204 | + tenant: '', |
| 205 | + taskId, |
| 206 | + id: 'pinned', |
| 207 | + url: 'https://example.test/wh-2', |
| 208 | + token: '', |
| 209 | + authentication: undefined, |
| 210 | + }, |
| 211 | + serverContext |
| 212 | + ); |
| 213 | + const idless2 = await handler.createTaskPushNotificationConfig( |
| 214 | + makeIdLessConfig(taskId, 'https://example.test/wh-3'), |
| 215 | + serverContext |
| 216 | + ); |
| 217 | + |
| 218 | + const list = await handler.listTaskPushNotificationConfigs( |
| 219 | + { tenant: '', taskId, pageSize: 0, pageToken: '' }, |
| 220 | + serverContext |
| 221 | + ); |
| 222 | + |
| 223 | + expect(list.configs).toHaveLength(3); |
| 224 | + const idsSeen = list.configs.map((c) => c.id).sort(); |
| 225 | + expect(idsSeen).toEqual([idless1.id, explicit.id, idless2.id].sort()); |
| 226 | + }); |
| 227 | +}); |
0 commit comments