import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { createTestDb, testDatabaseUrl, type TestDb } from "khb_common"; import { isHumanMessage, syncSlackMessages } from "./slack-messages.js"; describe("isHumanMessage", () => { it("люди - да; боты, служебные подтипы и Slackbot - нет", () => { expect(isHumanMessage({ ts: "1.1", user: "U1" })).toBe(true); expect(isHumanMessage({ ts: "1.1", user: "U1", subtype: "file_share" })).toBe(true); expect(isHumanMessage({ ts: "1.1", user: "U1", bot_id: "B1" })).toBe(false); expect(isHumanMessage({ ts: "1.1", user: "U1", subtype: "channel_join" })).toBe(false); expect(isHumanMessage({ ts: "1.1", user: "USLACKBOT" })).toBe(false); expect(isHumanMessage({ ts: "1.1" })).toBe(false); }); }); describe("syncSlackMessages (временная БД, фейковый Slack)", () => { let db: TestDb; beforeAll(async () => { db = await createTestDb(testDatabaseUrl()); }); afterAll(async () => { await db?.drop(); }); it("грузит сообщения и ответы в тредах, пропускает ботов и каналы без доступа, повтор не дублирует", async () => { await db.pool.query("insert into project_channel (channel_id, jt_job_id, track_activity) values ('CA', null, true), ('CB', null, true), ('CC', null, false)"); const calls: string[] = []; const fetchFn = (async (url: string) => { const u = new URL(url); const ch = u.searchParams.get("channel"); calls.push(`${u.pathname.split("/").pop()}:${ch}`); const json = (o: unknown) => ({ status: 200, headers: new Headers(), json: async () => o }) as unknown as Response; if (ch === "CB") return json({ ok: false, error: "not_in_channel" }); if (u.pathname.endsWith("conversations.history")) { return json({ ok: true, messages: [ { ts: "1760000000.000100", user: "U1", reply_count: 1 }, { ts: "1760000100.000100", user: "U2", bot_id: "B1" }, { ts: "1760000200.000100", user: "U2", subtype: "channel_join" }, { ts: "1760000300.000100", user: "U2" }, ] }); } return json({ ok: true, messages: [{ ts: "1760000000.000100", user: "U1" }, { ts: "1760000050.000100", user: "U3", thread_ts: "1760000000.000100" }] }); }) as unknown as typeof fetch; const now = new Date("2025-10-10T00:00:00Z"); const r = await syncSlackMessages(db.pool, { botToken: "x", fetchFn, now, backfillDays: 30, sleep: async () => {} }); expect(r.channels).toBe(2); // CC не учитывается expect(r.errors).toEqual([{ channel: "CB", error: "not_in_channel" }]); expect(r.inserted).toBe(3); const rows = (await db.pool.query("select user_id, is_reply from slack_channel_message order by message_ts")).rows; expect(rows).toEqual([{ user_id: "U1", is_reply: false }, { user_id: "U3", is_reply: true }, { user_id: "U2", is_reply: false }]); expect(calls.filter((c) => c.startsWith("conversations.history")).length).toBe(2); const again = await syncSlackMessages(db.pool, { botToken: "x", fetchFn, now, backfillDays: 30, sleep: async () => {} }); expect(again.inserted).toBe(0); const st = (await db.pool.query("select last_error from activity_sync_state where source = 'slack_messages'")).rows[0]; expect(st.last_error).toContain("CB"); }); });