diff --git a/neode-ui/src/composables/__tests__/useCachedResource.test.ts b/neode-ui/src/composables/__tests__/useCachedResource.test.ts index bd105337..7b357000 100644 --- a/neode-ui/src/composables/__tests__/useCachedResource.test.ts +++ b/neode-ui/src/composables/__tests__/useCachedResource.test.ts @@ -127,4 +127,79 @@ describe('useCachedResource', () => { expect(resource!.data.value).toBe('v1') // keep-last-known-value expect(resource!.error.value).toBe('offline') }) + + // 02-04: found while auditing Cloud.vue/Server.vue's lazy (`immediate: + // false`) resources ahead of adding their routes to KEEP_ALIVE_PATHS. + // Without this guard, onActivated's refreshIfStale() would treat a + // never-fetched entry as stale and eagerly fire the "fetch on first use" + // resource the moment the tab is first activated, even though the caller + // never explicitly requested it (e.g. a tab-gated Paid Files fetch that + // should wait until that sub-tab is opened). + it('does not eagerly fetch an immediate:false resource on activation before it has been explicitly requested, but does revalidate it once it has', async () => { + vi.useFakeTimers() + vi.setSystemTime(new Date(2030, 0, 1, 0, 0, 0)) + const fetcher = vi.fn().mockResolvedValue('lazy-v1') + let resource: ReturnType> | null = null + + const Consumer = defineComponent({ + setup() { + resource = useCachedResource({ + key: 'test.lazy-key', + fetcher, + ttlMs: 1000, + persist: false, + immediate: false, + }) + return () => h('div', resource!.data.value ?? '') + }, + }) + const Other = defineComponent({ render: () => h('div', 'other') }) + + const Host = defineComponent({ + setup() { + const show = ref(true) + return { show } + }, + render() { + return h(KeepAlive, null, () => + this.show ? h(Consumer, { key: 'consumer' }) : h(Other, { key: 'other' }), + ) + }, + }) + + const wrapper = mount(Host) + await flushPromises() + expect(fetcher).not.toHaveBeenCalled() // immediate: false — not fetched on mount + + // Deactivate then reactivate — still never explicitly requested, so + // activation must not be the thing that fetches it. + ;(wrapper.vm as unknown as { show: boolean }).show = false + await wrapper.vm.$nextTick() + ;(wrapper.vm as unknown as { show: boolean }).show = true + await wrapper.vm.$nextTick() + await flushPromises() + expect(fetcher).not.toHaveBeenCalled() + + // The caller explicitly requests it now (e.g. the user opened the tab). + await resource!.refresh() + expect(fetcher).toHaveBeenCalledTimes(1) + + // Deactivate within the TTL, reactivate — no additional fetch. + ;(wrapper.vm as unknown as { show: boolean }).show = false + await wrapper.vm.$nextTick() + ;(wrapper.vm as unknown as { show: boolean }).show = true + await wrapper.vm.$nextTick() + await flushPromises() + expect(fetcher).toHaveBeenCalledTimes(1) + + // Deactivate, advance past the TTL, reactivate — now it revalidates, + // because it has been fetched before. + ;(wrapper.vm as unknown as { show: boolean }).show = false + await wrapper.vm.$nextTick() + vi.setSystemTime(new Date(2030, 0, 1, 0, 0, 2)) // +2s, past the 1s TTL + ;(wrapper.vm as unknown as { show: boolean }).show = true + await wrapper.vm.$nextTick() + await flushPromises() + expect(fetcher).toHaveBeenCalledTimes(2) + }) }) diff --git a/neode-ui/src/composables/useCachedResource.ts b/neode-ui/src/composables/useCachedResource.ts index c3080e41..a093e90b 100644 --- a/neode-ui/src/composables/useCachedResource.ts +++ b/neode-ui/src/composables/useCachedResource.ts @@ -97,7 +97,22 @@ export function useCachedResource(opts: CachedResourceOptions): CachedReso // loses focus). Without this a kept-alive tab would paint instantly // forever and never revalidate. Vue no-ops onActivated outside a // boundary, so this is safe for every existing consumer. - onActivated(() => refreshIfStale()) + // + // `immediate: false` resources are a distinct case (found in 02-04's + // audit, once Cloud.vue/Server.vue's main-tab paths joined + // KEEP_ALIVE_PATHS): `stale()` is true for any never-fetched entry + // (`fetchedAt === null`), so a bare `refreshIfStale()` here would fire + // the fetch the moment the tab is first activated — defeating a resource + // deliberately marked "fetch on first use" (e.g. a tab-gated fetch that + // should wait for the user to open that sub-tab). Only auto-revalidate + // an `immediate: false` resource on activation once it has actually been + // fetched at least once; before that, activation is a no-op and the + // resource's own explicit trigger (a watcher, an onMounted/onActivated + // "kick") still owns the first fetch. + onActivated(() => { + if (opts.immediate === false && entry.fetchedAt === null) return + refreshIfStale() + }) } if (opts.immediate ?? true) refreshIfStale() diff --git a/neode-ui/src/views/Chat.vue b/neode-ui/src/views/Chat.vue index 0d110d0f..d59edcdd 100644 --- a/neode-ui/src/views/Chat.vue +++ b/neode-ui/src/views/Chat.vue @@ -58,7 +58,7 @@