TestSnapshotFallbackIsNotMemoized guards against pinning the embedded fallback for the Store's lifetime: a transient fetch failure must degrade to the snapshot, but a later lookup (once the network recovers) must retry the fetch and serve the fresh catalog rather than the stale build-time snapshot.
(t *testing.T)
| 89 | // the snapshot, but a later lookup (once the network recovers) must retry the |
| 90 | // fetch and serve the fresh catalog rather than the stale build-time snapshot. |
| 91 | func TestSnapshotFallbackIsNotMemoized(t *testing.T) { |
| 92 | t.Parallel() |
| 93 | |
| 94 | cacheFile := filepath.Join(t.TempDir(), "models_dev.json") |
| 95 | |
| 96 | // First lookup: the network is down, so the fetch fails and we fall back |
| 97 | // to the embedded snapshot without memoizing it. |
| 98 | var firstFetched, secondFetched bool |
| 99 | fetch := func(context.Context, string) (*Database, string, error) { |
| 100 | if !firstFetched { |
| 101 | firstFetched = true |
| 102 | return nil, "", errors.New("fetch from API: network unreachable") |
| 103 | } |
| 104 | secondFetched = true |
| 105 | return &Database{Providers: map[string]Provider{ |
| 106 | "openai": {Models: map[string]Model{ |
| 107 | "fresh-model": {Name: "Freshly Fetched", Limit: Limit{Context: 12345}}, |
| 108 | }}, |
| 109 | }}, "etag-1", nil |
| 110 | } |
| 111 | store, err := NewStore(WithCache(cacheFile), WithFetcher(fetch)) |
| 112 | require.NoError(t, err) |
| 113 | |
| 114 | _, err = store.GetModel(t.Context(), NewID("openai", "gpt-4o")) |
| 115 | require.NoError(t, err) |
| 116 | assert.True(t, firstFetched) |
| 117 | |
| 118 | // Network recovers: a second lookup must retry the fetch (the fallback was |
| 119 | // not pinned) and serve the freshly fetched catalog. |
| 120 | m, err := store.GetModel(t.Context(), NewID("openai", "fresh-model")) |
| 121 | require.NoError(t, err) |
| 122 | assert.True(t, secondFetched, "a fetch failure must not be memoized; the next lookup must retry") |
| 123 | assert.Equal(t, 12345, m.Limit.Context, "the recovered fetch must serve fresh data, not the snapshot") |
| 124 | } |