|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + * |
| 4 | + * Guards the Algolia tools that put an index name into a request **body** |
| 5 | + * against it arriving as a JSON number. |
| 6 | + * |
| 7 | + * The path-zone sites already run through `safeUrlPathSegment`, whose |
| 8 | + * `toGuardedString` stringifies a number; the body-zone sites kept a bare |
| 9 | + * `.trim()`. An index literally named `2024` is ordinary, and a |
| 10 | + * `visibility: 'user-or-llm'` slot filled with `2024` reaches the builder as a |
| 11 | + * JSON number — `TypeError: x.trim is not a function`, surfaced as a tool crash |
| 12 | + * rather than a validation error. Sending the string `"undefined"` as an index |
| 13 | + * name instead of reporting the missing parameter would be no better, so the |
| 14 | + * nullish case is rejected before coercion. |
| 15 | + */ |
| 16 | +import { describe, expect, it } from 'vitest' |
| 17 | +import { copyMoveIndexTool } from '@/tools/algolia/copy_move_index' |
| 18 | +import { getRecordsTool } from '@/tools/algolia/get_records' |
| 19 | +import { searchTool } from '@/tools/algolia/search' |
| 20 | +import type { ToolConfig } from '@/tools/types' |
| 21 | + |
| 22 | +type AnyTool = ToolConfig<any, any> |
| 23 | + |
| 24 | +const BASE = { applicationId: 'APPID', apiKey: 'KEY' } |
| 25 | + |
| 26 | +function bodyOf(tool: AnyTool, params: Record<string, unknown>): Record<string, any> { |
| 27 | + const build = tool.request.body as (p: Record<string, unknown>) => Record<string, any> |
| 28 | + return build({ ...BASE, ...params }) |
| 29 | +} |
| 30 | + |
| 31 | +describe('algolia_search indexName coercion', () => { |
| 32 | + it('accepts a numeric index name emitted as a JSON number', () => { |
| 33 | + expect( |
| 34 | + bodyOf(searchTool as AnyTool, { indexName: 2024, query: 'q' }).requests[0].indexName |
| 35 | + ).toBe('2024') |
| 36 | + }) |
| 37 | + |
| 38 | + it('still trims a string index name byte-identically', () => { |
| 39 | + expect(bodyOf(searchTool as AnyTool, { indexName: ' products ', query: 'q' })).toEqual({ |
| 40 | + requests: [{ indexName: 'products', query: 'q' }], |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + it('reports a missing required index name by name rather than crashing', () => { |
| 45 | + expect(() => bodyOf(searchTool as AnyTool, { query: 'q' })).toThrow(/indexName/) |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +describe('algolia_copy_move_index destination coercion', () => { |
| 50 | + it('accepts a numeric destination emitted as a JSON number', () => { |
| 51 | + expect( |
| 52 | + bodyOf(copyMoveIndexTool as AnyTool, { |
| 53 | + indexName: 'src', |
| 54 | + destination: 2025, |
| 55 | + operation: 'copy', |
| 56 | + }).destination |
| 57 | + ).toBe('2025') |
| 58 | + }) |
| 59 | + |
| 60 | + it('still trims a string destination byte-identically', () => { |
| 61 | + expect( |
| 62 | + bodyOf(copyMoveIndexTool as AnyTool, { |
| 63 | + indexName: 'src', |
| 64 | + destination: ' dest ', |
| 65 | + operation: 'move', |
| 66 | + }) |
| 67 | + ).toEqual({ operation: 'move', destination: 'dest' }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('reports a missing required destination by name rather than crashing', () => { |
| 71 | + expect(() => |
| 72 | + bodyOf(copyMoveIndexTool as AnyTool, { indexName: 'src', operation: 'copy' }) |
| 73 | + ).toThrow(/destination/) |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +describe('algolia_get_records indexName coercion', () => { |
| 78 | + it('accepts a numeric fallback index name emitted as a JSON number', () => { |
| 79 | + const body = bodyOf(getRecordsTool as AnyTool, { |
| 80 | + indexName: 2024, |
| 81 | + requests: [{ objectID: 'a' }], |
| 82 | + }) |
| 83 | + expect(body.requests).toEqual([{ objectID: 'a', indexName: '2024' }]) |
| 84 | + }) |
| 85 | + |
| 86 | + it('accepts a numeric per-request index name', () => { |
| 87 | + const body = bodyOf(getRecordsTool as AnyTool, { |
| 88 | + indexName: 'fallback', |
| 89 | + requests: [{ objectID: 'a', indexName: 2024 }], |
| 90 | + }) |
| 91 | + expect(body.requests).toEqual([{ objectID: 'a', indexName: '2024' }]) |
| 92 | + }) |
| 93 | + |
| 94 | + it('still trims string index names byte-identically', () => { |
| 95 | + const body = bodyOf(getRecordsTool as AnyTool, { |
| 96 | + indexName: ' fallback ', |
| 97 | + requests: [{ objectID: 'a' }, { objectID: 'b', indexName: ' own ' }], |
| 98 | + }) |
| 99 | + expect(body.requests).toEqual([ |
| 100 | + { objectID: 'a', indexName: 'fallback' }, |
| 101 | + { objectID: 'b', indexName: 'own' }, |
| 102 | + ]) |
| 103 | + }) |
| 104 | +}) |
0 commit comments