feat: add logto auth and backend metrics integration

This commit is contained in:
2026-04-22 15:52:12 +08:00
parent b1bb28f5aa
commit c7ae2fbfcb
38 changed files with 4886 additions and 34 deletions
+140
View File
@@ -127,4 +127,144 @@ describe("background-index", () => {
value: { accessToken: "test-access-token" }
});
});
test("submits batches through the background message handler", async () => {
const listeners: Array<
(message: unknown, sender: unknown, sendResponse: (response: unknown) => void) => boolean | void
> = [];
const sendResponse = vi.fn();
const submitBatch = vi.fn(async () => ({
acceptedCount: 1,
batchId: "批次A-2026-04-22T12:30:00.000Z",
ok: true
}));
registerBackgroundMessageHandler(
{
runtime: {
onMessage: {
addListener(listener) {
listeners.push(listener);
}
}
}
},
{
authController: {
getAccessToken: vi.fn(async () => "test-access-token"),
getAuthState: vi.fn(),
signIn: vi.fn(),
signOut: vi.fn()
},
submitBatch
}
);
const result = listeners[0](
{
payload: {
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"
},
type: "batch:submit"
},
{},
sendResponse
);
expect(result).toBe(true);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(submitBatch).toHaveBeenCalledWith(
expect.objectContaining({
batchId: "批次A-2026-04-22T12:30:00.000Z"
})
);
expect(sendResponse).toHaveBeenCalledWith({
ok: true,
type: "batch:ack",
value: {
acceptedCount: 1,
batchId: "批次A-2026-04-22T12:30:00.000Z",
ok: true
}
});
});
test("searches backend metrics through the background message handler", async () => {
const listeners: Array<
(message: unknown, sender: unknown, sendResponse: (response: unknown) => void) => boolean | void
> = [];
const sendResponse = vi.fn();
const searchBackendMetrics = vi.fn(async () => [
{
a3IncreaseCount: "78,366.22",
afterViewSearchCount: "9,689.96",
afterViewSearchRate: "0.36%",
cpSearch: "14.46",
cpa3: "1.79",
newA3Rate: "3.44%",
starId: "111"
}
]);
registerBackgroundMessageHandler(
{
runtime: {
onMessage: {
addListener(listener) {
listeners.push(listener);
}
}
}
},
{
authController: {
getAccessToken: vi.fn(async () => "test-access-token"),
getAuthState: vi.fn(),
signIn: vi.fn(),
signOut: vi.fn()
},
searchBackendMetrics
}
);
const result = listeners[0](
{
type: "backend-metrics:search",
value: {
starIds: ["111", "222"]
}
},
{},
sendResponse
);
expect(result).toBe(true);
await new Promise((resolve) => setTimeout(resolve, 0));
expect(searchBackendMetrics).toHaveBeenCalledWith(["111", "222"]);
expect(sendResponse).toHaveBeenCalledWith({
ok: true,
type: "backend-metrics:result",
value: {
rows: [
{
a3IncreaseCount: "78,366.22",
afterViewSearchCount: "9,689.96",
afterViewSearchRate: "0.36%",
cpSearch: "14.46",
cpa3: "1.79",
newA3Rate: "3.44%",
starId: "111"
}
]
}
});
});
});