Skip to content

Commit b1b0384

Browse files
committed
Improve type safety and test robustness
Refactored several tests and code to use more explicit type assertions and safer environment variable handling. Updated order creation in payment validator tests to use address IDs instead of connect objects. Improved audit log type safety and clarified export permissions for STAFF users in order export tests.
1 parent c2e25f9 commit b1b0384

10 files changed

Lines changed: 22 additions & 22 deletions

File tree

src/app/shop/orders/[id]/confirmation/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ export default async function OrderConfirmationPage({ params }: OrderConfirmatio
124124
{order.items.map((item) => (
125125
<Flex key={item.id} align="center" gap="4" style={{ paddingBottom: '16px', borderBottom: '1px solid var(--gray-6)' }}>
126126
<div className="w-16 h-16 bg-gray-200 rounded-md flex-shrink-0 relative">
127-
{item.product?.images?.[0] ? (
127+
{(item.product?.images as string[] | null)?.[0] ? (
128128
<Image
129-
src={(item.product.images as string[])[0]}
129+
src={(item.product!.images as string[])[0]}
130130
alt={item.productName}
131131
fill
132132
className="object-cover rounded-md"

src/lib/audit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ export async function getResourceAuditLogs(
348348
id: log.id,
349349
userId: log.userId,
350350
action: log.action,
351-
changes: log.changes,
351+
changes: log.changes as string | null,
352352
ipAddress: log.ipAddress,
353353
createdAt: log.createdAt,
354354
})),

tests/e2e/auth/login-errors.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ test.describe('Login Errors', () => {
8787
expect(auditLog?.action).toBe('LOGIN_FAILED');
8888

8989
// Parse changes JSON to verify it contains the expected data
90-
const changes = auditLog?.changes ? JSON.parse(auditLog.changes) : null;
90+
const changes = auditLog?.changes ? JSON.parse(auditLog.changes as string) : null;
9191
expect(changes).toMatchObject({
9292
email: user.email,
9393
reason: expect.stringMatching(/password|credentials/i),

tests/e2e/auth/login.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test.describe('User Login', () => {
7676
expect(auditLog?.action).toBe('LOGIN');
7777

7878
// Parse changes JSON to verify it contains the expected data
79-
const changes = auditLog?.changes ? JSON.parse(auditLog.changes) : null;
79+
const changes = auditLog?.changes ? JSON.parse(auditLog.changes as string) : null;
8080
expect(changes).toMatchObject({
8181
email: admin.email,
8282
role: 'SUPER_ADMIN',

tests/integration/auth/test/route.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('GET /api/auth/test', () => {
6666

6767
process.env.NEXTAUTH_SECRET = 'test-secret';
6868
process.env.NEXTAUTH_URL = 'http://localhost:3000';
69-
process.env.NODE_ENV = 'test';
69+
Object.defineProperty(process.env, 'NODE_ENV', { value: 'test', writable: true, configurable: true });
7070

7171
const response = await GET();
7272

@@ -81,7 +81,7 @@ describe('GET /api/auth/test', () => {
8181
// Restore original env vars
8282
process.env.NEXTAUTH_SECRET = originalSecret;
8383
process.env.NEXTAUTH_URL = originalUrl;
84-
process.env.NODE_ENV = originalNodeEnv;
84+
Object.defineProperty(process.env, 'NODE_ENV', { value: originalNodeEnv, writable: true, configurable: true });
8585
});
8686

8787
it('should detect missing environment variables', async () => {

tests/integration/orders/export/route.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('GET /api/orders/export', () => {
6464
const originalHandler = createApiHandler;
6565

6666
vi.mocked(createApiHandler).mockImplementation((_middlewares, handler) => {
67-
return async (request, context) => {
67+
return async (request, context: any) => {
6868
context = context || {};
6969
context.session = {
7070
user: { id: 'user-1', email: 'user@example.com', role: 'CUSTOMER' },
@@ -90,7 +90,7 @@ describe('GET /api/orders/export', () => {
9090
const originalHandler = createApiHandler;
9191

9292
vi.mocked(createApiHandler).mockImplementation((_middlewares, handler) => {
93-
return async (request, context) => {
93+
return async (request, context: any) => {
9494
context = context || {};
9595
context.session = {
9696
user: { id: 'admin-1', email: 'admin@example.com', role: 'SUPER_ADMIN' },
@@ -122,12 +122,12 @@ describe('GET /api/orders/export', () => {
122122
vi.mocked(createApiHandler).mockImplementation(originalHandler as any);
123123
});
124124

125-
it('should return 403 for non-super-admin without storeId', async () => {
125+
it('should allow STAFF to export store orders', async () => {
126126
const { createApiHandler } = await import('@/lib/api-middleware');
127127
const originalHandler = createApiHandler;
128128

129129
vi.mocked(createApiHandler).mockImplementation((_middlewares, handler) => {
130-
return async (request, context) => {
130+
return async (request, context: any) => {
131131
context = context || {};
132132
context.session = {
133133
user: { id: 'staff-1', email: 'staff@example.com', role: 'STAFF' },

tests/integration/payments/payment-validator-robustness.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ describe('Payment Validator Provider Outage Resilience (T041)', () => {
130130
shippingAmount: 0,
131131
discountAmount: 0,
132132
totalAmount: 5000,
133-
shippingAddress: { connect: { id: shippingAddr.id } },
134-
billingAddress: { connect: { id: billingAddr.id } },
133+
shippingAddressId: shippingAddr.id,
134+
billingAddressId: billingAddr.id,
135135
},
136136
});
137137

@@ -212,8 +212,8 @@ describe('Payment Validator Provider Outage Resilience (T041)', () => {
212212
shippingAmount: 0,
213213
discountAmount: 0,
214214
totalAmount: 15000,
215-
shippingAddress: { connect: { id: otherShippingAddr.id } },
216-
billingAddress: { connect: { id: otherBillingAddr.id } },
215+
shippingAddressId: otherShippingAddr.id,
216+
billingAddressId: otherBillingAddr.id,
217217
},
218218
});
219219

@@ -360,15 +360,15 @@ describe('Payment Validator Provider Outage Resilience (T041)', () => {
360360
data: {
361361
storeId,
362362
userId,
363-
orderNumber: 'ORD-FULL-FLOW',
363+
orderNumber: 'ORD-CHECKOUT',
364364
status: 'PENDING',
365365
subtotal: 12000,
366366
taxAmount: 0,
367367
shippingAmount: 0,
368368
discountAmount: 0,
369369
totalAmount: 12000,
370-
shippingAddress: { connect: { id: checkoutShippingAddr.id } },
371-
billingAddress: { connect: { id: checkoutBillingAddr.id } },
370+
shippingAddressId: checkoutShippingAddr.id,
371+
billingAddressId: checkoutBillingAddr.id,
372372
},
373373
});
374374

tests/unit/lib/job-queue-stub.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ describe('JobQueueStub', () => {
200200

201201
it('should auto-start in development mode', () => {
202202
const originalEnv = process.env.NODE_ENV;
203-
process.env.NODE_ENV = 'development';
203+
Object.defineProperty(process.env, 'NODE_ENV', { value: 'development', writable: true, configurable: true });
204204

205205
const devQueue = new JobQueueStub();
206206
expect(devQueue).toBeDefined(); // Auto-started
207207

208-
process.env.NODE_ENV = originalEnv;
208+
Object.defineProperty(process.env, 'NODE_ENV', { value: originalEnv, writable: true, configurable: true });
209209
});
210210
});
211211

tests/visual/percy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import percySnapshot from '@percy/playwright';
4040
*/
4141

4242
const BASE_URL = process.env.BASE_URL || 'http://localhost:3000';
43-
const _TEST_STORE_SUBDOMAIN = 'test-store';/**
43+
/**
4444
* Percy snapshot options
4545
* - widths: Defined in .percy.yml (375px, 768px, 1280px)
4646
* - minHeight: Defined in .percy.yml (1024px)

tsconfig.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)