import { describe, expect, it } from "vitest"; import { ContentRegistry, TimeSystem, World, WorldEventBus, type TimeDef } from "../src/index.js"; function setup() { const registry = new ContentRegistry(); const world = new World(); const events = new WorldEventBus(); const timeDef: TimeDef = { id: "default", dayLengthTicks: 4, seasons: [ { name: "spring", dayCount: 3 }, { name: "summer", dayCount: 3 }, ], start: { day: 1, season: "spring" }, }; registry.register("time", timeDef); const ts = new TimeSystem(); ts.init({ world, registry, events }); return { ts, events }; } describe("TimeSystem", () => { it("starts from the TimeDef start state", () => { const { ts } = setup(); expect(ts.get()).toEqual({ day: 1, season: "spring", tickInDay: 0 }); }); it("advance across a day boundary emits day-start", () => { const { ts, events } = setup(); const seen: unknown[] = []; events.on("day-start", (p) => seen.push(p)); ts.advance(3); expect(seen).toHaveLength(0); expect(ts.get()).toEqual({ day: 1, season: "spring", tickInDay: 3 }); ts.advance(1); expect(seen).toHaveLength(1); expect(seen[0]).toEqual({ day: 2, season: "spring" }); expect(ts.get()).toEqual({ day: 2, season: "spring", tickInDay: 0 }); }); it("advance across a season boundary emits season-change", () => { const { ts, events } = setup(); const seen: unknown[] = []; events.on("season-change", (p) => seen.push(p)); ts.advance(4 * 3); expect(seen).toHaveLength(1); expect(seen[0]).toEqual({ day: 1, season: "summer" }); expect(ts.get()).toEqual({ day: 1, season: "summer", tickInDay: 0 }); }); it("advance across a season boundary also emits day-start for the new day", () => { const { ts, events } = setup(); const days: unknown[] = []; const seasons: unknown[] = []; events.on("day-start", (p) => days.push(p)); events.on("season-change", (p) => seasons.push(p)); ts.advance(4 * 3); expect(seasons).toHaveLength(1); expect(days).toHaveLength(3); expect(days[2]).toEqual({ day: 1, season: "summer" }); }); it("set jumps directly without emitting", () => { const { ts, events } = setup(); const seen: unknown[] = []; events.on("day-start", (p) => seen.push(p)); events.on("season-change", (p) => seen.push(p)); ts.set(10, "winter"); expect(ts.get()).toEqual({ day: 10, season: "winter", tickInDay: 0 }); expect(seen).toHaveLength(0); }); });