Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

3.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Carbon (github.com/crbnos/carbon). Modified or adapted versions of this file,
// including ports, remain AGPLv3; serving them over a network requires releasing their source.

import { parseAbsolute } from "@internationalized/date";
import { it } from "vitest";
import { toIsoDate } from "./date-utils.ts";
import {
  clipToQualification,
  isEligibleOperator,
  type QualifiedEmployee
} from "./operator-eligibility.ts";
import { assert, assertEquals } from "./test-helpers.ts";

const utc = (iso: string) => parseAbsolute(iso, "UTC").toDate().getTime();

const opStart = utc("2026-07-17T08:00:00.000Z");

function employee(
  overrides: Partial<QualifiedEmployee> = {}
): QualifiedEmployee {
  return {
    employeeId: "emp-1",
    expiresAt: null,
    ...overrides
  };
}

it("expired-as-of-op-start is excluded from the pool", () => {
  const expired = employee({ expiresAt: "2026-07-07" });
  assertEquals(isEligibleOperator(expired, opStart), false);
});

it("expiring after the op start still counts", () => {
  const stillValid = employee({ expiresAt: "2026-07-20" });
  assertEquals(isEligibleOperator(stillValid, opStart), true);
});

it("expiring exactly on the op start date is excluded (strict >)", () => {
  const expiresToday = employee({ expiresAt: "2026-07-17" });
  assertEquals(isEligibleOperator(expiresToday, opStart), false);
});

it("null expiry never expires", () => {
  assertEquals(isEligibleOperator(employee(), opStart), true);
});

it("regression: pg DATE object stringified via String() defeated the expiry check; toIsoDate restores it", () => {
  const pgDateExpired = new Date(2026, 6, 7); // DATE '2026-07-07' as returned by pg

  // Pre-fix behavior: String(...) made the expired welder eligible
  const broken = employee({ expiresAt: String(pgDateExpired) });
  assertEquals(isEligibleOperator(broken, opStart), true);

  // Post-fix behavior: normalized date excludes them
  const normalized = employee({ expiresAt: toIsoDate(pgDateExpired) });
  assertEquals(isEligibleOperator(normalized, opStart), false);
  assert(toIsoDate(pgDateExpired) === "2026-07-07");
});

it("expiry boundary respects the factory time zone", () => {
  const employee: QualifiedEmployee = {
    employeeId: "emp-1",
    expiresAt: "2026-07-21"
  };
  // 20:00 UTC on the 20th is already the 21st in India: the qualification
  // expiring on the 21st is expired-as-of-start there, but not in UTC.
  const start = utc("2026-07-20T20:00:00.000Z");
  assert(isEligibleOperator(employee, start, "UTC"));
  assert(!isEligibleOperator(employee, start, "Asia/Kolkata"));
});

// America/Chicago is UTC-5 in late October 2026.
it("a qualification ends at the start of its expiry day in the factory zone", () => {
  const windows = [
    // Mon 26 Oct 06:00-14:30 local: before expiry, kept whole
    { start: utc("2026-10-26T11:00:00Z"), end: utc("2026-10-26T19:30:00Z") },
    // a night shift Mon 22:00 -> Tue 06:00 local: cut at Tue 00:00 local
    { start: utc("2026-10-27T03:00:00Z"), end: utc("2026-10-27T11:00:00Z") },
    // Thu 29 Oct 06:00-14:30 local: after expiry, dropped
    { start: utc("2026-10-29T11:00:00Z"), end: utc("2026-10-29T19:30:00Z") }
  ];

  assertEquals(clipToQualification(windows, "2026-10-27", "America/Chicago"), [
    { start: utc("2026-10-26T11:00:00Z"), end: utc("2026-10-26T19:30:00Z") },
    { start: utc("2026-10-27T03:00:00Z"), end: utc("2026-10-27T05:00:00Z") }
  ]);
});

it("a qualification with no expiry keeps every window", () => {
  const windows = [{ start: opStart, end: opStart + 3_600_000 }];
  assertEquals(clipToQualification(windows, null, "America/Chicago"), windows);
});