|
2 | 2 |
|
3 | 3 | import { generateKeyPair } from '@libp2p/crypto/keys' |
4 | 4 | import { start, stop } from '@libp2p/interface' |
| 5 | +import { defaultLogger } from '@libp2p/logger' |
5 | 6 | import { peerIdFromPrivateKey, peerIdFromString, peerIdFromCID } from '@libp2p/peer-id' |
6 | 7 | import { multiaddr } from '@multiformats/multiaddr' |
7 | 8 | import { expect } from 'aegir/chai' |
8 | 9 | import { createIPNSRecord, marshalIPNSRecord } from 'ipns' |
9 | 10 | import all from 'it-all' |
10 | 11 | import { CID } from 'multiformats/cid' |
11 | | -import { createDelegatedRoutingV1HttpApiClient } from '../src/index.js' |
| 12 | +import { isBrowser } from 'wherearewe' |
| 13 | +import { delegatedRoutingV1HttpApiClient } from '../src/index.js' |
12 | 14 | import { itBrowser } from './fixtures/it.js' |
13 | 15 | import type { DelegatedRoutingV1HttpApiClient } from '../src/index.js' |
14 | 16 |
|
@@ -37,14 +39,27 @@ const serverUrl = process.env.ECHO_SERVER |
37 | 39 |
|
38 | 40 | describe('delegated-routing-v1-http-api-client', () => { |
39 | 41 | let client: DelegatedRoutingV1HttpApiClient |
| 42 | + let clientWithCache: DelegatedRoutingV1HttpApiClient |
40 | 43 |
|
41 | 44 | beforeEach(async () => { |
42 | | - client = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl), { cacheTTL: 0 }) |
43 | | - await start(client) |
| 45 | + client = delegatedRoutingV1HttpApiClient({ |
| 46 | + url: new URL(serverUrl), |
| 47 | + cacheTTL: 0 |
| 48 | + })({ |
| 49 | + logger: defaultLogger() |
| 50 | + }) |
| 51 | + clientWithCache = delegatedRoutingV1HttpApiClient({ |
| 52 | + url: new URL(serverUrl), |
| 53 | + cacheTTL: 30_000 |
| 54 | + })({ |
| 55 | + logger: defaultLogger() |
| 56 | + }) |
| 57 | + |
| 58 | + await start(client, clientWithCache) |
44 | 59 | }) |
45 | 60 |
|
46 | 61 | afterEach(async () => { |
47 | | - await stop(client) |
| 62 | + await stop(client, clientWithCache) |
48 | 63 | }) |
49 | 64 |
|
50 | 65 | it('should find providers', async () => { |
@@ -108,15 +123,40 @@ describe('delegated-routing-v1-http-api-client', () => { |
108 | 123 | await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, { |
109 | 124 | method: 'POST', |
110 | 125 | headers: { |
111 | | - Accept: 'application/x-ndjson' |
| 126 | + 'Content-Type': 'application/json' |
112 | 127 | }, |
113 | | - body: '' // Empty NDJSON stream |
| 128 | + body: JSON.stringify({ Providers: [] }) |
114 | 129 | }) |
115 | 130 |
|
116 | 131 | const provs = await all(client.getProviders(cid)) |
117 | 132 | expect(provs).to.be.empty() |
118 | 133 | }) |
119 | 134 |
|
| 135 | + it('should return empty array when no providers found in cached response (200 with empty NDJSON)', async function () { |
| 136 | + if (!isBrowser) { |
| 137 | + // 'globalThis.caches are only availale in browser environments' |
| 138 | + this.skip() |
| 139 | + } |
| 140 | + |
| 141 | + const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') |
| 142 | + |
| 143 | + // Clear any providers - send empty NDJSON |
| 144 | + await fetch(`${process.env.ECHO_SERVER}/add-providers/${cid.toString()}`, { |
| 145 | + method: 'POST', |
| 146 | + headers: { |
| 147 | + 'Content-Type': 'application/json' |
| 148 | + }, |
| 149 | + body: JSON.stringify({ Providers: [] }) |
| 150 | + }) |
| 151 | + |
| 152 | + const provs = await all(clientWithCache.getProviders(cid)) |
| 153 | + expect(provs).to.be.empty() |
| 154 | + |
| 155 | + // invoke again immediately to get cached response |
| 156 | + const cachedProvs = await all(clientWithCache.getProviders(cid)) |
| 157 | + expect(cachedProvs).to.be.empty() |
| 158 | + }) |
| 159 | + |
120 | 160 | it('should return empty array when server returns 404 for providers (old server behavior)', async () => { |
121 | 161 | // Test backward compatibility with old servers that return 404 |
122 | 162 | const cid = CID.parse(TEST_CIDS.PROVIDERS_404) |
@@ -223,9 +263,12 @@ describe('delegated-routing-v1-http-api-client', () => { |
223 | 263 | }) |
224 | 264 |
|
225 | 265 | it('should add filter parameters the query of the request url based on global filter', async () => { |
226 | | - const client = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl), { |
| 266 | + const client = delegatedRoutingV1HttpApiClient({ |
| 267 | + url: new URL(serverUrl), |
227 | 268 | filterProtocols: ['transport-bitswap', 'unknown'], |
228 | 269 | filterAddrs: ['tcp', '!p2p-circuit'] |
| 270 | + })({ |
| 271 | + logger: defaultLogger() |
229 | 272 | }) |
230 | 273 | const cid = CID.parse('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') |
231 | 274 |
|
@@ -533,8 +576,11 @@ describe('delegated-routing-v1-http-api-client', () => { |
533 | 576 |
|
534 | 577 | itBrowser('should respect cache TTL', async () => { |
535 | 578 | const shortTTL = 100 // 100ms TTL for testing |
536 | | - const clientWithShortTTL = createDelegatedRoutingV1HttpApiClient(new URL(serverUrl), { |
| 579 | + const clientWithShortTTL = delegatedRoutingV1HttpApiClient({ |
| 580 | + url: new URL(serverUrl), |
537 | 581 | cacheTTL: shortTTL |
| 582 | + })({ |
| 583 | + logger: defaultLogger() |
538 | 584 | }) |
539 | 585 | await start(clientWithShortTTL) |
540 | 586 |
|
|
0 commit comments