freetravel/test/internship.test.ts
2026-11-14 22:50:18 +05:00

97 lines
4.4 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 { emptyCatalog, isEligible, mergeIntoCatalog, normalizeUrl, 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],
]);
});
});