Skip to content

fix(test): stop two flaky tests from lying about the code - #845

Merged
huichen merged 1 commit into
mainfrom
fix/test-isolation-fixes
Sep 25, 2026
Merged

huichen merged 1 commit into
mainfrom
fix/test-isolation-fixes

Conversation

@huichen

@huichen huichen commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

Three failures met while working on the remote lane. All three are "the test is wrong" — none is a product bug — but two of them were hiding real behaviour behind a passing assertion, which is worse than a plain failure.

1. The recommendation budget reset itself for the first eight hours of every local day

today() reads the local calendar day and says so in a comment:

/** Local calendar day. `toISOString` is UTC, so it is not used here. */

The test then seeded the stored record with the UTC date:

day: new Date().toISOString().slice(0, 10),

On a UTC+8 host the two disagree between 00:00 and 08:00 local. In that window parseDay saw a record from "another day", discarded it, and the hook offered a skill it had already shown — so the test failed only at that hour, and only on hosts east of UTC. It looked like a passing suite every other time of day.

The test now uses the production today(), which makes the two calendars structurally impossible to drift apart.

A second test pins the invariant, and does so at a fixed instant with the local date parts forced to disagree with UTC. This matters: CI runs on UTC, where the local and UTC calendars always agree, so the natural assertion would be vacuous exactly where it needs to hold. Forcing the disagreement with mocks keeps it meaningful on any host.

2. The preview-cache sweep raced a sibling over one process-wide directory

preview_cache_dir() is $TMPDIR/futureos-attachments/mobile/preview-cache for the whole process, and preview_cache_survives_transfer_cleanup_and_invalidates_on_source_change calls clear_preview_cache(), which remove_dir_alls it — deleting the fresh.jpg the prune test was about to assert on. It failed only under full-suite load, when the two happen to interleave; in isolation it always passed.

Fixed by removing the shared state rather than serializing access to it: the sweep now takes its directory as a parameter (the shape persist_image_preview_cache already uses), the test passes its own private one, and afterwards exactly one test touches the shared cache. A mutex would have worked too, but it would have left the hazard in place for the next test that forgets to take it.

Added a case for a directory that does not exist — the state of every fresh install, and a path the sweep reaches on every transfer cycle.

3. Not fixed — reported instead

instance::tests::excludes_second_owner_and_releases_on_drop failed once in a full run and I could not reproduce it: 150 isolated runs, 320 concurrent runs of the same test binary, and 3 further full-suite runs all pass. I am not claiming a fix for it.

One candidate if it recurs: flock is interruptible, so an EINTR would land in the "Could not lock Desktop data directory…: {e}" arm instead of the contended "Desktop is already running" arm — failing this test's assertions and showing a confusing message to a real user. That is a hypothesis, not a diagnosis, and it is not changed here.

Verification

  • Desktop Rust: 1307 pass, 3 consecutive full-suite runs green (this bug only ever appeared under full-suite load). cargo fmt --check + cargo clippy --all-targets -D warnings clean on pinned 1.97.
  • Mobile: 128 suites / 2050 tests pass, including the test that used to fail — which I ran at the exact hour it used to fail (local 26th while UTC was still the 25th).
  • The new guard was falsified: switching today() to toISOString fails both new assertions.

Three failures I met while working on the remote lane, all of them "the test
is wrong", none of them a product bug.

**The recommendation budget reset itself for the first eight hours of every
local day.** `today()` reads the local calendar day and says so in a comment
("`toISOString` is UTC, so it is not used here"), but the test seeded the
stored record with `new Date().toISOString().slice(0, 10)`. For a UTC+8 host
the two disagree between 00:00 and 08:00 local, `parseDay` saw a record from
"another day", discarded it, and the hook offered a skill it had already
shown — so the test failed only in that window, and only on hosts east of
UTC. The test now uses the production `today()`, which makes the two
calendars structurally impossible to drift apart.

A second test file pins the invariant itself, and does so at a fixed instant
with the local date parts forced to disagree with UTC: on a UTC host (what CI
runs) the two calendars otherwise always agree, so without forcing it the
assertion would be vacuous exactly where it needs to hold.

**The preview-cache sweep test raced a sibling over one process-wide
directory.** `preview_cache_dir()` is `$TMPDIR/futureos-attachments/mobile/
preview-cache` for the whole process, and `preview_cache_survives_transfer_
cleanup_and_invalidates_on_source_change` calls `clear_preview_cache()`,
which `remove_dir_all`s it — deleting the `fresh.jpg` the prune test was
about to assert on. It only failed under full-suite load, when the two
happen to interleave; in isolation it always passed.

Removing the shared state rather than serializing access to it: the sweep
now takes its directory as a parameter (the shape
`persist_image_preview_cache` already uses), the test passes its own, and
afterwards exactly one test touches the shared cache. Added a case for a
directory that does not exist, which is the state of every fresh install.

**Not fixed, and reported rather than papered over:**
`instance::tests::excludes_second_owner_and_releases_on_drop` failed once in
a full run and I could not reproduce it — 150 isolated runs, 320 concurrent
runs of the same binary, and 3 further full-suite runs all pass. No fix is
claimed here. One candidate worth a look if it recurs: `flock` is
interruptible, so an `EINTR` would take the "Could not lock…" arm of the
error message instead of "already running", which would fail this test's
assertions *and* show a confusing message to a user.
@huichen
huichen enabled auto-merge (squash) September 25, 2026 17:00
@huichen
huichen merged commit d90a6f4 into main Sep 25, 2026
12 checks passed
@huichen
huichen deleted the fix/test-isolation-fixes branch September 25, 2026 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant