File size: 6,921 Bytes
5710dd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
 * Tests for the global search REST client (`POST /api/v1/search`): request
 * serialization, envelope unwrapping, and hit parsing.
 */

import { describe, expect, it } from 'vitest';

import { fetchSearchPage } from './api';

function okEnvelope(data: unknown) {
  return { code: 0, msg: 'success', data, request_id: 'r1' };
}

function fakeFetch(envelope: unknown) {
  const calls: { url: string; init?: RequestInit }[] = [];
  const fetchImpl = (async (url: string | URL, init?: RequestInit) => {
    calls.push({ url: String(url), init });
    return { json: async () => envelope };
  }) as unknown as typeof fetch;
  return { calls, fetchImpl };
}

const pageData = {
  items: [
    {
      session_id: 's1',
      workspace_id: 'ws',
      session_title: '鎼滅储閲嶆瀯',
      agent_id: 'main',
      role: 'assistant',
      snippet: 'Here is the apple guide.',
      time: 1_700_000_000_000,
      turn: 3,
      step_id: 't3.2',
      score: 1.5,
    },
    {
      session_id: 's2',
      workspace_id: 'ws',
      session_title: 'title doc',
      agent_id: '',
      role: 'title',
      snippet: '鑻规灉璇环',
      time: 1_700_000_100_000,
      score: 0.9,
    },
    // Malformed entries are dropped, not fatal.
    { session_id: 42 },
  ],
  has_more: true,
  page_token: 'tok-next',
  index_state: { state: 'ready', indexed_sessions: 2, total_sessions: 2, documents: 10 },
};

describe('fetchSearchPage', () => {
  it('posts the snake_case body with bearer auth and maps hits to camelCase', async () => {
    const { calls, fetchImpl } = fakeFetch(okEnvelope(pageData));
    const page = await fetchSearchPage({
      baseUrl: 'http://h:1',
      token: 'tok',
      query: '鑻规灉',
      role: 'assistant',
      sort: 'time_desc',
      pageSize: 20,
      pageToken: 'tok-prev',
      fetchImpl,
    });

    expect(calls).toHaveLength(1);
    expect(calls[0]!.url).toBe('http://h:1/api/v1/search');
    expect(calls[0]!.init?.method).toBe('POST');
    expect(calls[0]!.init?.headers).toEqual({
      'content-type': 'application/json',
      authorization: 'Bearer tok',
    });
    expect(JSON.parse(calls[0]!.init?.body as string)).toEqual({
      query: '鑻规灉',
      role: 'assistant',
      sort: 'time_desc',
      page_size: 20,
      page_token: 'tok-prev',
    });

    expect(page.items).toHaveLength(2);
    const assistant = page.items[0]!;
    expect(assistant.sessionId).toBe('s1');
    expect(assistant.role).toBe('assistant');
    expect(assistant.turn).toBe(3);
    expect(assistant.stepId).toBe('t3.2');
    const title = page.items[1]!;
    expect(title.agentId).toBe('');
    expect(title.turn).toBeUndefined();
    expect(title.stepId).toBeUndefined();
    expect(page.hasMore).toBe(true);
    expect(page.pageToken).toBe('tok-next');
    expect(page.indexState).toEqual({
      state: 'ready',
      indexedSessions: 2,
      totalSessions: 2,
      documents: 10,
    });
  });

  it('omits the authorization header when no token is configured', async () => {
    const { calls, fetchImpl } = fakeFetch(okEnvelope(pageData));
    await fetchSearchPage({ baseUrl: 'http://h:1', query: '鑻规灉', fetchImpl });
    expect(calls[0]!.init?.headers).toEqual({ 'content-type': 'application/json' });
  });

  it('serializes mode into the body and parses incomplete: candidate_cap', async () => {
    const { calls, fetchImpl } = fakeFetch(
      okEnvelope({ ...pageData, has_more: false, incomplete: 'candidate_cap' }),
    );
    const page = await fetchSearchPage({
      baseUrl: 'http://h:1',
      query: 'C++',
      mode: 'literal',
      fetchImpl,
    });

    expect(JSON.parse(calls[0]!.init?.body as string)).toEqual({
      query: 'C++',
      mode: 'literal',
    });
    expect(page.incomplete).toBe('candidate_cap');
  });

  it('leaves incomplete undefined for absent or unexpected values', async () => {
    const { fetchImpl } = fakeFetch(okEnvelope(pageData));
    const page = await fetchSearchPage({ baseUrl: 'http://h:1', query: '鑻规灉', fetchImpl });
    expect(page.incomplete).toBeUndefined();

    const { fetchImpl: fetchImpl2 } = fakeFetch(
      okEnvelope({ ...pageData, incomplete: 'something_else' }),
    );
    const page2 = await fetchSearchPage({
      baseUrl: 'http://h:1',
      query: '鑻规灉',
      fetchImpl: fetchImpl2,
    });
    expect(page2.incomplete).toBeUndefined();
  });

  it('serializes container into the body (session only, and session + agent)', async () => {
    const { calls, fetchImpl } = fakeFetch(okEnvelope(pageData));
    await fetchSearchPage({
      baseUrl: 'http://h:1',
      query: '鑻规灉',
      container: { sessionId: 's1' },
      fetchImpl,
    });
    expect(JSON.parse(calls[0]!.init?.body as string)).toEqual({
      query: '鑻规灉',
      container: { session_id: 's1' },
    });

    const { calls: calls2, fetchImpl: fetchImpl2 } = fakeFetch(okEnvelope(pageData));
    await fetchSearchPage({
      baseUrl: 'http://h:1',
      query: '鑻规灉',
      container: { sessionId: 's1', agentId: 'main' },
      fetchImpl: fetchImpl2,
    });
    expect(JSON.parse(calls2[0]!.init?.body as string)).toEqual({
      query: '鑻规灉',
      container: { session_id: 's1', agent_id: 'main' },
    });
  });

  it('parses source: live and source: index', async () => {
    const { fetchImpl } = fakeFetch(okEnvelope({ ...pageData, source: 'live' }));
    const page = await fetchSearchPage({ baseUrl: 'http://h:1', query: '鑻规灉', fetchImpl });
    expect(page.source).toBe('live');

    const { fetchImpl: fetchImpl2 } = fakeFetch(okEnvelope({ ...pageData, source: 'index' }));
    const page2 = await fetchSearchPage({
      baseUrl: 'http://h:1',
      query: '鑻规灉',
      fetchImpl: fetchImpl2,
    });
    expect(page2.source).toBe('index');
  });

  it('leaves source undefined for absent or unknown values', async () => {
    const { fetchImpl } = fakeFetch(okEnvelope(pageData));
    const page = await fetchSearchPage({ baseUrl: 'http://h:1', query: '鑻规灉', fetchImpl });
    expect(page.source).toBeUndefined();

    const { fetchImpl: fetchImpl2 } = fakeFetch(okEnvelope({ ...pageData, source: 'whatever' }));
    const page2 = await fetchSearchPage({
      baseUrl: 'http://h:1',
      query: '鑻规灉',
      fetchImpl: fetchImpl2,
    });
    expect(page2.source).toBeUndefined();
  });

  it('throws on a non-zero envelope code', async () => {
    const { fetchImpl } = fakeFetch({
      code: 40001,
      msg: 'query must be a non-empty string',
      data: null,
    });
    await expect(fetchSearchPage({ baseUrl: 'http://h:1', query: '', fetchImpl })).rejects.toThrow(
      /40001/,
    );
  });

  it('throws on a malformed payload', async () => {
    const { fetchImpl } = fakeFetch(okEnvelope({ has_more: false }));
    await expect(fetchSearchPage({ baseUrl: 'http://h:1', query: 'x', fetchImpl })).rejects.toThrow(
      /unexpected response shape/,
    );
  });
});