128 lines
3.7 KiB
TypeScript
128 lines
3.7 KiB
TypeScript
import { describe, expect, test, vi } from "vitest";
|
|
|
|
import { DEFAULT_BATCH_SUBMIT_BASE_URL } from "../src/shared/batch-submit-config";
|
|
import { createBatchSubmitClient } from "../src/shared/batch-submit-client";
|
|
|
|
describe("batch-submit-client", () => {
|
|
test("exports the default batch submit base url", () => {
|
|
expect(DEFAULT_BATCH_SUBMIT_BASE_URL).toBe("http://192.168.31.29:8083");
|
|
});
|
|
|
|
test("posts the batch payload with a Bearer token", async () => {
|
|
const sendMessage = vi.fn(async () => ({
|
|
ok: true,
|
|
type: "auth:token",
|
|
value: { accessToken: "abc123" }
|
|
}));
|
|
const fetchImpl = vi.fn(async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({
|
|
data: {
|
|
batch_id: "p7pdhhtde8kj-2026-04-22T12:30:00.000Z",
|
|
status: true,
|
|
talent_count: 1
|
|
},
|
|
msg: "",
|
|
success: true
|
|
})
|
|
}));
|
|
|
|
const client = createBatchSubmitClient({
|
|
baseUrl: "http://127.0.0.1:4319",
|
|
fetchImpl,
|
|
sendMessage
|
|
});
|
|
|
|
await client.submitBatch({
|
|
authors: [{ authorId: "111", authorName: "达人A" }],
|
|
batchId: "批次A-2026-04-22T12:30:00.000Z",
|
|
batchName: "批次A",
|
|
createdAt: "2026-04-22T12:30:00.000Z",
|
|
creatorName: "王少卿",
|
|
logtoUserId: "p7pdhhtde8kj",
|
|
resource: "https://talent-search.intelligrow.cn"
|
|
});
|
|
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
"http://127.0.0.1:4319/api/v1/batch-status/batches",
|
|
expect.objectContaining({
|
|
body: JSON.stringify({
|
|
authors: [{ authorId: "111", authorName: "达人A" }],
|
|
batchId: "批次A-2026-04-22T12:30:00.000Z",
|
|
batchName: "批次A",
|
|
createdAt: "2026-04-22T12:30:00.000Z",
|
|
creatorName: "王少卿",
|
|
logtoUserId: "p7pdhhtde8kj",
|
|
resource: "https://talent-search.intelligrow.cn"
|
|
}),
|
|
headers: expect.objectContaining({
|
|
Authorization: "Bearer abc123",
|
|
"Content-Type": "application/json"
|
|
}),
|
|
method: "POST"
|
|
})
|
|
);
|
|
});
|
|
|
|
test("throws when the batch submit api returns success false", async () => {
|
|
const client = createBatchSubmitClient({
|
|
baseUrl: "http://127.0.0.1:4319",
|
|
fetchImpl: vi.fn(async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
json: async () => ({
|
|
data: null,
|
|
msg: "duplicate batch id",
|
|
success: false
|
|
})
|
|
})),
|
|
sendMessage: vi.fn(async () => ({
|
|
ok: true,
|
|
type: "auth:token",
|
|
value: { accessToken: "abc123" }
|
|
}))
|
|
});
|
|
|
|
await expect(
|
|
client.submitBatch({
|
|
authors: [],
|
|
batchId: "批次A-2026-04-22T12:30:00.000Z",
|
|
batchName: "批次A",
|
|
createdAt: "2026-04-22T12:30:00.000Z",
|
|
creatorName: "王少卿",
|
|
logtoUserId: "p7pdhhtde8kj",
|
|
resource: "https://talent-search.intelligrow.cn"
|
|
})
|
|
).rejects.toThrow(/duplicate batch id/i);
|
|
});
|
|
|
|
test("throws on unauthorized responses", async () => {
|
|
const client = createBatchSubmitClient({
|
|
baseUrl: "http://127.0.0.1:4319",
|
|
fetchImpl: vi.fn(async () => ({
|
|
ok: false,
|
|
status: 401,
|
|
json: async () => ({ error: "unauthorized", ok: false })
|
|
})),
|
|
sendMessage: vi.fn(async () => ({
|
|
ok: true,
|
|
type: "auth:token",
|
|
value: { accessToken: "abc123" }
|
|
}))
|
|
});
|
|
|
|
await expect(
|
|
client.submitBatch({
|
|
authors: [],
|
|
batchId: "批次A-2026-04-22T12:30:00.000Z",
|
|
batchName: "批次A",
|
|
createdAt: "2026-04-22T12:30:00.000Z",
|
|
creatorName: "王少卿",
|
|
logtoUserId: "p7pdhhtde8kj",
|
|
resource: "https://talent-search.intelligrow.cn"
|
|
})
|
|
).rejects.toThrow(/unauthorized/i);
|
|
});
|
|
});
|