Files
shanxia2.0/web-admin/src/views/doclib/enter/api.test.ts
T

55 lines
2.2 KiB
TypeScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import test from 'node:test'
import ts from 'typescript'
// Exercise the API adapter without loading the browser-only auth/router graph.
function apiAdapter() {
const calls: Array<{ method: string; config: any }> = []
const http = Object.fromEntries(
['get', 'post', 'put', 'delete'].map((method) => [
method,
(config: any) => {
calls.push({ method, config })
return Promise.resolve({})
}
])
)
const source = readFileSync(new URL('../../../api/doclib.ts', import.meta.url), 'utf8')
const compiled = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS }
}).outputText
const exports: Record<string, (...args: any[]) => any> = {}
new Function('require', 'exports', compiled)(() => ({ default: http }), exports)
return { api: exports, calls }
}
test('大文件下载给予五分钟超时并使用鉴权blob请求', async () => {
const { api, calls } = apiAdapter()
await api.downloadDoclibFile('id/with space')
assert.equal(calls[0].method, 'get')
assert.equal(calls[0].config.url, '/web/doclib/files/id%2Fwith%20space/download')
assert.equal(calls[0].config.responseType, 'blob')
assert.equal(calls[0].config.timeout, 300000)
})
test('专用上传请求提交原文件及目标目录,清空分享提交空数组', async () => {
const { api, calls } = apiAdapter()
const file = new File(['hello'], '测试.txt', { type: 'text/plain' })
await api.uploadDoclibFile(file, 'folder-id')
assert.equal(calls[0].config.url, '/web/doclib/upload')
assert.equal(calls[0].config.data.get('folderId'), 'folder-id')
assert.equal(calls[0].config.data.get('file').name, '测试.txt')
await api.saveDoclibShares('document-id', [])
assert.equal(calls[1].method, 'put')
assert.deepEqual(calls[1].config.data, { userIds: [] })
})
test('批量分享提交文档 id 列表与人员 id', async () => {
const { api, calls } = apiAdapter()
await api.saveDoclibBatchShares(['id/a', 'id/b'], ['u1', 'u2'])
assert.equal(calls[0].method, 'post')
assert.equal(calls[0].config.url, '/web/doclib/files/batch-shares')
assert.deepEqual(calls[0].config.data, { ids: ['id/a', 'id/b'], userIds: ['u1', 'u2'] })
})