deadhead-space-freight-tycoon/apps/server/test/time.test.ts

29 lines
1.3 KiB
TypeScript

import { content } from "@deadhead/content";
import { serverTimeSchema } from "@deadhead/schema";
import { realToGame } from "@deadhead/sim";
import { afterAll, describe, expect, it } from "vitest";
import { buildApp } from "../src/app";
import { createRecordingQueue } from "../src/jobs/queue";
import { testConfig } from "./helpers";
describe("server time endpoint", () => {
const app = buildApp(testConfig(), { jobs: createRecordingQueue() });
afterAll(() => app.close());
it("returns the current real time and the matching game time", async () => {
const response = await app.inject({ method: "GET", url: "/api/time" });
expect(response.statusCode).toBe(200);
const body = serverTimeSchema.parse(response.json());
expect(Math.abs(body.realTime - Date.now())).toBeLessThan(2000);
expect(body.gameTime).toBe(realToGame(body.realTime, content.config));
});
it("reads time from the injected clock", async () => {
const fixed = Date.parse("2100-06-01T12:00:00Z");
const fixedApp = buildApp(testConfig(), { clock: () => fixed, jobs: createRecordingQueue() });
const body = (await fixedApp.inject({ method: "GET", url: "/api/time" })).json();
expect(body).toEqual({ realTime: fixed, gameTime: realToGame(fixed, content.config) });
await fixedApp.close();
});
});