Skip to content

Commit fbf5bca

Browse files
test(mobile): permission and update-check tests prove what they claim
The stale-read case in permissionStatusAtom passed with or without the `active` guard. The guard was redundant: effect's Lifetime.setSelf is a no-op once the lifetime is disposed. Probing a stale read resolved after unmount against a remounted, newer value passed with the guard deleted, so no test can tell the difference. Drop the guard and the claim. The release-check tests waited for fetch with a fixed 10-microtask yield loop; the fetch stub now resolves a promise when called and the tests await that. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b66655f commit fbf5bca

3 files changed

Lines changed: 19 additions & 37 deletions

File tree

‎apps/mobile/src/features/settings/environmentUpdateCheck.test.ts‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ import { describe, expect, it, vi } from "vite-plus/test";
55
import { environmentReleaseCheckAtom, releaseCheckError } from "./environmentUpdateCheck";
66

77
function stubHangingFetch() {
8-
const signals: AbortSignal[] = [];
9-
vi.stubGlobal(
10-
"fetch",
11-
(_url: string, init: { signal: AbortSignal }) =>
12-
new Promise((_resolve, reject) => {
13-
signals.push(init.signal);
14-
init.signal.addEventListener("abort", () => reject(init.signal.reason));
15-
}),
16-
);
17-
return Object.assign(signals, { [Symbol.dispose]: () => vi.unstubAllGlobals() });
18-
}
19-
20-
async function settleMicrotasks() {
21-
for (let index = 0; index < 10; index++) await Promise.resolve();
8+
const { promise: called, resolve } = Promise.withResolvers<AbortSignal>();
9+
vi.stubGlobal("fetch", (_url: string, init: { signal: AbortSignal }) => {
10+
resolve(init.signal);
11+
return new Promise((_resolve, reject) => {
12+
init.signal.addEventListener("abort", () => reject(init.signal.reason));
13+
});
14+
});
15+
return { called, [Symbol.dispose]: () => vi.unstubAllGlobals() };
2216
}
2317

2418
function makeRegistry() {
@@ -37,35 +31,33 @@ function makeRegistry() {
3731

3832
describe("environmentReleaseCheckAtom", () => {
3933
it("aborts the release lookup when the screen stops reading it", async () => {
40-
using signals = stubHangingFetch();
34+
using fetchStub = stubHangingFetch();
4135
const { registry, flush } = makeRegistry();
4236
const atom = environmentReleaseCheckAtom(EnvironmentId.make("env-unmount"));
4337
const unmount = registry.mount(atom);
4438

4539
registry.set(atom, async () => "0.0.40");
46-
await settleMicrotasks();
47-
expect(signals).toHaveLength(1);
48-
expect(signals[0]?.aborted).toBe(false);
40+
const signal = await fetchStub.called;
41+
expect(signal.aborted).toBe(false);
4942

5043
unmount();
5144
flush();
52-
expect(signals[0]?.aborted).toBe(true);
45+
expect(signal.aborted).toBe(true);
5346
});
5447

5548
it("reports a timeout in words after 20 seconds", async () => {
56-
using signals = stubHangingFetch();
49+
using fetchStub = stubHangingFetch();
5750
using _timers = { [Symbol.dispose]: () => vi.useRealTimers() };
5851
vi.useFakeTimers();
5952
const { registry, flush } = makeRegistry();
6053
const atom = environmentReleaseCheckAtom(EnvironmentId.make("env-timeout"));
6154
const unmount = registry.mount(atom);
6255

6356
registry.set(atom, async () => "0.0.40");
64-
await settleMicrotasks();
65-
expect(signals).toHaveLength(1);
57+
const signal = await fetchStub.called;
6658

6759
await vi.advanceTimersByTimeAsync(20_000);
68-
expect(signals[0]?.aborted).toBe(true);
60+
expect(signal.aborted).toBe(true);
6961
expect(releaseCheckError(registry.get(atom))).toBe(
7062
"Checking for updates timed out. Try again.",
7163
);

‎apps/mobile/src/lib/permissionStatusAtom.test.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe("makePermissionStatusAtom", () => {
6868
expect(native.listeners.size).toBe(0);
6969
});
7070

71-
it("stores a request's outcome and ignores a read that settles after unmount", async () => {
71+
it("stores a request's outcome", () => {
7272
const reads = deferredReads();
7373
const atom = makePermissionStatusAtom({ label: "test", initial: "checking", read: reads.read });
7474
const { registry, flush } = makeRegistry();
@@ -79,9 +79,5 @@ describe("makePermissionStatusAtom", () => {
7979

8080
unmount();
8181
flush();
82-
await reads.resolveNext("granted");
83-
const remount = registry.mount(atom);
84-
expect(registry.get(atom)).toBe("checking");
85-
remount();
8682
});
8783
});

‎apps/mobile/src/lib/permissionStatusAtom.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@ export function makePermissionStatusAtom<S>(options: {
1414
}): Atom.Writable<S, S> {
1515
return Atom.writable(
1616
(get) => {
17-
let active = true;
17+
// A read that settles after unmount is dropped: setSelf on a disposed lifetime is a no-op.
1818
const refresh = () => {
19-
void options.read().then((status) => {
20-
if (active) get.setSelf(status);
21-
});
19+
void options.read().then((status) => get.setSelf(status));
2220
};
2321
refresh();
24-
const unsubscribe = subscribeAppActive(refresh);
25-
get.addFinalizer(() => {
26-
active = false;
27-
unsubscribe();
28-
});
22+
get.addFinalizer(subscribeAppActive(refresh));
2923
return options.initial;
3024
},
3125
(ctx, status: S) => ctx.setSelf(status),

0 commit comments

Comments
 (0)