All checks were successful
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
136 lines
6.2 KiB
TypeScript
136 lines
6.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
||
import {
|
||
emptyCatalog,
|
||
isEligible,
|
||
isExpired,
|
||
mergeIntoCatalog,
|
||
normalizeUrl,
|
||
parseRawInternship,
|
||
sortForDisplay,
|
||
} from "../src/domain/internship.js";
|
||
import { raw } from "./helpers.js";
|
||
|
||
describe("Requirement: Карточка стажировки", () => {
|
||
it("Scenario: Полная карточка из результата поиска", () => {
|
||
const { catalog } = mergeIntoCatalog(
|
||
emptyCatalog(),
|
||
[raw({ sourceUrl: "https://example.org/alpina" })],
|
||
"run-1",
|
||
"2026-11-14T10:00:00.000Z",
|
||
);
|
||
expect(catalog.internships).toHaveLength(1);
|
||
const card = catalog.internships[0]!;
|
||
expect(card.title).toBe("Guest Service Intern");
|
||
expect(card.country).toBe("Швейцария");
|
||
expect(card.sourceUrl).toBe("https://example.org/alpina");
|
||
expect(card.coveredExpenses).not.toBe("");
|
||
expect(card.languageDetails).not.toBe("");
|
||
expect(card.checkedAt).toBe("2026-11-14T10:00:00.000Z");
|
||
});
|
||
|
||
it("Scenario: Результат без ссылки на источник отбрасывается", () => {
|
||
const result = mergeIntoCatalog(emptyCatalog(), [raw({ sourceUrl: null })], "run-1", "2026-11-14T10:00:00.000Z");
|
||
expect(result.catalog.internships).toHaveLength(0);
|
||
expect(result.rejected).toBe(1);
|
||
});
|
||
|
||
it("отбрасывает объект без обязательных полей и с невалидной ссылкой", () => {
|
||
const result = mergeIntoCatalog(
|
||
emptyCatalog(),
|
||
[{ title: "x" }, raw({ sourceUrl: "ftp://example.org/a" }), null],
|
||
"run-1",
|
||
"2026-11-14T10:00:00.000Z",
|
||
);
|
||
expect(result.rejected).toBe(3);
|
||
});
|
||
});
|
||
|
||
describe("Requirement: Критерии отбора — Scenario Outline: Отбор по условиям", () => {
|
||
it.each([
|
||
{ funding: "full", fee: null, language: "russian", expected: true },
|
||
{ funding: "full", fee: null, language: "not_required", expected: true },
|
||
{ funding: "partial", fee: null, language: "russian", expected: false },
|
||
{ funding: "full", fee: "300 EUR", language: "russian", expected: false },
|
||
{ funding: "full", fee: null, language: "required", expected: false },
|
||
] as const)("оплата $funding, взнос $fee, язык $language → $expected", ({ funding, fee, language, expected }) => {
|
||
expect(isEligible(raw({ funding, participationFee: fee, language }))).toBe(expected);
|
||
});
|
||
|
||
it("не клиентский сервис — не попадает", () => {
|
||
expect(isEligible(raw({ customerService: false }))).toBe(false);
|
||
});
|
||
|
||
it("взнос «нет» от модели считается бесплатным участием", () => {
|
||
const result = mergeIntoCatalog(emptyCatalog(), [{ ...raw(), participationFee: "нет" }], "r", "t");
|
||
expect(result.found).toBe(1);
|
||
});
|
||
});
|
||
|
||
describe("Requirement: Дедупликация", () => {
|
||
it("Scenario: Та же ссылка с UTM-меткой", () => {
|
||
const first = mergeIntoCatalog(emptyCatalog(), [raw()], "run-1", "2026-11-01T00:00:00.000Z");
|
||
const catalog = { ...first.catalog, lastSuccessfulRunId: "run-1" };
|
||
const second = mergeIntoCatalog(
|
||
catalog,
|
||
[raw({ sourceUrl: "http://www.example.org/alpina/?utm_source=x" })],
|
||
"run-2",
|
||
"2026-11-14T00:00:00.000Z",
|
||
);
|
||
expect(second.catalog.internships).toHaveLength(1);
|
||
expect(second.added).toBe(0);
|
||
const card = second.catalog.internships[0]!;
|
||
expect(card.checkedAt).toBe("2026-11-14T00:00:00.000Z");
|
||
expect(card.firstSeenAt).toBe("2026-11-01T00:00:00.000Z");
|
||
const shown = sortForDisplay({ ...second.catalog, lastSuccessfulRunId: "run-2" });
|
||
expect(shown[0]!.isNew).toBe(false);
|
||
});
|
||
|
||
it("normalizeUrl сохраняет значимые параметры и отбрасывает якорь", () => {
|
||
expect(normalizeUrl("https://Example.org/a/?id=5&utm_medium=m#top")).toBe("example.org/a?id=5");
|
||
expect(normalizeUrl("not a url")).toBeNull();
|
||
});
|
||
});
|
||
|
||
describe("sortForDisplay", () => {
|
||
it("показывает новые первыми", () => {
|
||
const first = mergeIntoCatalog(emptyCatalog(), [raw({ sourceUrl: "https://a.org/1" })], "run-1", "2026-11-01");
|
||
const second = mergeIntoCatalog(first.catalog, [raw({ sourceUrl: "https://a.org/2" })], "run-2", "2026-11-02");
|
||
const shown = sortForDisplay({ ...second.catalog, lastSuccessfulRunId: "run-2" });
|
||
expect(shown.map((card) => [card.id, card.isNew])).toEqual([
|
||
["a.org/2", true],
|
||
["a.org/1", false],
|
||
]);
|
||
});
|
||
});
|
||
|
||
describe("Requirement: Уверенность и смежность", () => {
|
||
it("Scenario: Уверенность не указана — low, смежность по умолчанию false", () => {
|
||
const rest: Record<string, unknown> = { ...raw() };
|
||
delete rest.confidence;
|
||
delete rest.adjacent;
|
||
const parsed = parseRawInternship(rest);
|
||
expect(parsed).toMatchObject({ confidence: "low", adjacent: false });
|
||
});
|
||
|
||
it("Scenario: Карточка со смежной сферой сохраняется с отметками", () => {
|
||
const merged = mergeIntoCatalog(emptyCatalog(), [raw({ confidence: "medium", adjacent: true })], "r1", "2026-11-14T00:00:00Z");
|
||
expect(merged.catalog.internships[0]).toMatchObject({ confidence: "medium", adjacent: true });
|
||
});
|
||
});
|
||
|
||
describe("Requirement: Показанные карточки", () => {
|
||
it("Scenario: Ручной запуск помечает показанным, запуск по расписанию — нет", () => {
|
||
const scheduled = mergeIntoCatalog(emptyCatalog(), [raw()], "r1", "2026-11-14T00:00:00Z", "scheduled");
|
||
expect(scheduled.catalog.internships[0]!.shownAt).toBeNull();
|
||
const manual = mergeIntoCatalog(scheduled.catalog, [raw()], "r2", "2026-11-15T00:00:00Z", "manual");
|
||
expect(manual.catalog.internships[0]!.shownAt).toBe("2026-11-15T00:00:00Z");
|
||
});
|
||
|
||
it("isExpired: прошедший срок, будущий, не указан, не разобран", () => {
|
||
expect(isExpired("2026-11-13", "2026-11-14")).toBe(true);
|
||
expect(isExpired("2026-11-14", "2026-11-14")).toBe(false);
|
||
expect(isExpired(null, "2026-11-14")).toBe(false);
|
||
expect(isExpired("осенью", "2026-11-14")).toBe(false);
|
||
});
|
||
});
|