Rebasely Beta: data may be purged

examples/MCP-tools Public

TypeScript · 63 lines · 2.5 KiB · at d0ed32f

import assert from "node:assert/strict";import { test } from "node:test";import { ToolError } from "../src/tools/errors.ts";import { resolveZone, timeInZone } from "../src/tools/time-in-zone.ts";const now = new Date("2026-10-16T14:00:00Z");const at = (zone: string, instant?: string) => timeInZone.run({ zone, at: instant }, { now });test("gives the local time, weekday and offset for now", () => {  const r = at("Asia/Tokyo");  assert.equal(r.instant, "2026-10-16T14:00:00.000Z");  assert.equal(r.date, "2026-10-16");  assert.equal(r.time, "23:00:00");  assert.equal(r.weekday, "Friday");  assert.equal(r.utc_offset, "+09:00");  assert.equal(r.utc_offset_minutes, 540);  assert.match(r.formatted, /Friday, October 16, 2026/);});test("handles half-hour offsets and the other side of the date line", () => {  assert.equal(at("Asia/Kathmandu").utc_offset, "+05:45");  const honolulu = at("Pacific/Honolulu");  assert.equal(honolulu.utc_offset, "-10:00");  assert.equal(honolulu.date, "2026-10-16");  assert.equal(honolulu.time, "04:00:00");});test("applies daylight saving time at the given instant", () => {  assert.equal(at("America/New_York", "2026-07-01T12:00:00Z").utc_offset, "-04:00");  assert.equal(at("America/New_York", "2026-12-01T12:00:00Z").utc_offset, "-05:00");});test("accepts an instant with an offset", () => {  const r = at("Europe/London", "2026-10-16T09:00:00-05:00");  assert.equal(r.instant, "2026-10-16T14:00:00.000Z");  assert.equal(r.time, "15:00:00");});test("UTC has a zero offset", () => {  const r = at("utc");  assert.equal(r.zone, "UTC");  assert.equal(r.utc_offset, "+00:00");  assert.equal(r.time, "14:00:00");});test("resolves names case-insensitively and by city", () => {  assert.equal(resolveZone("europe/paris"), "Europe/Paris");  assert.equal(resolveZone("tokyo"), "Asia/Tokyo");  assert.equal(resolveZone("New York"), "America/New_York");});test("an unknown zone says what a valid one looks like", () => {  assert.throws(() => at("Mars/Olympus_Mons"), (err: unknown) => {    assert.ok(err instanceof ToolError);    assert.match(err.message, /Unknown time zone "Mars\/Olympus_Mons"\. Use an IANA name such as Europe\/Paris/);    return true;  });});test("an instant without an offset is refused, since it would be ambiguous", () => {  assert.throws(() => at("Asia/Tokyo", "2026-10-16T14:00:00"), /not an ISO 8601 instant with an offset/);  assert.throws(() => at("Asia/Tokyo", "next tuesday"), ToolError);});