59 lines
1.9 KiB
TypeScript
59 lines
1.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { DEFAULT_CONFIG } from "../src/config.js";
|
|
import type { NocoBaseClient } from "../src/nocobase-client.js";
|
|
import { ProjectService } from "../src/project-service.js";
|
|
|
|
test("creates fixed-schema projects and translates progress labels", async () => {
|
|
const calls: unknown[][] = [];
|
|
const fake = {
|
|
createRecord: async (...args: unknown[]) => {
|
|
calls.push(args);
|
|
return { data: { id: 42 } };
|
|
},
|
|
addAttachmentIds: async (...args: unknown[]) => calls.push(args),
|
|
} as unknown as NocoBaseClient;
|
|
const service = new ProjectService(DEFAULT_CONFIG.project, fake);
|
|
assert.deepEqual(
|
|
await service.createProject({
|
|
name: " EV inverter ",
|
|
progress: "已报价",
|
|
customerId: 7,
|
|
attachmentIds: [8, 8, 9],
|
|
}),
|
|
{ project_id: 42 },
|
|
);
|
|
assert.ok(calls[0]);
|
|
assert.deepEqual(
|
|
(calls[0][1] as Record<string, unknown>)[DEFAULT_CONFIG.project.progressField],
|
|
"k4w2vcz819w",
|
|
);
|
|
assert.deepEqual(calls[1], [42, [8, 9]]);
|
|
});
|
|
|
|
test("validates project inputs before writes", async () => {
|
|
const fake = {
|
|
createRecord: async () => assert.fail("must not write"),
|
|
updateRecord: async () => assert.fail("must not write"),
|
|
} as unknown as NocoBaseClient;
|
|
const service = new ProjectService(DEFAULT_CONFIG.project, fake);
|
|
assert.equal(
|
|
((await service.createProject({ name: " " })).error as Record<string, unknown>).code,
|
|
"invalid_project_input",
|
|
);
|
|
assert.equal(
|
|
((await service.createProject({ name: "Project", progress: "unknown" })).error as Record<string, unknown>)
|
|
.code,
|
|
"invalid_progress",
|
|
);
|
|
assert.equal(
|
|
(
|
|
(await service.updateProject(1, { addAttachmentIds: [8], removeAttachmentIds: [8] })).error as Record<
|
|
string,
|
|
unknown
|
|
>
|
|
).code,
|
|
"invalid_project_input",
|
|
);
|
|
});
|