diff --git a/site/package.json b/site/package.json index 9b2e7099..702726a0 100644 --- a/site/package.json +++ b/site/package.json @@ -9,7 +9,7 @@ "lint": "next lint", "typecheck": "tsc --noEmit", "fetch:rates": "tsx scripts/fetch-rates.ts", - "prebuild": "node scripts/check-site-url.mjs && tsx scripts/fetch-rates.ts && node scripts/check-pressure-reference.mjs && node scripts/check-currency-rate-freshness.mjs", + "prebuild": "node --test scripts/workdays-faq-parity.test.mjs && node scripts/check-site-url.mjs && tsx scripts/fetch-rates.ts && node scripts/check-pressure-reference.mjs && node scripts/check-currency-rate-freshness.mjs", "sitemap": "tsx scripts/generate-sitemap.ts", "validate": "tsx scripts/validate.ts", "audit:seo": "tsx scripts/audit-seo.ts", @@ -29,7 +29,8 @@ "check:site-url": "node --test scripts/check-site-url.test.mjs", "audit:nattzy-cta": "node scripts/check-nattzy-cta.mjs", "test:nattzy-cta": "node --test scripts/check-nattzy-cta.test.mjs", - "audit:nattzy-timezone": "node scripts/check-nattzy-cta.mjs --timezone" + "audit:nattzy-timezone": "node scripts/check-nattzy-cta.mjs --timezone", + "test:workdays-faq": "node --test scripts/workdays-faq-parity.test.mjs" }, "dependencies": { "@types/qrcode": "^1.5.6", diff --git a/site/scripts/workdays-faq-parity.test.mjs b/site/scripts/workdays-faq-parity.test.mjs new file mode 100644 index 00000000..b8f525ff --- /dev/null +++ b/site/scripts/workdays-faq-parity.test.mjs @@ -0,0 +1,41 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; + +const root = new URL("../src/tools/workdays-calculator/", import.meta.url); +const locales = ["en","ja","es","de","fr","it","pt-BR","ar","zh-CN","zh-TW","ko","hi","id","ru","th","tr","vi"]; +const read = name => readFileSync(new URL(name, root), "utf8"); + +test("all 17 workday locales have an exclusive-date label and correct boundary FAQ", () => { + for (const lang of locales) { + const value = JSON.parse(read("messages/" + lang + ".json")); + const faq = value.faq[lang === "en" ? 2 : 0]; + const label = value.input.excludeEndpoints; + assert.equal(typeof label, "string", lang + " missing input label"); + assert.ok(label.length >= 6, lang + " input label too short"); + assert.ok(faq.a.includes(label), lang + " FAQ must mention the actual control"); + for (const v of ["0", "1", "2"]) assert.ok(faq.a.includes(v), lang + " FAQ needs 0/1/2 boundary handling"); + assert.ok(value.mode.range && value.mode.add, lang + " two modes must remain"); + assert.ok(value.input.holidays && value.input.includeSat && value.input.includeSun, lang + " original input keys missing"); + } +}); + +test("English copy uses the existing N-workdays mode and does not suggest manual holiday offsets", () => { + const english = JSON.parse(read("messages/en.json")); + assert.match(english.description, /N workdays from a date/); + assert.match(english.faq[1].a, /on this calculator/); + assert.match(english.faq[1].a, /All listed custom holidays are applied here/); + assert.doesNotMatch(english.faq[1].a, /use the Date Calculator|move that result forward one workday/i); + assert.match(english.article.sections[1].paragraphs[1], /Friday–Saturday weekend is not modeled directly/); +}); + +test("range UI renders zero-result exclusive counts and restores the checkbox from shared state", () => { + const component = read("Component.tsx"); + assert.match(component, /useState\(false\);[\s\S]*?\[excludeEndpoints, setExcludeEndpoints\]/); + assert.match(component, /d\.excludeEndpoints/); + assert.match(component, /checked=\{excludeEndpoints\}/); + assert.match(component, /t\("input\.excludeEndpoints"\)/); + assert.match(component, /countWorkdays\(sd, ed, weekend, hSet, excludeEndpoints\)/); + assert.match(component, /result && start <= end/); + assert.match(component, /mode === "range" && excludeEndpoints \? "\+exclusive"/); +}); diff --git a/site/src/tools/workdays-calculator/Component.tsx b/site/src/tools/workdays-calculator/Component.tsx index b5e49d2f..b016dd77 100644 --- a/site/src/tools/workdays-calculator/Component.tsx +++ b/site/src/tools/workdays-calculator/Component.tsx @@ -4,6 +4,7 @@ import { useMemo, useState } from "react"; import { useLocale, useTranslations } from "next-intl"; import { useShareableState } from "@/lib/hooks/useShareableState"; import { HOLIDAY_PRESETS, type HolidayCountry } from "./holidays"; +import { countWorkdays, isoDate } from "./workday-range"; const PRESET_OPTIONS = ["", "US", "JP", "UK", "DE"] as const; @@ -16,40 +17,6 @@ function parseHolidays(input: string): Set { ); } -// Local calendar date → "yyyy-mm-dd". (toISOString() is UTC: east of UTC — e.g. JST — it shifted -// every day back by one, so preset holidays matched the wrong weekday.) -function isoDate(d: Date): string { - return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`; -} - -function countWorkdays( - start: Date, - end: Date, - weekend: Set, - holidays: Set, -): { workdays: number; weekendDays: number; holidayDays: number; total: number } { - if (start > end) return { workdays: 0, weekendDays: 0, holidayDays: 0, total: 0 }; - let workdays = 0; - let weekendDays = 0; - let holidayDays = 0; - let total = 0; - const cur = new Date(start); - while (cur <= end) { - total++; - const dow = cur.getDay(); - const dStr = isoDate(cur); - if (weekend.has(dow)) { - weekendDays++; - } else if (holidays.has(dStr)) { - holidayDays++; - } else { - workdays++; - } - cur.setDate(cur.getDate() + 1); - } - return { workdays, weekendDays, holidayDays, total }; -} - // start から n 営業日後(n<0 は前)の日付。開始日自体は数えない。 function addWorkdays(start: Date, n: number, weekend: Set, holidays: Set): Date | null { const step = n < 0 ? -1 : 1; @@ -73,6 +40,7 @@ export default function WorkdaysCalculator() { const [end, setEnd] = useState(futureStr); const [includeSat, setIncludeSat] = useState(false); const [includeSun, setIncludeSun] = useState(false); + const [excludeEndpoints, setExcludeEndpoints] = useState(false); const [holidays, setHolidays] = useState(""); const [preset, setPreset] = useState<"" | HolidayCountry>(""); const [mode, setMode] = useState<"range" | "add">("range"); @@ -80,7 +48,7 @@ export default function WorkdaysCalculator() { useShareableState( "workdays-calculator", - { mode, start, end, days, includeSat, includeSun, holidays, preset }, + { mode, start, end, days, includeSat, includeSun, excludeEndpoints, holidays, preset }, (d) => { if (d.mode === "range" || d.mode === "add") setMode(d.mode); if (typeof d.start === "string") setStart(d.start); @@ -88,6 +56,7 @@ export default function WorkdaysCalculator() { if (typeof d.days === "string") setDays(d.days); if (typeof d.includeSat === "boolean") setIncludeSat(d.includeSat); if (typeof d.includeSun === "boolean") setIncludeSun(d.includeSun); + if (typeof d.excludeEndpoints === "boolean") setExcludeEndpoints(d.excludeEndpoints); if (typeof d.holidays === "string") setHolidays(d.holidays); if (d.preset === "" || (typeof d.preset === "string" && d.preset in HOLIDAY_PRESETS)) setPreset(d.preset as "" | HolidayCountry); }, @@ -95,8 +64,10 @@ export default function WorkdaysCalculator() { const presetInRange = useMemo(() => { if (!preset) return []; - return HOLIDAY_PRESETS[preset].filter((h) => h.date >= start && h.date <= end); - }, [preset, start, end]); + return HOLIDAY_PRESETS[preset].filter((h) => + h.date >= start && h.date <= end && !(mode === "range" && excludeEndpoints && (h.date === start || h.date === end)) + ); + }, [preset, start, end, mode, excludeEndpoints]); const afterDate = useMemo(() => { if (mode !== "add") return null; @@ -122,8 +93,8 @@ export default function WorkdaysCalculator() { if (preset) { for (const h of HOLIDAY_PRESETS[preset]) hSet.add(h.date); } - return countWorkdays(sd, ed, weekend, hSet); - }, [start, end, includeSat, includeSun, holidays, preset]); + return countWorkdays(sd, ed, weekend, hSet, excludeEndpoints); + }, [start, end, includeSat, includeSun, excludeEndpoints, holidays, preset]); const dateFmt = useMemo( () => new Intl.DateTimeFormat(locale, { month: "short", day: "numeric", year: "numeric", weekday: "short" }), @@ -131,7 +102,7 @@ export default function WorkdaysCalculator() { ); // GA4 calculate の label(ToolInteractionTracker が読む)。祝日プリセットと週末設定のみ=日付は送らない。 - const calcLabel = `${mode === "add" ? "add:" : ""}${preset || "none"}${includeSat ? "+sat" : ""}${includeSun ? "+sun" : ""}`; + const calcLabel = `${mode === "add" ? "add:" : ""}${preset || "none"}${includeSat ? "+sat" : ""}${includeSun ? "+sun" : ""}${mode === "range" && excludeEndpoints ? "+exclusive" : ""}`; return (
@@ -167,6 +138,12 @@ export default function WorkdaysCalculator() { setIncludeSun(e.target.checked)} /> {t("input.includeSun")} + {mode === "range" && ( + + )}