sendou.ink/app/features/chat/chat-utils.test.ts
Kalle fef1ffc955
Design refresh + a bunch of stuff (#2864)
Co-authored-by: hfcRed <hfcred@gmx.net>
2026-03-19 17:51:42 +02:00

92 lines
2.2 KiB
TypeScript

import { sub } from "date-fns";
import { describe, expect, test } from "vitest";
import {
chatAccessible,
datePlaceholder,
resolveDatePlaceholders,
} from "./chat-utils";
describe("chatCodeVisible", () => {
test("visible when within expiration window", () => {
const result = chatAccessible({
isStaff: false,
expiresAfterDays: 1,
comparedTo: new Date(),
});
expect(result).toBe(true);
});
test("not visible when past expiration window", () => {
const result = chatAccessible({
isStaff: false,
expiresAfterDays: 1,
comparedTo: sub(new Date(), { days: 3 }),
});
expect(result).toBe(false);
});
test("staff gets 7 extra days", () => {
const result = chatAccessible({
isStaff: true,
expiresAfterDays: 1,
comparedTo: sub(new Date(), { days: 5 }),
});
expect(result).toBe(true);
});
test("staff extra days are not infinite", () => {
const result = chatAccessible({
isStaff: true,
expiresAfterDays: 1,
comparedTo: sub(new Date(), { days: 10 }),
});
expect(result).toBe(false);
});
});
describe("datePlaceholder", () => {
test("returns correctly formatted placeholder string", () => {
const date = new Date(1700000000000);
expect(datePlaceholder(date)).toBe("{{date:1700000000000}}");
});
});
describe("resolveDatePlaceholders", () => {
const mockFormat = (d: Date) => `FORMATTED:${d.getTime()}`;
test("replaces a single placeholder with formatted date", () => {
const text = "Starts at {{date:1700000000000}}";
expect(resolveDatePlaceholders(text, mockFormat)).toBe(
"Starts at FORMATTED:1700000000000",
);
});
test("replaces multiple placeholders in one string", () => {
const text = "From {{date:1700000000000}} to {{date:1700003600000}}";
expect(resolveDatePlaceholders(text, mockFormat)).toBe(
"From FORMATTED:1700000000000 to FORMATTED:1700003600000",
);
});
test("returns text unchanged when no placeholders present", () => {
const text = "Just a normal string";
expect(resolveDatePlaceholders(text, mockFormat)).toBe(text);
});
test("handles text that is only a placeholder", () => {
const text = "{{date:1700000000000}}";
expect(resolveDatePlaceholders(text, mockFormat)).toBe(
"FORMATTED:1700000000000",
);
});
});