Skip to content

Commit bca89fa

Browse files
committed
code review comments
1 parent d686913 commit bca89fa

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

shared/utils/metrics-util.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const cache = new Map<string, CacheData>();
1212
interface CacheData {
1313
data: CopilotMetrics[];
1414
valid_until: number;
15-
auth_fingerprint: string; // hashed representation of Authorization header used to populate cache
1615
}
1716

1817
class MetricsError extends Error {
@@ -112,7 +111,7 @@ export async function getMetricsData(event: H3Event<EventHandlerRequest>): Promi
112111
// Attempt cache lookup with auth fingerprint validation
113112
const cachedData = cache.get(cacheKey);
114113
if (cachedData) {
115-
if (cachedData.valid_until > Date.now() / 1000 && cachedData.auth_fingerprint === cacheKey.split(':')[0]) {
114+
if (cachedData.valid_until > Date.now() / 1000) {
116115
logger.info(`Returning cached data for ${cacheKey}`);
117116
return cachedData.data;
118117
} else {
@@ -134,8 +133,7 @@ export async function getMetricsData(event: H3Event<EventHandlerRequest>): Promi
134133
const filteredUsageData = filterHolidaysFromMetrics(usageData, options.excludeHolidays || false, options.locale);
135134
// metrics is the old API format
136135
const validUntil = Math.floor(Date.now() / 1000) + 5 * 60; // Cache for 5 minutes
137-
const authFingerprint: string = cacheKey.split(':', 1)[0] || '';
138-
cache.set(cacheKey, { data: filteredUsageData, valid_until: validUntil, auth_fingerprint: authFingerprint });
136+
cache.set(cacheKey, { data: filteredUsageData, valid_until: validUntil });
139137
return filteredUsageData;
140138
} catch (error: unknown) {
141139
logger.error('Error fetching metrics data:', error);

tests/test.setup.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Global test setup
2+
// Force mock mode so server-side handlers use local mock data instead of hitting GitHub APIs.
3+
process.env.NUXT_PUBLIC_IS_DATA_MOCKED = 'true';
4+
5+
// Block accidental real GitHub API calls if mock mode logic fails.
6+
const originalFetch: typeof globalThis.fetch | undefined = globalThis.fetch;
7+
if (originalFetch) {
8+
globalThis.fetch = (async (...args: Parameters<typeof fetch>): Promise<Response> => {
9+
const url = String(args[0]);
10+
if (url.startsWith('https://api.github.com')) {
11+
throw new Error(`Blocked external GitHub API call during tests: ${url}`);
12+
}
13+
return originalFetch(...args);
14+
}) as typeof fetch;
15+
}
16+
17+
// Stub $fetch (ofetch) similarly if present at runtime.
18+
if (typeof globalThis.$fetch === 'function') {
19+
const original$fetch = globalThis.$fetch;
20+
const wrapped: typeof globalThis.$fetch = ((url: unknown, opts: unknown) => {
21+
const str = String(url);
22+
if (str.startsWith('https://api.github.com')) {
23+
return Promise.reject(new Error(`Blocked external GitHub API call during tests via $fetch: ${str}`));
24+
}
25+
return original$fetch(url as never, opts as never);
26+
}) as typeof globalThis.$fetch;
27+
// Preserve special properties if present
28+
(wrapped as unknown as { raw?: unknown }).raw = (original$fetch as unknown as { raw?: () => unknown }).raw?.bind(original$fetch);
29+
(wrapped as unknown as { create?: unknown }).create = (original$fetch as unknown as { create?: () => unknown }).create?.bind(original$fetch);
30+
globalThis.$fetch = wrapped;
31+
}

vitest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default defineVitestConfig({
55
test: {
66
exclude: ['**/node_modules/**', '**/e2e-tests/**'],
77
environment: 'nuxt',
8-
globals: true // Use describe, test/expect, etc. without importing
8+
globals: true, // Use describe, test/expect, etc. without importing
9+
setupFiles: ['./tests/test.setup.ts']
910
}
1011
})

0 commit comments

Comments
 (0)