Skip to content
Merged
2 changes: 0 additions & 2 deletions tests/api-mocking/mock-e2e-allowlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const ALLOWLISTED_HOSTS = [
'127.0.0.1',
'localhost',
'10.0.2.2', // Android emulator host
'gamma-api.polymarket.com',
'*.polymarket.com',
'metamask.github.io', // Test-snaps and test-dapp pages loaded in browser
];

Expand Down
69 changes: 67 additions & 2 deletions tests/api-mocking/mock-responses/defaults/polymarket-apis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
/**
* Default mock responses for Polymarket API endpoints
* Default mock responses for Polymarket API endpoints.
*
* These defaults cover the calls fired by the wallet's Explore tab
* (TrendingView / usePredictMarketData) so that non-predict specs that pass
* through Explore (e.g. via navigateToBrowserView()) don't leak live requests
* to Polymarket. Predict and trending specs override these via
* POLYMARKET_COMPLETE_MOCKS registered as testSpecificMock at higher priority.
*
* clob.polymarket.com is intentionally not covered here — it only fires on
* trade actions, never during Explore render.
*/

/** Single source of truth for geoblock mock (eligible region). Reused in defaults and in POLYMARKET_COMPLETE_MOCKS. */
Expand All @@ -13,7 +22,63 @@ export const POLYMARKET_GEOBLOCK_ELIGIBLE = {
} as const;

export const POLYMARKET_API_MOCKS = {
GET: [POLYMARKET_GEOBLOCK_ELIGIBLE],
GET: [
POLYMARKET_GEOBLOCK_ELIGIBLE,
// gamma-api: events pagination — consumer reads `data?.data`
{
urlEndpoint:
/^https:\/\/gamma-api\.polymarket\.com\/events\/pagination(\?.*)?$/,
responseCode: 200,
response: { data: [] },
},
// gamma-api: public-search — consumer reads `data?.events`
{
urlEndpoint:
/^https:\/\/gamma-api\.polymarket\.com\/public-search(\?.*)?$/,
responseCode: 200,
response: { events: [] },
},
// gamma-api: markets list
{
urlEndpoint: /^https:\/\/gamma-api\.polymarket\.com\/markets(\?.*)?$/,
responseCode: 200,
response: [],
},
// gamma-api: sports league team metadata (TeamsCache)
{
urlEndpoint: /^https:\/\/gamma-api\.polymarket\.com\/teams(\?.*)?$/,
responseCode: 200,
response: [],
},
// polymarket.com: homepage carousel
{
urlEndpoint: 'https://polymarket.com/api/homepage/carousel',
responseCode: 200,
response: [],
},
// polymarket.com: crypto price feed
{
urlEndpoint: /^https:\/\/polymarket\.com\/api\/crypto\/crypto-price.*$/,
responseCode: 200,
response: {},
},
// data-api: positions / activity / upnl
{
urlEndpoint: /^https:\/\/data-api\.polymarket\.com\/positions(\?.*)?$/,
responseCode: 200,
response: [],
},
{
urlEndpoint: /^https:\/\/data-api\.polymarket\.com\/activity(\?.*)?$/,
responseCode: 200,
response: [],
},
{
urlEndpoint: /^https:\/\/data-api\.polymarket\.com\/upnl(\?.*)?$/,
responseCode: 200,
response: [],
},
],
POST: [],
PUT: [],
DELETE: [],
Expand Down
257 changes: 256 additions & 1 deletion tests/api-mocking/mock-responses/polymarket/polymarket-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,13 @@ export const POLYMARKET_EVENT_DETAILS_MOCKS = async (mockServer: Mockttp) => {
.forGet('/proxy')
.matching((request) => {
const url = new URL(request.url).searchParams.get('url');
return Boolean(url?.includes('gamma-api.polymarket.com/events/'));
// Match only /events/{numericId}, NOT /events/pagination or
// /events?parent_event_id=... — those have their own dedicated mocks.
return Boolean(
url && /gamma-api\.polymarket\.com\/events\/[0-9]+/.test(url),
);
})
.asPriority(PRIORITY.BASE)
Comment thread
cursor[bot] marked this conversation as resolved.
.thenCallback((request) => {
const url = new URL(request.url).searchParams.get('url');
const eventIdMatch = url?.match(/\/events\/([0-9]+)$/);
Expand Down Expand Up @@ -656,6 +661,254 @@ export const POLYMARKET_FEE_RATE_MOCKS = async (mockServer: Mockttp) => {
});
};

/**
* Mock for Polymarket CLOB prices-history API
* Returns an empty history series — sufficient for predict happy-path specs
* that render the chart (consumer treats non-array history as empty).
*/
export const POLYMARKET_PRICES_HISTORY_MOCKS = async (mockServer: Mockttp) => {
await mockServer
.forGet('/proxy')
.matching((request) => {
const url = new URL(request.url).searchParams.get('url');
return Boolean(url?.includes('clob.polymarket.com/prices-history'));
})
.asPriority(PRIORITY.BASE)
.thenReply(200, JSON.stringify({ history: [] }), {
'content-type': 'application/json',
});
};

/**
* Static team metadata for sports leagues used by predict E2E fixtures.
* Covers NBA + NFL teams referenced in event slugs (see polymarket-positions-response.ts).
*
* The wallet's TeamsCache stores teams by abbreviation, and buildGameData
* (gameParser.ts) returns null when either home or away team is missing —
* which causes market.game to be undefined and the wallet to fall back to
* the non-game market detail layout (no picks list). So returning realistic
* team data here is required for the new PredictGameDetailsContent path.
*/
const POLYMARKET_TEAMS_BY_LEAGUE: Record<
string,
{
id: string;
name: string;
logo: string;
abbreviation: string;
color: string;
alias: string;
league: string;
}[]
> = {
nba: [
{
id: 'nba-sas',
name: 'San Antonio Spurs',
logo: '',
abbreviation: 'sas',
color: '#000000',
alias: 'Spurs',
league: 'nba',
},
{
id: 'nba-nop',
name: 'New Orleans Pelicans',
logo: '',
abbreviation: 'nop',
color: '#0C2340',
alias: 'Pelicans',
league: 'nba',
},
{
id: 'nba-bos',
name: 'Boston Celtics',
logo: '',
abbreviation: 'bos',
color: '#007A33',
alias: 'Celtics',
league: 'nba',
},
{
id: 'nba-bkn',
name: 'Brooklyn Nets',
logo: '',
abbreviation: 'bkn',
color: '#000000',
alias: 'Nets',
league: 'nba',
},
],
nfl: [
{
id: 'nfl-buf',
name: 'Buffalo Bills',
logo: '',
abbreviation: 'buf',
color: '#00338D',
alias: 'Bills',
league: 'nfl',
},
{
id: 'nfl-atl',
name: 'Atlanta Falcons',
logo: '',
abbreviation: 'atl',
color: '#A71930',
alias: 'Falcons',
league: 'nfl',
},
{
id: 'nfl-chi',
name: 'Chicago Bears',
logo: '',
abbreviation: 'chi',
color: '#0B162A',
alias: 'Bears',
league: 'nfl',
},
{
id: 'nfl-was',
name: 'Washington Commanders',
logo: '',
abbreviation: 'was',
color: '#5A1414',
alias: 'Commanders',
league: 'nfl',
},
{
id: 'nfl-den',
name: 'Denver Broncos',
logo: '',
abbreviation: 'den',
color: '#FB4F14',
alias: 'Broncos',
league: 'nfl',
},
{
id: 'nfl-nyj',
name: 'New York Jets',
logo: '',
abbreviation: 'nyj',
color: '#125740',
alias: 'Jets',
league: 'nfl',
},
{
id: 'nfl-det',
name: 'Detroit Lions',
logo: '',
abbreviation: 'det',
color: '#0076B6',
alias: 'Lions',
league: 'nfl',
},
{
id: 'nfl-kc',
name: 'Kansas City Chiefs',
logo: '',
abbreviation: 'kc',
color: '#E31837',
alias: 'Chiefs',
league: 'nfl',
},
{
id: 'nfl-jax',
name: 'Jacksonville Jaguars',
logo: '',
abbreviation: 'jax',
color: '#006778',
alias: 'Jaguars',
league: 'nfl',
},
{
id: 'nfl-pit',
name: 'Pittsburgh Steelers',
logo: '',
abbreviation: 'pit',
color: '#FFB612',
alias: 'Steelers',
league: 'nfl',
},
{
id: 'nfl-cin',
name: 'Cincinnati Bengals',
logo: '',
abbreviation: 'cin',
color: '#FB4F14',
alias: 'Bengals',
league: 'nfl',
},
{
id: 'nfl-sea',
name: 'Seattle Seahawks',
logo: '',
abbreviation: 'sea',
color: '#002244',
alias: 'Seahawks',
league: 'nfl',
},
{
id: 'nfl-sf',
name: 'San Francisco 49ers',
logo: '',
abbreviation: 'sf',
color: '#AA0000',
alias: '49ers',
league: 'nfl',
},
{
id: 'nfl-tb',
name: 'Tampa Bay Buccaneers',
logo: '',
abbreviation: 'tb',
color: '#D50A0A',
alias: 'Buccaneers',
league: 'nfl',
},
{
id: 'nfl-dal',
name: 'Dallas Cowboys',
logo: '',
abbreviation: 'dal',
color: '#003594',
alias: 'Cowboys',
league: 'nfl',
},
],
};

/**
* Mock for Polymarket gamma-api /teams API
* Returns the full list of teams for the queried league. The wallet's
* TeamsCache filters by requested abbreviations, so returning all teams
* for the league is safe and avoids per-test fixture maintenance.
*/
export const POLYMARKET_TEAMS_MOCKS = async (mockServer: Mockttp) => {
await mockServer
.forGet('/proxy')
.matching((request) => {
const url = new URL(request.url).searchParams.get('url');
return Boolean(url?.includes('gamma-api.polymarket.com/teams'));
})
.asPriority(PRIORITY.BASE)
.thenCallback((request) => {
const proxiedUrlParam = new URL(request.url).searchParams.get('url');
let league = '';
try {
const polymarketUrl = new URL(proxiedUrlParam ?? '');
league = polymarketUrl.searchParams.get('league') ?? '';
} catch {
league = '';
}
const teams = POLYMARKET_TEAMS_BY_LEAGUE[league] ?? [];
return {
statusCode: 200,
json: teams,
};
});
};

/**
* Mock for Polymarket CLOB order book API
* Returns order book data for specific token IDs with correct market mapping
Expand Down Expand Up @@ -2214,6 +2467,8 @@ export const POLYMARKET_COMPLETE_MOCKS = async (mockServer: Mockttp) => {
await POLYMARKET_EVENT_DETAILS_MOCKS(mockServer);
await POLYMARKET_ORDER_BOOK_MOCKS(mockServer);
await POLYMARKET_PRICES_MOCKS(mockServer); // Mock for CLOB prices API
await POLYMARKET_PRICES_HISTORY_MOCKS(mockServer); // Mock for CLOB prices-history API (chart series)
await POLYMARKET_TEAMS_MOCKS(mockServer); // Mock for gamma-api /teams (sports league team metadata)
await POLYMARKET_FEE_RATE_MOCKS(mockServer);
await POLYMARKET_MARKET_FEEDS_MOCKS(mockServer);
await POLYMARKET_CLOB_AUTH_MOCKS(mockServer);
Expand Down
Loading
Loading