recruter/test/outreach.test.ts
2026-11-14 21:46:24 +05:00

82 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from "vitest";
import { MockChannelAdapter, MockScreener } from "../src/channels.js";
import { openDb } from "../src/db.js";
import { Outreach } from "../src/outreach.js";
import { createVacancy } from "../src/vacancies.js";
function setup(slots = ["2027-01-15 11:00"]) {
const db = openDb();
const v = createVacancy(db, { title: "Оператор", targetCount: 3 });
const id = Number(
db.prepare("INSERT INTO candidates (vacancy_id, full_name, phone, channel) VALUES (?, 'Анна', '79001112233', 'telegram')").run(v.id).lastInsertRowid,
);
const ch = new MockChannelAdapter();
return { db, id, ch, o: new Outreach(db, ch, new MockScreener(), slots) };
}
const status = (db: ReturnType<typeof openDb>, id: number) =>
(db.prepare("SELECT status FROM candidates WHERE id=?").get(id) as { status: string }).status;
describe("candidate-outreach", () => {
it("Scenario: Первое сообщение — вакансия, ассистент, способ отказа", async () => {
const { o, ch, id } = setup();
await o.startContact(id);
const t = ch.sent[0]!.text;
expect(ch.sent[0]!.to.channel).toBe("telegram");
expect(t).toContain("«Оператор»");
expect(t).toContain("автоматический ассистент");
expect(t).toContain("«стоп»");
});
it("Scenario: Отказ кандидата — «стоп» прекращает контакт", async () => {
const { o, ch, id, db } = setup();
await o.startContact(id);
await o.handleReply(id, "Стоп");
expect(status(db, id)).toBe("declined");
const n = ch.sent.length;
await o.handleReply(id, "ещё вопрос");
expect(ch.sent.length).toBe(n);
});
it("Scenario: Кандидат выбирает слот — полный путь до встречи", async () => {
const { o, ch, id, db } = setup();
await o.startContact(id);
await o.handleReply(id, "Здравствуйте, интересно"); // → вопрос 1
await o.handleReply(id, "да"); // → вопрос 2
await o.handleReply(id, "да, готов"); // → слоты
expect(status(db, id)).toBe("qualified");
expect(ch.sent.at(-1)!.text).toContain("2027-01-15 11:00");
await o.handleReply(id, "Выбираю 2027-01-15 11:00");
expect(status(db, id)).toBe("meeting_set");
expect(db.prepare("SELECT starts_at FROM meetings WHERE candidate_id=?").get(id)).toEqual({ starts_at: "2027-01-15 11:00" });
});
it("Scenario: Нет свободных слотов — ожидает слот", async () => {
const { o, id, db } = setup([]);
await o.startContact(id);
for (const a of ["ок", "да", "да"]) await o.handleReply(id, a);
expect(status(db, id)).toBe("awaiting_slot");
});
it("не готов к обучению — отказ, ready_for_training=0", async () => {
const { o, id, db } = setup();
await o.startContact(id);
for (const a of ["ок", "да", "нет"]) await o.handleReply(id, a);
expect(status(db, id)).toBe("rejected");
});
it("Scenario: Превышение лимита — 4-е сообщение откладывается", async () => {
const { o, ch, id, db } = setup();
db.prepare("UPDATE candidates SET status='contacted' WHERE id=?").run(id);
for (let i = 0; i < 3; i++) db.prepare("INSERT INTO messages (candidate_id, direction, body) VALUES (?, 'out', 'x')").run(id);
db.prepare("UPDATE candidates SET status='found' WHERE id=?").run(id);
expect(await o.startContact(id)).toBe("deferred");
expect(ch.sent).toHaveLength(0);
});
it("нет канала связи — ошибка", async () => {
const { o, id, db } = setup();
db.prepare("UPDATE candidates SET channel=NULL WHERE id=?").run(id);
await expect(o.startContact(id)).rejects.toThrow("нет номера");
});
});