Files
memind/mindspace-oa-drive/drive-service.test.mjs
T
john 97a3e89ff7 fix(mindspace): repair OA drive JSON body parsing and trash paths
Express already parses JSON request bodies, so drive handlers must read req.body directly. Also fix nested trash item original-path parsing to match file-drop-sync behavior.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-10 19:04:27 +08:00

115 lines
3.1 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
import { createMindSpaceOaDriveService } from './drive-service.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const repoRoot = path.resolve(__dirname, '..');
test('oa drive upload/list/rename/trash against user oa root', async () => {
const tmpRoot = await fsp.mkdtemp(path.join(os.tmpdir(), 'memind-oa-drive-'));
const userId = 'user-oa-drive-test';
const workspaceRoot = path.join(tmpRoot, 'MindSpace', userId);
await fsp.mkdir(path.join(workspaceRoot, 'oa'), { recursive: true });
const mindSpace = {
async getQuota() {
return {
quotaBytes: 1024 * 1024 * 1024,
usedBytes: 0,
reservedBytes: 0,
availableBytes: 1024 * 1024 * 1024,
};
},
};
const syncCalls = [];
const assets = {
async syncWorkspaceAssets(uid, options) {
syncCalls.push({ uid, options });
},
};
const service = createMindSpaceOaDriveService({
h5Root: tmpRoot,
getMindSpace: () => mindSpace,
getMindSpaceAssets: () => assets,
});
const runtime = service.getRuntime(userId);
const req = {
method: 'POST',
path: '/mindspace/v1/oa/drive/folders',
url: '/mindspace/v1/oa/drive/folders',
headers: { 'content-type': 'application/json' },
currentUser: { id: userId },
async *[Symbol.asyncIterator]() {
yield Buffer.from(JSON.stringify({ name: 'box', parent: '' }));
},
body: Buffer.from(JSON.stringify({ name: 'box', parent: '' })),
on() {},
};
const res = {
statusCode: 200,
headers: {},
set(fields) {
Object.assign(this.headers, fields);
},
status(code) {
this.statusCode = code;
return this;
},
json(payload) {
this.payload = payload;
},
send(body) {
this.body = body;
},
write() {},
end() {},
};
await service.handlers.handle(req, res);
assert.equal(res.statusCode, 200);
assert.equal(res.payload.ok, true);
const helloPath = path.join(workspaceRoot, 'oa', 'box', 'hello.txt');
await fsp.mkdir(path.dirname(helloPath), { recursive: true });
await fsp.writeFile(helloPath, 'hello');
const listReq = {
method: 'GET',
path: '/mindspace/v1/oa/drive/files',
url: '/mindspace/v1/oa/drive/files',
headers: {},
currentUser: { id: userId },
on() {},
};
const listRes = {
statusCode: 200,
set() {},
status(code) {
this.statusCode = code;
return this;
},
json(payload) {
this.payload = payload;
},
send() {},
write() {},
end() {},
};
await service.handlers.handle(listReq, listRes);
assert.ok(listRes.payload.files.some((file) => file.name === 'box/hello.txt'));
assert.ok(syncCalls.length >= 1);
assert.equal(syncCalls[0].uid, userId);
assert.equal(syncCalls[0].options.categoryCode, 'oa');
runtime.dispose();
fs.rmSync(tmpRoot, { recursive: true, force: true });
});