/** * tests/test-webapi-datasource.ts — 验证 webapi/dataSource.ts 完整覆盖 * * 与 test-webapi-visualdev/datamodel 同样模式, 覆盖 11 个函数可用性 + 实测 */ import { JnpfClient } from '../src/core/client.ts' import { initWebapi, loadDataSource, isWebapiLoaded } from '../src/webapi/dataSource.ts' import { log } from '../src/core/log.ts' async function main() { log.info('=== test-webapi-dataSource ===') const c = await JnpfClient.fromToken() log.ok(` 登录成功: baseUrl=${c.baseUrl} appCode=${c.appCode} dbLinkId=${c.dbLinkId}`) const t0 = Date.now() await initWebapi(c) log.ok(` 初始化成功 (${Date.now() - t0}ms), isWebapiLoaded=${isWebapiLoaded()}`) // 函数可用性 const fns = [ 'getDataSourceList', 'getDataSourceSelector', 'createDataSource', 'updateDataSource', 'getDataSourceInfo', 'delDataSource', 'testDbConnection', 'dataSync', 'execute', 'batchExecute', 'checkDbLink', ] for (const fn of fns) { if (typeof loadDataSource[fn] !== 'function') { throw new Error(`函数 ${fn} 不存在`) } } log.ok(` ${fns.length} 个核心函数可用`) // 实测 1: getDataSourceList log.info('Step: 实测 getDataSourceList({ currentPage: 1, pageSize: 5 })') const list = await loadDataSource.getDataSourceList({ currentPage: 1, pageSize: 5 }) const arr = Array.isArray(list) ? list : list?.list ?? [] log.ok(` 返回 ${arr.length} 条`) if (arr.length > 0) { const ds = arr[0] log.ok(` 示例: id=${ds.id} fullName=${ds.fullName}`) // 实测 2: getDataSourceInfo (用第一个 datasource id) log.info(`Step: 实测 getDataSourceInfo(${ds.id})`) const info = await loadDataSource.getDataSourceInfo(ds.id) log.ok(` 返回 enCode=${info?.enCode} dbType=${info?.dbType}`) // 实测 3: testDbConnection (POST /Actions/Test, 不修改) if (info?.id) { log.info(`Step: 实测 testDbConnection({ id: ${info.id} }) (无副作用)`) try { const test = await loadDataSource.testDbConnection({ id: info.id }) log.ok(` 返回: ${JSON.stringify(test).slice(0, 100)}`) } catch (e) { log.warn(` testDbConnection 报: ${(e as Error).message} (可能需特殊权限, 跳过)`) } } } // 实测 4: getDataSourceSelector (无需参数, 仅下拉框) log.info('Step: 实测 getDataSourceSelector()') const sel = await loadDataSource.getDataSourceSelector() const selArr = Array.isArray(sel) ? sel : sel?.list ?? [] log.ok(` 返回 ${selArr.length} 项 (下拉框)`) log.ok('=== 全部 PASS ===') } main().catch((e) => { log.error(`FAIL: ${(e as Error).message}`) console.error(e) process.exit(1) })