mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-01 23:58:36 -05:00
Move app to apps/web-react in pnpm workspace layout
This commit is contained in:
101
apps/web-react/app/features/chat/chat-utils.test.ts
Normal file
101
apps/web-react/app/features/chat/chat-utils.test.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
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 just past expiration window", () => {
|
||||
const result = chatAccessible({
|
||||
isStaff: false,
|
||||
expiresAfterDays: 1,
|
||||
comparedTo: sub(new Date(), { days: 1, hours: 12 }),
|
||||
});
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
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",
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user