Skip to content

Commit 0528747

Browse files
committed
fix: tests failing / errors
1 parent 3930fc5 commit 0528747

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

apps/basket/src/index.test.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,51 @@ mock.module('autumn-js', () => ({
5656

5757
mock.module('./routes/basket', () => ({
5858
default: {
59-
fetch: mock(() => Promise.resolve(new Response(JSON.stringify({ status: 'success' }), { status: 200 }))),
59+
fetch: mock((request: Request) => {
60+
const url = new URL(request.url);
61+
const isBatch = url.pathname.includes('/batch');
62+
63+
if (isBatch) {
64+
return request.json().then((body) => {
65+
const eventCount = Array.isArray(body) ? body.length : 1;
66+
const results = Array(eventCount).fill(null).map((_, index) => {
67+
const event = Array.isArray(body) ? body[index] : body;
68+
const eventType = event?.type || 'track';
69+
return { status: 'success', type: eventType };
70+
});
71+
72+
return Promise.resolve(new Response(JSON.stringify({
73+
status: 'success',
74+
batch: true,
75+
processed: eventCount,
76+
results
77+
}), { status: 200 }));
78+
}).catch(() => {
79+
// Fallback if JSON parsing fails
80+
return Promise.resolve(new Response(JSON.stringify({
81+
status: 'success',
82+
batch: true,
83+
processed: 1,
84+
results: [{ status: 'success', type: 'track' }]
85+
}), { status: 200 }));
86+
});
87+
}
88+
89+
// Parse the request body to determine the event type
90+
return request.json().then((body) => {
91+
const eventType = body.type || 'track';
92+
return Promise.resolve(new Response(JSON.stringify({
93+
status: 'success',
94+
type: eventType
95+
}), { status: 200 }));
96+
}).catch(() => {
97+
// Fallback if JSON parsing fails
98+
return Promise.resolve(new Response(JSON.stringify({
99+
status: 'success',
100+
type: 'track'
101+
}), { status: 200 }));
102+
});
103+
}),
60104
},
61105
}));
62106

apps/basket/src/lib/logger.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ import { Logtail } from '@logtail/edge';
22

33
const token = process.env.LOGTAIL_SOURCE_TOKEN as string;
44
const endpoint = process.env.LOGTAIL_ENDPOINT as string;
5-
export const logger = new Logtail(token || '', {
6-
endpoint: endpoint || '',
7-
batchSize: 10,
8-
batchInterval: 1000,
5+
6+
// Create a no-op logger for testing or when token is missing
7+
const createNoopLogger = () => ({
8+
info: () => {},
9+
warn: () => {},
10+
error: () => {},
11+
debug: () => {},
12+
log: () => {},
913
});
1014

15+
// Only initialize Logtail if we have a valid token
16+
export const logger = token && token.trim() !== ''
17+
? new Logtail(token, {
18+
endpoint: endpoint || '',
19+
batchSize: 10,
20+
batchInterval: 1000,
21+
})
22+
: createNoopLogger();
23+
1124
// Log levels to ensure we only log important events
1225
// export enum LogLevel {
1326
// ERROR = 'error',

0 commit comments

Comments
 (0)