|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import type { ServiceListResponse } from '../src/api-types.js' |
| 3 | +import type { KbClient } from '../src/client.js' |
| 4 | +import { listServices } from '../src/services.js' |
| 5 | + |
| 6 | +function fakeClient(byScene: Record<string, ServiceListResponse>) { |
| 7 | + const postJson = vi.fn(async (_path: string, body: { agent_scene: string }) => byScene[body.agent_scene]) |
| 8 | + return { client: { postJson } as unknown as KbClient, postJson } |
| 9 | +} |
| 10 | + |
| 11 | +const row = (id: string, scene: string) => ({ |
| 12 | + agent_id: id, agent_name: `svc-${id}`, agent_scene: scene, agent_status: 'deployed', |
| 13 | + pipeline_list: [{ pipeline_id: 'p1', pipeline_name: 'kb-one' }], |
| 14 | +}) |
| 15 | + |
| 16 | +describe('listServices', () => { |
| 17 | + it('queries both scenes when scene is omitted and merges rows with scene tags', async () => { |
| 18 | + const { client, postJson } = fakeClient({ |
| 19 | + chat: { data: { total_count: 1, rows: [row('a', 'chat')] } }, |
| 20 | + search: { data: { total_count: 1, rows: [row('b', 'search')] } }, |
| 21 | + }) |
| 22 | + const out = await listServices(client, {}) |
| 23 | + expect(postJson).toHaveBeenCalledTimes(2) |
| 24 | + expect(out.services.map(s => [s.agent_id, s.scene])).toEqual([['a', 'chat'], ['b', 'search']]) |
| 25 | + expect(out.services[0]!.knowledge_bases).toEqual(['kb-one']) |
| 26 | + expect(out.total).toBe(2) |
| 27 | + expect(out.truncated).toBe(false) |
| 28 | + const body = postJson.mock.calls[0]![1] as unknown as Record<string, unknown> |
| 29 | + expect(body.page_number).toBe(1) |
| 30 | + expect(body.page_size).toBe(100) |
| 31 | + }) |
| 32 | + |
| 33 | + it('queries one scene and forwards the name filter', async () => { |
| 34 | + const { client, postJson } = fakeClient({ search: { data: { total_count: 0, rows: [] } } }) |
| 35 | + await listServices(client, { scene: 'search', nameFilter: '客服' }) |
| 36 | + expect(postJson).toHaveBeenCalledTimes(1) |
| 37 | + expect((postJson.mock.calls[0]![1] as unknown as Record<string, unknown>).agent_name).toBe('客服') |
| 38 | + }) |
| 39 | + |
| 40 | + it('flags truncation when a scene exceeds one max page', async () => { |
| 41 | + const { client } = fakeClient({ |
| 42 | + chat: { data: { total_count: 250, rows: [row('a', 'chat')] } }, |
| 43 | + search: { data: { total_count: 0, rows: [] } }, |
| 44 | + }) |
| 45 | + const out = await listServices(client, {}) |
| 46 | + expect(out.truncated).toBe(true) |
| 47 | + expect(out.total).toBe(250) |
| 48 | + }) |
| 49 | +}) |
0 commit comments