diff --git a/README.md b/README.md index 680a14a..9b8e056 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ app can need any combination of, or none: `@dhis2/d2-i18n` — non-breaking (current major) by default for each, with an explicit choice offered whenever `latest` would cross a major version. Upgrading `@dhis2/multi-calendar-dates`/`@js-temporal/polyfill` is left to the separate - `multi-calendar-dates-upgrade` skill. + `calendar-upgrade` skill. - **README refresh.** A pnpm badge alongside existing badges, an app description pulled from the App Hub when the app is published there, and the scaffold's verbose "Available Scripts" section collapsed into a concise "Get Started". @@ -106,6 +106,28 @@ still renders after logging in. npx skills add dhis2/ai-devtools --skill modernise-apps ``` +## `@dhis2/skill-calendar-upgrade` + +An AI skill for moving an existing DHIS2 app onto `@dhis2/multi-calendar-dates` `3.x` and +`@js-temporal/polyfill` `0.5.1`. `3.x` removed Temporal types from the library's public API, +so this is treated as a behavioural migration rather than a routine bump: + +- **Library bump.** A before/after regression snapshot test of every direct import + (`getNowInCalendar`, the period helpers, `useDatePicker`) across the `gregory`, + `ethiopian` and `nepali` calendars, captured before bumping and re-run unmodified after. +- **Helper migration.** Replaces the app's own calendar date formatting and arithmetic with + the library's `formatCalendarDate`, `addToCalendarDate` and `getDaysInMonth`. +- **Direct polyfill upgrade.** Moves apps that import `@js-temporal/polyfill` from `0.4.3` to + `0.5.1`, with snapshot tests and a revertable patch for CLDR 48's Ethiopic era codes. +- **Bundle check.** Lists the Temporal copies that end up in the production build from its + source maps, and works to get them down to one. + +### Install + +```sh +npx skills add dhis2/ai-devtools --skill calendar-upgrade +``` + --- ## Contributing diff --git a/src/skills/calendar-upgrade/SKILL.md b/src/skills/calendar-upgrade/SKILL.md new file mode 100644 index 0000000..38cce49 --- /dev/null +++ b/src/skills/calendar-upgrade/SKILL.md @@ -0,0 +1,136 @@ +--- +name: calendar-upgrade +description: > + Guide for upgrading an existing DHIS2 app to @dhis2/multi-calendar-dates 3.x (3.2.0+) and + @js-temporal/polyfill 0.5.1. Use this skill whenever the user wants to bump or update + @dhis2/multi-calendar-dates or @js-temporal/polyfill, fix code broken by + multi-calendar-dates 3.x (getNowInCalendar or convertFromIso8601/convertToIso8601 no longer + returning Temporal objects), replace an app's own calendar date formatting/arithmetic with + multi-calendar-dates' formatCalendarDate, addToCalendarDate or getDaysInMonth helpers, + remove an @js-temporal/polyfill 0.4.3 resolutions pin, fix "Era am ... was not matched by + any era" errors for Ethiopian dates, or check which Temporal copies end up in an app's + bundle. For general tooling modernisation (pnpm, lint configs, CI, @dhis2/ui/app-runtime + bumps), use the modernise-apps skill instead. +--- + +# Upgrading DHIS2 Calendar Libraries + +You are helping a developer move an existing DHIS2 app onto `@dhis2/multi-calendar-dates` +`3.x` (target `3.2.0`+) and, where the app imports it directly, `@js-temporal/polyfill` +`0.5.1`. Unlike a routine dependency bump, this is a **behavioural migration**: +[`dhis2/multi-calendar-dates#102`](https://github.com/dhis2/multi-calendar-dates/pull/102) +removed Temporal types from the library's public API, so wrong results compile and run fine. +Every step is guarded by regression snapshots captured _before_ the change. + +It covers four tasks, in this order — an app may need only some of them: + +1. **Bump `@dhis2/multi-calendar-dates` to `3.2.0`+**, with a before/after regression test + covering every direct import from the library. +2. **Migrate the app's own calendar date code onto the library's helpers** + (`formatCalendarDate`, `addToCalendarDate`, `getDaysInMonth`), deleting the app's copies. +3. **Upgrade direct `@js-temporal/polyfill` usage from `0.4.3` to `0.5.1`**, and patch + `0.5.1`'s Ethiopic era handling (applied by default, offered for revert). +4. **Check which Temporal copies end up in the production build**, and reduce them to the + library's vendored copy plus `0.5.1`. + +Nothing here fixes `multi-calendar-dates` `2.x` or the polyfill's `0.4.3`. Patching those is +only a temporary, uncommitted step to capture a test baseline before bumping. + +## First: check `@dhis2/ui` + +`@dhis2/ui` versions before `10.19.3` bring their own `multi-calendar-dates` `2.x` (and the +`0.4.3` polyfill), so the app can't end up with a single `3.x`/`0.5.1` copy until `@dhis2/ui` +is at least `10.19.3` — directly, or through the app shell (`@dhis2/cli-app-scripts`). Check: + +```bash +node -p "require('./package.json').dependencies?.['@dhis2/ui']" +npm view @dhis2/ui@10 version +``` + +- **Already `10.19.3`+**: nothing to do. +- **On `10.x` below `10.19.3`**: bump `@dhis2/ui` to the latest `10.x` first, in its own + commit. It's a same-major bump, so it should be safe; run `lint`/`test` after it. If the + `modernise-apps` skill is installed, follow its platform library bump step + (`references/platform-library-bumps.md`, Step 1). +- **On `9.x` or older**: that's a breaking upgrade of its own. Don't fold it into this + migration — ask the user whether to do it first as a separate task, or to go ahead without + it and accept the extra copies (reported in the summary, see + `references/bundle-temporal-check.md`). + +## Which tasks apply? + +| Check | How | Result | +| --------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- | --------------------------------------------------------------- | +| `@dhis2/multi-calendar-dates` is in `package.json` and below `3.2.0` | Read `package.json` + `npm view @dhis2/multi-calendar-dates dist-tags` | **Library behind** — `references/multi-calendar-dates-bump.md` | +| `src` formats calendar dates (`eraYear`, `padStart`) or does month/day arithmetic around `multi-calendar-dates` calls | See the grep in `references/multi-calendar-dates-bump.md` | **Hand-rolled date code** — migrate it to the `3.2.0` helpers | +| `src` imports `@js-temporal/polyfill` and it's pinned below `0.5.1` (deps or `resolutions`) | `grep -rln "@js-temporal/polyfill" src` + read `package.json` | **Polyfill behind** — `references/temporal-polyfill-upgrade.md` | +| Any of the above was done | — | **Check the bundle** — `references/bundle-temporal-check.md` | +| None of the packages is used | — | **Nothing to do** — this skill doesn't apply | + +## What does the user need? + +| Scenario | References (read in order) | +| --------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- | +| Bump `@dhis2/multi-calendar-dates` to the latest version | `references/multi-calendar-dates-bump.md`, then `references/bundle-temporal-check.md` | +| Replace the app's own calendar date formatting/arithmetic with `multi-calendar-dates` helpers | `references/multi-calendar-dates-bump.md` ("Replace hand-rolled calendar date code") | +| Upgrade direct `@js-temporal/polyfill` usage from `0.4.3` to `0.5.1` | `references/temporal-polyfill-upgrade.md`, then `references/bundle-temporal-check.md` | +| Check which Temporal copies end up in the build, and reduce them to `0.5.1` | `references/bundle-temporal-check.md` | + +## Rules + +- **`@dhis2/multi-calendar-dates`'s `3.x` is a real breaking change — don't treat it like a + routine bump.** `getNowInCalendar` no longer returns anything Temporal-shaped (calling + `.add()` etc. on it throws). Target `3.2.0`+. +- **Migrate hand-rolled calendar date code to the library's helpers — don't re-implement it.** + Every place the app formats a calendar date (`eraYear ?? year` + padding), adds months/ + years/days to one (13-month calendar lists, ISO round-trips, `validateDateString` clamping + loops) or works out days in a month should use `formatCalendarDate`, `addToCalendarDate` or + `getDaysInMonth` from `3.2.0`, and the app's own helper should be deleted. Do it in its own + commit after the bump; the regression snapshots should stay unchanged, and any change must + be explained. See `references/multi-calendar-dates-bump.md`. +- **Before bumping `@dhis2/multi-calendar-dates`, write a before/after regression test + covering every direct import from the library** — `getNowInCalendar`, the + period-calculation helpers (`getFixedPeriodByDate`, `getAdjacentFixedPeriods`, etc. — + `#102` rewrote these too, not just `getNowInCalendar`), and `useDatePicker` if the app + uses it directly. The exclusion is `@dhis2/ui`'s own bundled `Calendar`/`DatePicker` + components (a separate internal copy this app doesn't control), not this library's own + hook — check the import, not the visible UI. Cover `gregory`, `ethiopian`, and `nepali` + for each; run the test against the currently-installed version first to confirm it's a + real baseline, then bump and re-run it unmodified. Add it as a real committed test if the + app has a test runner; otherwise use a temporary standalone script (a headless-hook test + via `renderHook` for `useDatePicker` if used) and delete it once confirmed safe. Don't rely + on a `lint`/generic `test` pass alone as proof this is safe. See + `references/multi-calendar-dates-bump.md`. +- **Before upgrading a direct `@js-temporal/polyfill` dependency to `0.5.1`, snapshot-test + every function in the app that calls it**, against `gregory`, `ethiopian` and `nepali` + (those three matter, the rest don't), with "now" pinned. Capture the snapshots on `0.4.3` + first, then re-run them unmodified with `--ci` on `0.5.1`. Declare `0.5.1` as a real + dependency rather than a `resolutions` pin. On CLDR 48 runtimes, Ethiopic throws in **both** + versions ([LIBS-830](https://dhis2.atlassian.net/browse/LIBS-830)). Patch `0.4.3` only + temporarily, to capture the baseline, and **never commit that patch**. **Apply the `0.5.1` + patch by default**, in its own commit. In the summary, **offer to revert it**: say that the patch + file is ~516 KB because the dist is minified onto one line, and that the alternative is to + wait for the upstream fix. If reverted, leave `ethiopian` out of these tests and note the + known issue in the PR description. See `references/temporal-polyfill-upgrade.md`. +- **After bumping `@dhis2/multi-calendar-dates` or `@js-temporal/polyfill`, check the + production build's source maps for Temporal copies.** The only one should be + `multi-calendar-dates`' vendored copy (plus a direct `@js-temporal/polyfill@0.5.1` if the + app imports it). Flag multiple versions in the lockfile, and any extra copy in the bundle. + Try to get the bundle down to `0.5.1` only: bump the package that brings the old copy in, + or else force `0.5.1` with `resolutions`/`pnpm.overrides` after snapshot-testing that code, + plus the Ethiopic patch if Ethiopic matters for the app. See + `references/bundle-temporal-check.md`. + +## Troubleshooting + +| Symptom | Fix | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| The app has its own date formatting, month/day arithmetic or days-in-month code around `multi-calendar-dates` calls | Migrate it to `formatCalendarDate`/`addToCalendarDate`/`getDaysInMonth` and delete the app's copy. See "Replace hand-rolled calendar date code with the library's helpers" in `references/multi-calendar-dates-bump.md`. | +| A regression snapshot changes when moving onto the `3.2.0` helpers | Explain it before updating: the `ethiopic` id getting the era year, a nonexistent date (e.g. month 13 in a 12-month calendar) now throwing, or Nepali month arithmetic no longer drifting are expected. Anything else is a regression. See `references/multi-calendar-dates-bump.md`. | +| `RangeError: Era am (ISO year ...) was not matched by any era` on the `ethiopian` calendar while capturing the baseline | Unrelated `@js-temporal/polyfill@0.4.3` bug with CLDR 48's new Ethiopic era codes on newer Node. Temporarily patch the polyfill to capture the baseline, then revert it and never commit it (`3.x` vendors the fix). See "If `ethiopian` fails before you've bumped anything" in `references/multi-calendar-dates-bump.md`. | +| The before/after regression test fails on the `nepali` calendar specifically, but `gregory`/`ethiopian` pass | Expected to be the highest-risk case — `#102` rewrote Nepali support from scratch (it's the one calendar this library implements itself rather than delegating to CLDR). Don't assume it's a test bug; diff the actual before/after values and check the library's own changelog/tests for that release before concluding it's safe. | +| `warning Resolution field "@js-temporal/polyfill@0.4.3" is incompatible with requested version "@js-temporal/polyfill@0.5.1"` | The app pins the polyfill in `resolutions`, forcing `multi-calendar-dates`/`@dhis2/ui` onto `0.4.3`. Follow `references/temporal-polyfill-upgrade.md`: snapshot-test direct usages, then move to `0.5.1` as a real dependency. | +| `RangeError: Era am/aa … was not matched by any era` from app code (not just while capturing a baseline) | The app calls the polyfill directly for Ethiopic dates, and CLDR 48 era codes break it. After the upgrade, apply the `0.5.1` patch from `references/temporal-polyfill-upgrade.md` by default, then offer to revert it in the summary (~516 KB), noting the known issue in the PR if reverted. Don't fix it on `0.4.3`: that patch is only a temporary baseline aid. Upstream fix: [LIBS-830](https://dhis2.atlassian.net/browse/LIBS-830). | +| The `references/bundle-temporal-check.md` check shows an `@js-temporal/polyfill` entry other than a direct `0.5.1`, or a `multi-calendar-dates` below `3.x` (e.g. `1.3.2` under `@dhis2/analytics`) | An older `multi-calendar-dates` is bundled with its own polyfill (the polyfill may be hoisted to the top level, so check the versions, not the paths). Bump the package that brings it in; otherwise force `0.5.1` with `resolutions`/`pnpm.overrides`, snapshot-test that code, and add the Ethiopic patch from `references/temporal-polyfill-upgrade.md` if Ethiopic matters. See `references/bundle-temporal-check.md`. | +| No test runner is set up and it's unclear whether to add one just for this | Don't — that's a bigger decision than a dependency bump warrants. Use the standalone verify script instead, and delete it once the bump is confirmed safe. | +| Unsure whether `useDatePicker` is in scope for this app's regression test, since the app also uses `@dhis2/ui`'s `Calendar`/`DatePicker` | Check the import, not the visible UI — `import { useDatePicker } from '@dhis2/multi-calendar-dates'` is in scope (direct usage of the version this app controls); `@dhis2/ui`'s bundled `Calendar`/`DatePicker` components are not (they resolve their own separate internal copy). | diff --git a/src/skills/calendar-upgrade/evals/evals.json b/src/skills/calendar-upgrade/evals/evals.json new file mode 100644 index 0000000..03013ea --- /dev/null +++ b/src/skills/calendar-upgrade/evals/evals.json @@ -0,0 +1,17 @@ +{ + "skill_name": "calendar-upgrade", + "evals": [ + { + "id": 0, + "name": "multi-calendar-dates-bump-aggregate-data-entry-app", + "prompt": "Can you update @dhis2/multi-calendar-dates to the latest version in this app?", + "expected_output": "A real snapshot of dhis2/aggregate-data-entry-app on @dhis2/multi-calendar-dates ^2.1.2. The target is 3.2.0 or later, a genuine breaking change (dhis2/multi-calendar-dates#102): getNowInCalendar returns a plain { year, month, day, eraYear?, hour, minute, second } object instead of a Temporal.ZonedDateTime, and the library provides formatCalendarDate, addToCalendarDate and getDaysInMonth. This app's src/shared/date/get-now-in-calendar.js wraps it in getNowInCalendarString, whose stringifyDate helper builds the date string by hand (padding, eraYear only for ethiopian/ethiopic, and .hour/.minute/.second when `long: true` is passed; used by src/shared/date/date-utils.js and src/shared/locked-status/use-check-lock-status.js), and date-utils.js hand-builds date strings and adds days via ISO round-trips. The app's own pre-existing jest suite (src/shared/date/get-now-in-calendar.test.js) hard-asserts exact HH:MM:SS strings, including a timezone-correction case for Africa/Kigali (UTC+2) -- this is the actual acceptance test. The correct fix bumps to 3.2.0 or later, replaces stringifyDate with formatCalendarDate(now, { includeTime: long }), moves the hand-built date strings and day arithmetic in date-utils.js to formatCalendarDate/addToCalendarDate, and deletes the app's own helpers.", + "files": ["fixtures/real-aggregate-data-entry-app/"], + "assertions": [ + "@dhis2/multi-calendar-dates is bumped to 3.2.0 or later", + "stringifyDate and other hand-built calendar date strings are replaced with formatCalendarDate, with the app's own helpers deleted", + "src/shared/date/get-now-in-calendar.test.js still passes in full, including the Africa/Kigali timezone-correction case -- not just that eslint/tsc are silent" + ] + } + ] +} diff --git a/src/skills/calendar-upgrade/package.json b/src/skills/calendar-upgrade/package.json new file mode 100644 index 0000000..5d60c02 --- /dev/null +++ b/src/skills/calendar-upgrade/package.json @@ -0,0 +1,12 @@ +{ + "name": "@dhis2/skill-calendar-upgrade", + "version": "0.0.1", + "description": "AI skill for upgrading DHIS2 apps to @dhis2/multi-calendar-dates 3.x and @js-temporal/polyfill 0.5.1", + "license": "BSD-3-Clause", + "publishConfig": { + "access": "public" + }, + "exports": { + ".": "./SKILL.md" + } +} diff --git a/src/skills/calendar-upgrade/references/bundle-temporal-check.md b/src/skills/calendar-upgrade/references/bundle-temporal-check.md new file mode 100644 index 0000000..89a3c5b --- /dev/null +++ b/src/skills/calendar-upgrade/references/bundle-temporal-check.md @@ -0,0 +1,97 @@ +# Checking which Temporal copies end up in the build + +After bumping `@dhis2/multi-calendar-dates` (`references/multi-calendar-dates-bump.md`) or a direct +`@js-temporal/polyfill` (`references/temporal-polyfill-upgrade.md`), check what the **production build** contains. The only Temporal implementation in +the bundle should be the copy that `multi-calendar-dates` `3.x` vendors +(`@dhis2/multi-calendar-dates/build/es/vendor/temporal`, which already has the Ethiopic era +fix), plus `@js-temporal/polyfill@0.5.1` only if the app imports the polyfill itself. +`3.x` lists `@js-temporal/polyfill` as a dependency but doesn't import it at runtime. + +The lockfile alone doesn't answer this. It often has more copies than the bundle does: for +example, `@dhis2/analytics` still depends on `multi-calendar-dates` `1.x` with +`@js-temporal/polyfill@0.4.3`, but if the app only imports components that don't use +calendars (such as `OrgUnitDimension`), that code is tree-shaken out of the build. + +1. **List the copies in the lockfile**, and flag in the summary if there's more than one + version of either package: + + ```bash + yarn why @js-temporal/polyfill # or: pnpm why @js-temporal/polyfill + yarn why @dhis2/multi-calendar-dates + ``` + +2. **Build, then list the `multi-calendar-dates` and `@js-temporal/polyfill` copies in the + bundle**, from the source maps the platform build writes next to the bundles in + `build/app`. Run this from the app's root after the build: + + ```bash + node -e ' + const fs = require("fs") + const path = require("path") + const maps = [] + const walk = (dir) => + fs.readdirSync(dir, { withFileTypes: true }).forEach((e) => { + const p = path.join(dir, e.name) + if (e.isDirectory()) walk(p) + else if (p.endsWith(".js.map")) maps.push(p) + }) + walk("build/app") + const copies = {} + for (const map of maps) { + for (const source of JSON.parse(fs.readFileSync(map, "utf8")).sources) { + const match = source.match( + /node_modules\/(?:.*\/)?(?:@js-temporal\/polyfill|@dhis2\/multi-calendar-dates)(?=\/)/ + ) + if (match && !copies[match[0]]) { + const { version } = require(path.resolve(match[0], "package.json")) + copies[match[0]] = version + } + } + } + console.log(maps.length ? copies : "no source maps found in build/app") + ' + ``` + + **Expected:** only `multi-calendar-dates` `3.x` entries (its Temporal is the vendored + copy, which these source maps show under `build/es/vendor/temporal`), and no + `@js-temporal/polyfill` entry, unless the app imports the polyfill itself, in + which case it must be `0.5.1`. **Flag anything else.** Look at versions, not paths: an old + copy can be hoisted to the top-level `node_modules/@js-temporal/polyfill` even though only + a nested package uses it. For example, maps-app (which uses analytics' period selectors) + bundles: + + ```text + { 'node_modules/@dhis2/multi-calendar-dates': '3.2.0', + 'node_modules/@js-temporal/polyfill': '0.4.3', + 'node_modules/@dhis2/analytics/node_modules/@dhis2/multi-calendar-dates': '1.3.2' } + ``` + + Here analytics' period selectors run on `multi-calendar-dates` `1.3.2` with the `0.4.3` + polyfill, which has no Ethiopic era fix (and adds to the bundle's size). climate-app, + which only imports `OrgUnitDimension` from analytics, bundles just + `{ 'node_modules/@dhis2/multi-calendar-dates': '3.2.0' }`, although its lockfile has the + same extra copies. + +3. **If there are extra copies, try to get the bundle down to `0.5.1` only:** + - First bump whatever pulls the old copy in, using `yarn why`/`pnpm why`: `@dhis2/ui` + `10.19.3`+ (older versions bring `multi-calendar-dates` `2.x`), the app shell (the + `@dhis2/cli-app-scripts`/app-platform release that has `@dhis2/ui` `10.19.3`+), or + `@dhis2/analytics` once it's on `multi-calendar-dates` `3.x`. + - If that's not possible, force the remaining copies to `0.5.1` with `resolutions` (yarn 1) + or `pnpm.overrides` (pnpm): `"@js-temporal/polyfill": "0.5.1"`. Those packages weren't + released against `0.5.1`, so before keeping the override, snapshot-test the app code that + goes through them (the same approach as the other two references, with `gregory`, `ethiopian` and `nepali`). + This is the only case where a `resolutions` pin is right here: `references/temporal-polyfill-upgrade.md` declares `0.5.1` as + a direct dependency instead, because there the app imports it itself. + - **If Ethiopic matters for the app** (it's used on Ethiopian-calendar instances, and the + code that goes through the forced copy handles server-calendar dates, such as analytics' + period selectors), also apply the `0.5.1` Ethiopic era patch from `references/temporal-polyfill-upgrade.md`. Unpatched, + `0.5.1` still throws `Era am … was not matched by any era` on CLDR 48 runtimes. Say in the + summary that the patch was applied, and offer to revert it, as there. + - If neither works (e.g. the old copy is pinned exactly and overriding it breaks the + snapshot tests), leave it and report it in the summary and the PR description, naming + which package brings it in. + +4. **Re-run the build and the source-map check**, and put the result in the PR description: + which copies the lockfile has, which ones the bundle has, and why any extra one is still + there. diff --git a/src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md b/src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md new file mode 100644 index 0000000..e8786f4 --- /dev/null +++ b/src/skills/calendar-upgrade/references/multi-calendar-dates-bump.md @@ -0,0 +1,348 @@ +# Bumping `@dhis2/multi-calendar-dates` to `3.2.0`+ + +Check for `@dhis2/multi-calendar-dates` in `package.json`. Its `3.x` line is now `latest`. +Target **`3.2.0` or later**, which has the date helpers this step migrates the app onto (see +"Replace hand-rolled calendar date code with the library's helpers" below). Confirm the +current version: + +```bash +npm view @dhis2/multi-calendar-dates dist-tags +``` + +This is a real breaking change, not a routine bump, regardless of which dist-tag it ships +under — [`dhis2/multi-calendar-dates#102`](https://github.com/dhis2/multi-calendar-dates/pull/102) +removed Temporal types from the public API entirely. Specifically, **`getNowInCalendar` now +returns a plain `{ year, month, day, eraYear?, hour, minute, second }` object instead of a +`Temporal.ZonedDateTime`**, with the time of day in the `timezone` it's given. +`convertFromIso8601`/`convertToIso8601` return the same plain `{ year, month, day, eraYear? }` +shape. Grep for `getNowInCalendar` (the app may wrap it, like +`aggregate-data-entry-app`'s `getNowInCalendarString` does) and check every call site for: + +- **Calling a Temporal method on the result** (`.add()`, `.withCalendar()`, `.startOfDay()`, + etc.) — these don't exist on the plain object anymore, and the call throws. Don't rebuild + the arithmetic by hand: use `addToCalendarDate` (see the helpers section below). +- **Building a date-time string by hand** from `.hour`/`.minute`/`.second` — use + `formatCalendarDate(now, { includeTime: true })` instead (see the helpers section below). +- **Reading `.year` for an Ethiopic date** — `year` is the Amete Alem year (e.g. `7516`); + the year DHIS2 shows is `eraYear` (`2016`). Islamic has no `eraYear`. Always read + `eraYear ?? year` (`formatCalendarDate` does this for you), never + `calendar === 'ethiopian' ? eraYear : year`, which misses the `ethiopic` id. +- Plain reads of `.month`/`.day`/`.eraYear` need no changes — that part of the shape is + unchanged. + +## Write a before/after regression test for direct library usage + +Don't just eyeball the call sites above — this is a real behavioral change that can silently +produce wrong output (not a compile error, not a thrown exception), so pin down the exact +current output with a test, confirm it passes against the version installed _right now_ +(before bumping), then bump and re-run the identical test unmodified. If it still passes, +behavior is provably unchanged; if it fails, you've caught a real regression before it ships, +not after. + +**Test every direct import from `@dhis2/multi-calendar-dates` itself, including +`useDatePicker` — the exclusion is `@dhis2/ui`'s own bundled `Calendar`/`DatePicker` +components, not this library's own hook.** It's easy to conflate the two, but they're not +the same thing: + +- **In scope** — anything the app imports directly from `@dhis2/multi-calendar-dates`: + `getNowInCalendar`, `convertFromIso8601`, `convertToIso8601`, the period-calculation + helpers (`getFixedPeriodByDate`, `getAdjacentFixedPeriods`, `generateFixedPeriods`, + `createFixedPeriodFromPeriodId`), `useDatePicker`, or a wrapper the app built on any of + these. **`#102` touched the period-calculation internals extensively** (daily/monthly/ + weekly/yearly period generation, adjacent-period lookup, period-from-id construction) and + rewrote `useDatePicker` to use the new plain-date shape — these are not lower-risk than + `getNowInCalendar` just because they weren't named in the PR's headline breaking-change + note. +- **Out of scope** — `@dhis2/ui`'s `Calendar`/`DatePicker` React components. Those bundle + their _own_ internal `multi-calendar-dates` dependency (a separate resolved version this + app doesn't control — see the Troubleshooting entry on this in `SKILL.md`), have their own test coverage + upstream, and testing them here wouldn't exercise the version this app is actually bumping. + +Cover at least these three calendars for **every** function/hook under test above — they're +the most behaviorally distinct: `gregory` (the default, CLDR-backed), `ethiopian` +(CLDR-backed but uses `eraYear` instead of `year`), and `nepali` (the one calendar this +library implements itself rather than delegating to CLDR — per `#102`'s own description, +this is the part that got rewritten most substantially, so it's the highest-risk case for a +subtle regression, and it's exactly the kind of calendar a period-calculation helper or +`useDatePicker` could be driving just as easily as `getNowInCalendar`). + +### If `ethiopian` fails before you've bumped anything (Node 24 / CLDR 48) + +On newer Node (anything after 20.19 — e.g. Node 24, which ships CLDR 48), the `ethiopian` +calendar can throw `RangeError: Era am (ISO year 2016) was not matched by any era` against +the **currently-installed** `2.x` — so the baseline can't be captured at all. This is an +unrelated upstream bug, not something the bump caused: CLDR 48 changed the Ethiopic era codes +`Intl` reports (to `am`/`aa`), and the `@js-temporal/polyfill@0.4.3` that `2.x` depends on +doesn't recognise them +([`js-temporal/temporal-polyfill#357`](https://github.com/js-temporal/temporal-polyfill/pull/357)). +**`3.x` doesn't have this problem** — it vendors the polyfill's calendar code with the era fix +applied — so this only blocks the _before_ run. Confirm the cause first: + +```bash +# resolves the polyfill copy multi-calendar-dates actually uses (it isn't a direct app dependency) +node -p "const {Temporal}=require(require.resolve('@js-temporal/polyfill',{paths:[require.resolve('@dhis2/multi-calendar-dates')]})); Temporal.PlainDate.from('2024-06-15').withCalendar('ethiopic').eraYear" +``` + +To capture the baseline, temporarily patch `@js-temporal/polyfill@0.4.3` (confirm the version +with `pnpm why`/`yarn why @js-temporal/polyfill`). **This patch is a local baseline aid only. +Never commit it, and revert it once the baseline is captured.** It doesn't fix `2.x`, and +nothing here should. Edit **both** `dist/index.cjs` (what jest loads) and `dist/index.esm.js` +(what bundlers load). #357's diff doesn't apply to `0.4.x` as-is (eras here are named +`era0`/`era1`, with no alias list), so add a `reviseIntlEra` to `EthiopicHelper` that maps the +new codes onto them, right after its constructor. The built code is minified, so search for +this exact string: + +```diff + super("ethiopic",[{name:"era0",isoEpoch:{year:-5492,month:7,day:17}},{name:"era1",isoEpoch:{year:8,month:8,day:27},anchorEpoch:{year:5501}}])} ++reviseIntlEra(e){let{era:t,eraYear:r}=e;return"am"===t&&(t="era1"),"aa"===t&&(t="era0"),{era:t,eraYear:r}} +``` + +- **yarn 1:** edit the files in `node_modules/@js-temporal/polyfill/dist/` directly. No + `patch-package` is needed, and the next `yarn install` undoes it. +- **pnpm:** don't edit `node_modules` in place. Its files are hard-linked to the global + store, so the edit would leak into every other project. Use `pnpm patch +@js-temporal/polyfill@0.4.3`, edit the files in the directory it prints, then + `pnpm patch-commit