fakes.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /** 测试替身:假的工具服务客户端、临时配置、临时 HTTP 服务。 */
  2. import type { AddressInfo } from 'node:net';
  3. import type http from 'node:http';
  4. import { mkdirSync, mkdtempSync } from 'node:fs';
  5. import os from 'node:os';
  6. import path from 'node:path';
  7. import type { Settings } from '../src/config.js';
  8. import type { ToolsClient, ObjectInfo, RunInfo, WorkspaceInfo } from '../src/tools/client.js';
  9. import { HttpError } from '../src/util/common.js';
  10. export function tempDir(prefix = 'cda-test-'): string {
  11. const directory = mkdtempSync(path.join(os.tmpdir(), prefix));
  12. mkdirSync(directory, { recursive: true });
  13. return directory;
  14. }
  15. export function testSettings(dataDir: string): Settings {
  16. return {
  17. repoRoot: path.resolve(import.meta.dirname, '..', '..', '..'),
  18. host: '127.0.0.1',
  19. port: 0,
  20. toolsUrl: 'http://127.0.0.1:1',
  21. dataDir,
  22. webDist: path.resolve(import.meta.dirname, '..', '..', '..', 'web', 'dist'),
  23. presetFile: path.resolve(import.meta.dirname, '..', '..', '..', 'agent.cordis.yml'),
  24. apiKey: 'test-key',
  25. baseUrl: 'http://127.0.0.1:1',
  26. modelOverride: null,
  27. providerOverride: null,
  28. maxUploadBytes: 10 * 1024 * 1024,
  29. verificationEnabled: true,
  30. llmMode: 'replay',
  31. replayFile: '',
  32. };
  33. }
  34. export interface FakeRow {
  35. values: Record<string, unknown>;
  36. object_id?: string;
  37. table?: string;
  38. row_index?: number;
  39. row_hash?: string;
  40. }
  41. /** 假工具服务:内存对象库 + 固定数据,用于 BFF 单测与联调。 */
  42. export class FakeToolsClient {
  43. workspaces: WorkspaceInfo[] = [
  44. { id: 'ws_test', name: '测试工作区', description: '', created_at: '2026-01-01T00:00:00+08:00', object_count: 1 },
  45. ];
  46. objects: ObjectInfo[] = [];
  47. runs: RunInfo[] = [];
  48. dataRows: Record<string, FakeRow[]> = {};
  49. calculatorResult = '400316.13';
  50. processSheets = [
  51. { name: '1.0-3.0_MLCC', sqlite_name: 's_1_0_3_0_MLCC', rows: 20, columns: ['基础类目', '年度', '月度', '实际金额(美元)'] },
  52. ];
  53. constructor() {
  54. this.objects.push(makeObject({ id: 'obj_raw', name: 'small.csv', kind: 'raw', rows: 100 }));
  55. this.objects.push(makeObject({ id: 'obj_stat', name: 'small_统计结果.xlsx', kind: 'stat', rows: 23, tables: this.processSheets }));
  56. this.dataRows['obj_stat:1.0-3.0_MLCC'] = [
  57. { values: { 基础类目: '多层瓷介电容器(MLCC)', 年度: 2025, 月度: 1, '实际金额(美元)': '400316.13' }, row_index: 0, row_hash: 'sha256:aaa' },
  58. { values: { 基础类目: '多层瓷介电容器(MLCC)', 年度: 2025, 月度: 2, '实际金额(美元)': '410000.00' }, row_index: 1, row_hash: 'sha256:bbb' },
  59. ];
  60. }
  61. async health() {
  62. return { ok: true, service: 'tools', version: 'test', data_dir: '/tmp' };
  63. }
  64. async meta() {
  65. return { tools: ['customs_process', 'data_read', 'calculator'] };
  66. }
  67. async listWorkspaces() {
  68. return { items: this.workspaces };
  69. }
  70. async createWorkspace(name: string, description = '') {
  71. const workspace = { id: `ws_${this.workspaces.length + 1}`, name, description, created_at: '2026-01-01T00:00:00+08:00', object_count: 0 };
  72. this.workspaces.push(workspace);
  73. return workspace;
  74. }
  75. async deleteWorkspace(id: string) {
  76. this.workspaces = this.workspaces.filter((item) => item.id !== id);
  77. return { ok: true };
  78. }
  79. async listObjects(workspaceId: string, kind?: string) {
  80. const items = this.objects.filter((item) => (!kind ? true : item.kind === kind));
  81. return { items };
  82. }
  83. async getObject(objectId: string) {
  84. const item = this.objects.find((entry) => entry.id === objectId);
  85. if (!item) throw new HttpError(404, `对象不存在: ${objectId}`);
  86. return item;
  87. }
  88. async deleteObject(objectId: string) {
  89. this.objects = this.objects.filter((item) => item.id !== objectId);
  90. return { ok: true };
  91. }
  92. async uploadObject(_workspaceId: string, filename: string, bytes: Uint8Array) {
  93. const object = makeObject({ id: `obj_raw_${this.objects.length + 1}`, name: filename, kind: 'raw', rows: bytes.length });
  94. this.objects.push(object);
  95. return object;
  96. }
  97. async preview(objectId: string, table = 'raw', limit = 20, offset = 0) {
  98. const all = this.dataRows[`${objectId}:${table}`] ?? [];
  99. const page = all.slice(offset, offset + limit);
  100. return {
  101. columns: Object.keys(all[0]?.values ?? {}),
  102. rows: page.map((row) => row.values),
  103. row_indexes: page.map((row) => row.row_index ?? 0),
  104. row_hashes: page.map((row) => row.row_hash ?? 'sha256:x'),
  105. offset,
  106. limit,
  107. total: all.length,
  108. };
  109. }
  110. async row(objectId: string, table: string, rowIndex: number) {
  111. const all = this.dataRows[`${objectId}:${table}`] ?? [];
  112. const row = all.find((item) => (item.row_index ?? 0) === rowIndex);
  113. if (!row) throw new HttpError(404, `表 ${table} 中没有第 ${rowIndex} 行`);
  114. return {
  115. object_id: objectId,
  116. table,
  117. row_index: rowIndex,
  118. row_hash: row.row_hash ?? 'sha256:x',
  119. values: row.values,
  120. lineage: { source_object_id: 'obj_raw', source_rows: { year: '2025' } },
  121. };
  122. }
  123. async lineage(objectId: string) {
  124. return { object_id: objectId, parents: ['obj_raw'], lineage: { output_hash: 'sha256:out' } };
  125. }
  126. async download() {
  127. return new Response('col1,col2\n1,2\n', { headers: { 'content-type': 'text/csv' } });
  128. }
  129. async createRun(payload: any) {
  130. const run: RunInfo = {
  131. id: `run_${this.runs.length + 1}`,
  132. object_id: 'obj_stat',
  133. input_object: payload.input_object_id,
  134. status: 'succeeded',
  135. operation: payload.operation ?? 'full_pipeline',
  136. progress: 1,
  137. message: '处理完成',
  138. error: '',
  139. started_at: '2026-01-01T00:00:00+08:00',
  140. finished_at: '2026-01-01T00:00:10+08:00',
  141. params: payload.options ?? {},
  142. };
  143. this.runs.push(run);
  144. return run;
  145. }
  146. async getRun(runId: string) {
  147. const run = this.runs.find((item) => item.id === runId);
  148. if (!run) throw new HttpError(404, '任务不存在');
  149. return run;
  150. }
  151. async listRuns() {
  152. return { items: this.runs };
  153. }
  154. async processCustoms(payload: any) {
  155. return {
  156. run_id: 'run_tool_1',
  157. object_id: 'obj_stat_new',
  158. name: payload.output_filename ?? 'small_统计结果.xlsx',
  159. sheets: this.processSheets,
  160. lineage: { input_sha256: 'sha256:in', params_hash: 'sha256:p', output_hash: 'sha256:out', engine: { name: 'fake', version: '1' } },
  161. stats: { included: 100 },
  162. warnings: [],
  163. files: ['统计结果.xlsx'],
  164. };
  165. }
  166. async dataRead(payload: any) {
  167. const rows = (this.dataRows[`${payload.object_id}:${payload.table ?? 'raw'}`] ?? []).slice(
  168. payload.offset ?? 0,
  169. (payload.offset ?? 0) + (payload.limit ?? 20),
  170. );
  171. return {
  172. object_id: payload.object_id,
  173. table: payload.table ?? 'raw',
  174. columns: Object.keys(rows[0]?.values ?? {}),
  175. rows: rows.map((row) => ({
  176. values: row.values,
  177. _trace: {
  178. object_id: payload.object_id,
  179. table: payload.table ?? 'raw',
  180. row_index: row.row_index ?? 0,
  181. row_hash: row.row_hash ?? 'sha256:x',
  182. },
  183. })),
  184. total: (this.dataRows[`${payload.object_id}:${payload.table ?? 'raw'}`] ?? []).length,
  185. offset: payload.offset ?? 0,
  186. limit: payload.limit ?? 20,
  187. exhausted: true,
  188. };
  189. }
  190. async calculate(payload: any) {
  191. const values = (payload.inputs ?? []).map((input: any) => Number(input.value));
  192. const result = values.length ? String(values.reduce((sum: number, value: number) => sum + value, 0)) : this.calculatorResult;
  193. return {
  194. call_id: 'calc_test_1',
  195. tool: 'calculator',
  196. op: payload.op,
  197. result: payload.op === 'sum' ? result : this.calculatorResult,
  198. expression: payload.op,
  199. inputs: (payload.inputs ?? []).map((input: any, index: number) => ({
  200. index,
  201. value: String(input.value),
  202. from_call_id: input.from_call_id ?? null,
  203. row_hash: input.row_hash ?? null,
  204. verified: Boolean(input.from_call_id),
  205. })),
  206. unverified_inputs: 0,
  207. all_inputs_verified: true,
  208. _trace: { calculation_id: 'calc_test_1', result_hash: 'sha256:calc', op: payload.op },
  209. };
  210. }
  211. async ledgerStats() {
  212. return { calls: 1, indexed_values: 2, chain_ok: true };
  213. }
  214. }
  215. export function makeObject(input: {
  216. id: string;
  217. name: string;
  218. kind: 'raw' | 'stat';
  219. rows: number;
  220. tables?: Array<{ name: string; sqlite_name: string; rows: number; columns: string[] }>;
  221. }): ObjectInfo {
  222. return {
  223. id: input.id,
  224. workspace_id: 'ws_test',
  225. name: input.name,
  226. kind: input.kind,
  227. media_type: 'text/csv',
  228. path: `/data/${input.id}`,
  229. sha256: 'sha256:obj',
  230. bytes: 1024,
  231. rows: input.rows,
  232. columns: ['I_E_FLAG', 'HS_CODE', 'MONORSUM'],
  233. producer: 'upload',
  234. parents: [],
  235. meta: {},
  236. created_at: '2026-01-01T00:00:00+08:00',
  237. tables: input.tables ?? [{ name: 'raw', sqlite_name: 'raw', rows: input.rows, columns: ['I_E_FLAG', 'HS_CODE'] }],
  238. };
  239. }
  240. export function asToolsClient(fake: FakeToolsClient): ToolsClient {
  241. return fake as unknown as ToolsClient;
  242. }
  243. export interface RunningServer {
  244. url: string;
  245. close(): Promise<void>;
  246. server: http.Server;
  247. }
  248. export function listen(server: http.Server): Promise<RunningServer> {
  249. return new Promise((resolve, reject) => {
  250. server.listen(0, '127.0.0.1', () => {
  251. const address = server.address() as AddressInfo;
  252. resolve({
  253. url: `http://127.0.0.1:${address.port}`,
  254. server,
  255. close: () =>
  256. new Promise<void>((done) => {
  257. server.close(() => done());
  258. }),
  259. });
  260. });
  261. server.on('error', reject);
  262. });
  263. }
  264. /** 解析 SSE 响应为事件数组。 */
  265. export async function collectSse(response: Response): Promise<Array<{ event: string; data: any }>> {
  266. const text = await response.text();
  267. const events: Array<{ event: string; data: any }> = [];
  268. for (const block of text.split('\n\n')) {
  269. const lines = block.split('\n').filter(Boolean);
  270. let event = 'message';
  271. let data = '';
  272. for (const line of lines) {
  273. if (line.startsWith('event:')) event = line.slice(6).trim();
  274. else if (line.startsWith('data:')) data += line.slice(5).trim();
  275. }
  276. if (!data) continue;
  277. try {
  278. events.push({ event, data: JSON.parse(data) });
  279. } catch {
  280. events.push({ event, data });
  281. }
  282. }
  283. return events;
  284. }