From 1c71a2fb6fb6982f89c44d87366b0aa6cfd67d3d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 7 Jan 2026 08:10:58 +0000 Subject: [PATCH] fix: improve test coverage and mock implementations - Add assertion for original property in variable detection test - Clarify test descriptions for HTML-like patterns and programming keywords - Add edge cases for consecutive special characters in variable names - Add username validation assertion in register test - Improve useSearchParams mock to return ReadonlyURLSearchParams - Improve cookies mock with proper return values - Remove unused originalNavigator variable Co-authored-by: f <196477+f@users.noreply.github.com> --- src/__tests__/api/register.test.ts | 9 +++++++ src/__tests__/lib/utils.test.ts | 1 - src/__tests__/lib/variable-detection.test.ts | 27 ++++++++++++++++--- vitest.setup.ts | 28 +++++++++++++------- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/__tests__/api/register.test.ts b/src/__tests__/api/register.test.ts index 61a50417..8f8216bb 100644 --- a/src/__tests__/api/register.test.ts +++ b/src/__tests__/api/register.test.ts @@ -268,6 +268,15 @@ describe("POST /api/auth/register", () => { const response = await POST(request); expect(response.status).toBe(200); + + // Verify that db.user.create was called with the correct username + expect(db.user.create).toHaveBeenCalledWith( + expect.objectContaining({ + data: expect.objectContaining({ + username: "test_user_123", + }), + }) + ); }); }); diff --git a/src/__tests__/lib/utils.test.ts b/src/__tests__/lib/utils.test.ts index 8cb127c2..b60a6729 100644 --- a/src/__tests__/lib/utils.test.ts +++ b/src/__tests__/lib/utils.test.ts @@ -48,7 +48,6 @@ describe("cn (className utility)", () => { describe("isChromeBrowser", () => { const originalWindow = global.window; - const originalNavigator = global.navigator; beforeEach(() => { // Reset window and navigator before each test diff --git a/src/__tests__/lib/variable-detection.test.ts b/src/__tests__/lib/variable-detection.test.ts index b2cf7f69..34b5d457 100644 --- a/src/__tests__/lib/variable-detection.test.ts +++ b/src/__tests__/lib/variable-detection.test.ts @@ -21,6 +21,7 @@ describe("detectVariables", () => { const result = detectVariables("Hello [[ name ]]!"); expect(result).toHaveLength(1); expect(result[0].name).toBe("name"); + expect(result[0].original).toBe("[[ name ]]"); }); it("should detect [[name: default]] with default value", () => { @@ -115,7 +116,7 @@ describe("detectVariables", () => { expect(result).toHaveLength(0); }); - it("should NOT detect lowercase single words (looks like HTML)", () => { + it("should NOT detect lowercase single words in angle brackets (looks like HTML tags)", () => { const result = detectVariables(""); expect(result).toHaveLength(0); }); @@ -166,8 +167,8 @@ describe("detectVariables", () => { } }); - it("should skip programming keywords", () => { - // These would be in angle brackets but should be filtered + it("should skip uppercase programming keywords in angle brackets", () => { + // These would be in angle brackets but should be filtered as keywords const result = detectVariables(" "); expect(result).toHaveLength(0); }); @@ -271,6 +272,26 @@ describe("convertToSupportedFormat", () => { }; expect(convertToSupportedFormat(variable)).toBe("${username}"); }); + + it("should handle consecutive special characters", () => { + const variable1: DetectedVariable = { + original: "[[user@@name]]", + name: "user@@name", + pattern: "double_bracket", + startIndex: 0, + endIndex: 14, + }; + expect(convertToSupportedFormat(variable1)).toBe("${username}"); + + const variable2: DetectedVariable = { + original: "[[user!@#name]]", + name: "user!@#name", + pattern: "double_bracket", + startIndex: 0, + endIndex: 15, + }; + expect(convertToSupportedFormat(variable2)).toBe("${username}"); + }); }); describe("convertAllVariables", () => { diff --git a/vitest.setup.ts b/vitest.setup.ts index f3bb5dfe..49e00078 100644 --- a/vitest.setup.ts +++ b/vitest.setup.ts @@ -16,7 +16,11 @@ vi.mock("next/navigation", () => ({ forward: vi.fn(), prefetch: vi.fn(), }), - useSearchParams: () => new URLSearchParams(), + useSearchParams: () => { + const searchParams = new URLSearchParams(); + // Return ReadonlyURLSearchParams-like object + return searchParams as ReadonlyURLSearchParams; + }, usePathname: () => "/", useParams: () => ({}), redirect: vi.fn(), @@ -24,14 +28,20 @@ vi.mock("next/navigation", () => ({ })); // Mock next/headers -vi.mock("next/headers", () => ({ - cookies: () => ({ - get: vi.fn(), - set: vi.fn(), - delete: vi.fn(), - }), - headers: () => new Headers(), -})); +vi.mock("next/headers", () => { + const getCookieMock = vi.fn().mockReturnValue(null); + const setCookieMock = vi.fn(); + const deleteCookieMock = vi.fn(); + + return { + cookies: () => ({ + get: getCookieMock, + set: setCookieMock, + delete: deleteCookieMock, + }), + headers: () => new Headers(), + }; +}); // Mock next-intl vi.mock("next-intl", () => ({