|
| 1 | +/** |
| 2 | + * Example Protocol TCK Test |
| 3 | + * |
| 4 | + * Demonstrates how to use the Protocol TCK with a mock protocol endpoint |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, test, beforeEach } from 'vitest'; |
| 8 | +import { runProtocolTCK, ProtocolEndpoint, ProtocolOperation, ProtocolResponse } from '../src/index'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Mock in-memory protocol endpoint for testing the TCK itself |
| 12 | + */ |
| 13 | +class MockProtocolEndpoint implements ProtocolEndpoint { |
| 14 | + private data: Map<string, Map<string, any>> = new Map(); |
| 15 | + private idCounter = 0; |
| 16 | + |
| 17 | + async execute(operation: ProtocolOperation): Promise<ProtocolResponse> { |
| 18 | + try { |
| 19 | + const { type, entity, data, id, filter, options } = operation; |
| 20 | + |
| 21 | + // Ensure entity collection exists |
| 22 | + if (!this.data.has(entity)) { |
| 23 | + this.data.set(entity, new Map()); |
| 24 | + } |
| 25 | + |
| 26 | + const collection = this.data.get(entity)!; |
| 27 | + |
| 28 | + switch (type) { |
| 29 | + case 'create': { |
| 30 | + const newId = data?.id || `mock-${++this.idCounter}`; |
| 31 | + const newData = { |
| 32 | + ...data, |
| 33 | + id: newId, |
| 34 | + created_at: new Date().toISOString(), |
| 35 | + updated_at: new Date().toISOString() |
| 36 | + }; |
| 37 | + collection.set(newId, newData); |
| 38 | + return { success: true, data: newData }; |
| 39 | + } |
| 40 | + |
| 41 | + case 'read': { |
| 42 | + if (!id) { |
| 43 | + return { success: false, error: { code: '400', message: 'ID required' } }; |
| 44 | + } |
| 45 | + const item = collection.get(id); |
| 46 | + return { success: true, data: item || null }; |
| 47 | + } |
| 48 | + |
| 49 | + case 'update': { |
| 50 | + if (!id) { |
| 51 | + return { success: false, error: { code: '400', message: 'ID required' } }; |
| 52 | + } |
| 53 | + const existing = collection.get(id); |
| 54 | + if (!existing) { |
| 55 | + return { success: false, error: { code: '404', message: 'Not found' } }; |
| 56 | + } |
| 57 | + const updated = { |
| 58 | + ...existing, |
| 59 | + ...data, |
| 60 | + id, // Don't allow ID change |
| 61 | + updated_at: new Date().toISOString() |
| 62 | + }; |
| 63 | + collection.set(id, updated); |
| 64 | + return { success: true, data: updated }; |
| 65 | + } |
| 66 | + |
| 67 | + case 'delete': { |
| 68 | + if (!id) { |
| 69 | + return { success: false, error: { code: '400', message: 'ID required' } }; |
| 70 | + } |
| 71 | + const deleted = collection.delete(id); |
| 72 | + return { success: deleted, data: deleted ? { id } : null }; |
| 73 | + } |
| 74 | + |
| 75 | + case 'query': { |
| 76 | + let results = Array.from(collection.values()); |
| 77 | + |
| 78 | + // Apply filter |
| 79 | + if (filter) { |
| 80 | + results = results.filter(item => { |
| 81 | + return Object.entries(filter).every(([key, value]) => item[key] === value); |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + // Apply sorting |
| 86 | + if (options?.orderBy) { |
| 87 | + const sortField = options.orderBy[0]?.field; |
| 88 | + const sortOrder = options.orderBy[0]?.order?.toLowerCase(); |
| 89 | + if (sortField) { |
| 90 | + results.sort((a, b) => { |
| 91 | + const aVal = a[sortField]; |
| 92 | + const bVal = b[sortField]; |
| 93 | + const compare = aVal > bVal ? 1 : aVal < bVal ? -1 : 0; |
| 94 | + return sortOrder === 'desc' ? -compare : compare; |
| 95 | + }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Apply pagination |
| 100 | + if (options?.offset) { |
| 101 | + results = results.slice(options.offset); |
| 102 | + } |
| 103 | + if (options?.limit) { |
| 104 | + results = results.slice(0, options.limit); |
| 105 | + } |
| 106 | + |
| 107 | + return { success: true, data: results }; |
| 108 | + } |
| 109 | + |
| 110 | + case 'batch': { |
| 111 | + if (!Array.isArray(data)) { |
| 112 | + return { success: false, error: { code: '400', message: 'Batch requires array' } }; |
| 113 | + } |
| 114 | + |
| 115 | + const batchResults = []; |
| 116 | + for (const item of data) { |
| 117 | + const result = await this.execute({ |
| 118 | + type: 'create', |
| 119 | + entity, |
| 120 | + data: item |
| 121 | + }); |
| 122 | + if (result.data) { |
| 123 | + batchResults.push(result.data); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return { success: true, data: batchResults }; |
| 128 | + } |
| 129 | + |
| 130 | + default: |
| 131 | + return { |
| 132 | + success: false, |
| 133 | + error: { code: '400', message: `Unsupported operation: ${type}` } |
| 134 | + }; |
| 135 | + } |
| 136 | + } catch (error) { |
| 137 | + return { |
| 138 | + success: false, |
| 139 | + error: { |
| 140 | + code: '500', |
| 141 | + message: error instanceof Error ? error.message : 'Internal error' |
| 142 | + } |
| 143 | + }; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + async getMetadata(): Promise<any> { |
| 148 | + return { |
| 149 | + entities: Array.from(this.data.keys()), |
| 150 | + protocol: 'mock', |
| 151 | + version: '1.0.0' |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + async close(): Promise<void> { |
| 156 | + this.data.clear(); |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +// Run the TCK tests |
| 161 | +describe('Protocol TCK - Mock Protocol Example', () => { |
| 162 | + runProtocolTCK( |
| 163 | + () => new MockProtocolEndpoint(), |
| 164 | + 'Mock Protocol', |
| 165 | + { |
| 166 | + timeout: 10000, |
| 167 | + performance: { |
| 168 | + enabled: true, |
| 169 | + thresholds: { |
| 170 | + create: 10, |
| 171 | + read: 5, |
| 172 | + update: 10, |
| 173 | + delete: 5, |
| 174 | + query: 20, |
| 175 | + batch: 50 |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + ); |
| 180 | +}); |
0 commit comments