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>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-07 08:10:58 +00:00
parent b42098994c
commit 1c71a2fb6f
4 changed files with 52 additions and 13 deletions

View File

@@ -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",
}),
})
);
});
});

View File

@@ -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

View File

@@ -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("<username>");
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("<IF> <ELSE> <FOR>");
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", () => {

View File

@@ -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", () => ({