|
| 1 | +import { describe, expect, it, vi, type MockedFunction } from "vitest"; |
| 2 | +import { |
| 3 | + fetchLiveProviderModelIds, |
| 4 | + type LiveModelCatalogFetchGuard, |
| 5 | +} from "./provider-catalog-live-runtime.js"; |
| 6 | + |
| 7 | +describe("provider catalog malformed pagination", () => { |
| 8 | + it("reports incomplete pagination on malformed absolute next URL with no usable fallback", async () => { |
| 9 | + const release = vi.fn(async () => undefined); |
| 10 | + const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({ |
| 11 | + response: new Response( |
| 12 | + JSON.stringify({ |
| 13 | + data: [{ id: "model-a", object: "model" }], |
| 14 | + // Space in hostname makes this a genuinely invalid absolute URL. |
| 15 | + next: "http://exa mple.com/models?page=2", |
| 16 | + has_more: false, |
| 17 | + }), |
| 18 | + ), |
| 19 | + finalUrl: "https://provider.example.test/v1/models", |
| 20 | + release, |
| 21 | + })); |
| 22 | + |
| 23 | + // The provider explicitly advertised a next page via the `next` field but |
| 24 | + // the URL is malformed and there is no cursor fallback. The controlled |
| 25 | + // incomplete-pagination error prevents silently returning a truncated |
| 26 | + // catalog. |
| 27 | + await expect( |
| 28 | + fetchLiveProviderModelIds({ |
| 29 | + providerId: "provider", |
| 30 | + endpoint: "https://provider.example.test/v1/models", |
| 31 | + fetchGuard: fetchGuardMock, |
| 32 | + }), |
| 33 | + ).rejects.toThrow( |
| 34 | + "provider model discovery did not include a supported next page before the catalog completed", |
| 35 | + ); |
| 36 | + |
| 37 | + expect(fetchGuardMock).toHaveBeenCalledTimes(1); |
| 38 | + }); |
| 39 | + |
| 40 | + it("reports incomplete pagination on malformed nested links.next URL", async () => { |
| 41 | + const release = vi.fn(async () => undefined); |
| 42 | + const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({ |
| 43 | + response: new Response( |
| 44 | + JSON.stringify({ |
| 45 | + data: [{ id: "model-a", object: "model" }], |
| 46 | + links: { next: "http://exa mple.com/models?page=2" }, |
| 47 | + has_more: false, |
| 48 | + }), |
| 49 | + ), |
| 50 | + finalUrl: "https://provider.example.test/v1/models", |
| 51 | + release, |
| 52 | + })); |
| 53 | + |
| 54 | + await expect( |
| 55 | + fetchLiveProviderModelIds({ |
| 56 | + providerId: "provider", |
| 57 | + endpoint: "https://provider.example.test/v1/models", |
| 58 | + fetchGuard: fetchGuardMock, |
| 59 | + }), |
| 60 | + ).rejects.toThrow( |
| 61 | + "provider model discovery did not include a supported next page before the catalog completed", |
| 62 | + ); |
| 63 | + |
| 64 | + expect(fetchGuardMock).toHaveBeenCalledTimes(1); |
| 65 | + }); |
| 66 | + |
| 67 | + it("recovers malformed next URL via cursor fallback", async () => { |
| 68 | + const release = vi.fn(async () => undefined); |
| 69 | + const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi |
| 70 | + .fn() |
| 71 | + .mockResolvedValueOnce({ |
| 72 | + response: new Response( |
| 73 | + JSON.stringify({ |
| 74 | + data: [{ id: "model-a", object: "model" }], |
| 75 | + next: "http://exa mple.com/models?page=2", |
| 76 | + next_cursor: "cursor-2", |
| 77 | + has_more: true, |
| 78 | + }), |
| 79 | + ), |
| 80 | + finalUrl: "https://provider.example.test/v1/models", |
| 81 | + release, |
| 82 | + }) |
| 83 | + .mockResolvedValueOnce({ |
| 84 | + response: new Response( |
| 85 | + JSON.stringify({ data: [{ id: "model-b", object: "model" }], has_more: false }), |
| 86 | + ), |
| 87 | + finalUrl: "https://provider.example.test/v1/models?after=cursor-2", |
| 88 | + release, |
| 89 | + }); |
| 90 | + |
| 91 | + // The malformed next URL is ignored; cursor-based pagination takes over. |
| 92 | + await expect( |
| 93 | + fetchLiveProviderModelIds({ |
| 94 | + providerId: "provider", |
| 95 | + endpoint: "https://provider.example.test/v1/models", |
| 96 | + fetchGuard: fetchGuardMock, |
| 97 | + }), |
| 98 | + ).resolves.toEqual(["model-a", "model-b"]); |
| 99 | + |
| 100 | + expect(fetchGuardMock).toHaveBeenCalledTimes(2); |
| 101 | + expect(fetchGuardMock.mock.calls[1]?.[0].url).toBe( |
| 102 | + "https://provider.example.test/v1/models?after=cursor-2", |
| 103 | + ); |
| 104 | + }); |
| 105 | + |
| 106 | + it("sets safe replay headers when final URL is unparseable", async () => { |
| 107 | + const release = vi.fn(async () => undefined); |
| 108 | + const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({ |
| 109 | + response: new Response( |
| 110 | + JSON.stringify({ |
| 111 | + data: [{ id: "model-a", object: "model" }], |
| 112 | + }), |
| 113 | + ), |
| 114 | + // An unparseable finalUrl should trigger safe replay headers (conservative |
| 115 | + // cross-origin assumption), not crash. |
| 116 | + finalUrl: "http://exa mple.com/models", |
| 117 | + release, |
| 118 | + })); |
| 119 | + |
| 120 | + await expect( |
| 121 | + fetchLiveProviderModelIds({ |
| 122 | + providerId: "provider", |
| 123 | + endpoint: "https://provider.example.test/v1/models", |
| 124 | + fetchGuard: fetchGuardMock, |
| 125 | + }), |
| 126 | + ).resolves.toEqual(["model-a"]); |
| 127 | + |
| 128 | + expect(fetchGuardMock).toHaveBeenCalledTimes(1); |
| 129 | + }); |
| 130 | + |
| 131 | + it("reports incomplete pagination instead of crashing on malformed next URL with has_more", async () => { |
| 132 | + const release = vi.fn(async () => undefined); |
| 133 | + const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({ |
| 134 | + response: new Response( |
| 135 | + JSON.stringify({ |
| 136 | + data: [{ id: "model-a", object: "model" }], |
| 137 | + next: "http://exa mple.com/models?page=2", |
| 138 | + has_more: true, |
| 139 | + }), |
| 140 | + ), |
| 141 | + finalUrl: "https://provider.example.test/v1/models", |
| 142 | + release, |
| 143 | + })); |
| 144 | + |
| 145 | + await expect( |
| 146 | + fetchLiveProviderModelIds({ |
| 147 | + providerId: "provider", |
| 148 | + endpoint: "https://provider.example.test/v1/models", |
| 149 | + fetchGuard: fetchGuardMock, |
| 150 | + }), |
| 151 | + ).rejects.toThrow( |
| 152 | + "provider model discovery did not include a supported next page before the catalog completed", |
| 153 | + ); |
| 154 | + |
| 155 | + expect(fetchGuardMock).toHaveBeenCalledTimes(1); |
| 156 | + }); |
| 157 | +}); |
0 commit comments