-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38f5bc5
commit fdcf764
Showing
10 changed files
with
419 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { type SQSRecord } from "aws-lambda"; | ||
import { describe, expect, test, vi } from "vitest"; | ||
|
||
import { EmailParsePayloadError } from "@/lib/emails/errors"; | ||
import { getLogger } from "@/providers/logger"; // Mock the logger provider | ||
|
||
import { parseRecord } from "./utils"; | ||
|
||
describe("utils", () => { | ||
describe("parseRecord", () => { | ||
vi.mock("@/providers/logger", () => { | ||
const mockLogger = { | ||
error: vi.fn(), | ||
}; | ||
|
||
return { | ||
getLogger: () => mockLogger, | ||
}; | ||
}); | ||
|
||
test("should parse valid record and return data", () => { | ||
// given | ||
const data = { | ||
event: "some_event", | ||
data: { key: "value" }, | ||
}; | ||
const validRecord = { | ||
Body: JSON.stringify(data), | ||
} as any as SQSRecord; | ||
|
||
// when | ||
const result = parseRecord(validRecord); | ||
|
||
// when | ||
expect(result).toEqual(data); | ||
}); | ||
|
||
test("should log and throw error when parsing fails", () => { | ||
// given | ||
const invalidRecord = { | ||
Body: "{invalidJson", | ||
} as any as SQSRecord; | ||
const logger = getLogger(); | ||
|
||
// when & then | ||
expect(() => parseRecord(invalidRecord)).toThrow(EmailParsePayloadError); | ||
expect(logger.error).toHaveBeenCalledWith( | ||
"Failed to parse record payload.", | ||
expect.objectContaining({ | ||
record: invalidRecord, | ||
error: expect.any(Error), | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { type SQSRecord } from "aws-lambda"; | ||
|
||
import { EmailParsePayloadError } from "@/lib/emails/errors"; | ||
import { type SerializedPayload } from "@/lib/emails/events/helpers"; | ||
import { getLogger } from "@/providers/logger"; | ||
|
||
const logger = getLogger(); | ||
|
||
export const parseRecord = (record: SQSRecord) => { | ||
try { | ||
// Proxy events has invalid types. | ||
const data = JSON.parse((record as any).Body); | ||
|
||
return data as SerializedPayload; | ||
} catch (error) { | ||
logger.error("Failed to parse record payload.", { record, error }); | ||
|
||
// TODO: Should be non transient error | ||
throw new EmailParsePayloadError("Failed to parse record payload.", { | ||
cause: { source: error as Error }, | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { describe, expect, test, vi } from "vitest"; | ||
|
||
import { CONFIG } from "@/config"; | ||
|
||
import { DEFAULT_REGION, getRegion, REGIONS } from "./regions"; | ||
|
||
describe("regions", () => { | ||
describe("getRegion", () => { | ||
test("should return the correct region for a valid slug", () => { | ||
// given | ||
const validSlug = "channel-us"; | ||
|
||
// when | ||
const result = getRegion(validSlug); | ||
|
||
// then | ||
expect(result).toEqual(REGIONS.US); | ||
}); | ||
|
||
test("should return the default region when slug is not found", () => { | ||
// given | ||
const invalidSlug = "channel-invalid"; | ||
|
||
// when | ||
const result = getRegion(invalidSlug); | ||
|
||
// then | ||
expect(result).toEqual(DEFAULT_REGION); | ||
}); | ||
|
||
test("should throw an error if no default region exists in CONFIG", () => { | ||
// given | ||
const invalidSlug = "channel-invalid"; | ||
const originalDefaultRegion = CONFIG.DEFAULT_REGION; | ||
vi.spyOn(CONFIG, "DEFAULT_REGION", "get").mockReturnValue( | ||
undefined as any | ||
); | ||
|
||
// when / then | ||
expect(() => getRegion(invalidSlug)).toThrow( | ||
`Region not found for channel slug ${invalidSlug}.` | ||
); | ||
|
||
// Clean up | ||
vi.spyOn(CONFIG, "DEFAULT_REGION", "get").mockReturnValue( | ||
originalDefaultRegion | ||
); | ||
}); | ||
|
||
test("should return region even if slug case is different", () => { | ||
// given | ||
const slugWithDifferentCase = "CHANNEL-UK"; | ||
|
||
// when | ||
const result = getRegion(slugWithDifferentCase.toLowerCase()); | ||
|
||
// then | ||
expect(result).toEqual(REGIONS.GB); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe, expect, test } from "vitest"; | ||
|
||
import { isURL } from "./utils"; | ||
|
||
describe("utils", () => { | ||
describe("isURL", () => { | ||
test("should return true for a valid URL", () => { | ||
// given | ||
const validUrl = "http://example.com"; | ||
|
||
// when | ||
const result = isURL(validUrl); | ||
|
||
// then | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test("should return false for an invalid URL", () => { | ||
// given | ||
const invalidUrl = "not a valid url"; | ||
|
||
// when | ||
const result = isURL(invalidUrl); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should return false for an empty string", () => { | ||
// given | ||
const emptyString = ""; | ||
|
||
// when | ||
const result = isURL(emptyString); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should return true for a valid URL with query parameters", () => { | ||
// given | ||
const validUrlWithParams = "https://example.com?query=test"; | ||
|
||
// when | ||
const result = isURL(validUrlWithParams); | ||
|
||
// then | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test("should return false for a malformed URL", () => { | ||
// given | ||
const malformedUrl = "http://example.com:port"; | ||
|
||
// when | ||
const result = isURL(malformedUrl); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { z } from "zod"; | ||
|
||
import { envBool, envToStrList } from "./env"; | ||
|
||
describe("env", () => { | ||
describe("envBool", () => { | ||
test('should return true for "true"', () => { | ||
// given | ||
const input = "true"; | ||
|
||
// when | ||
const result = envBool.parse(input); | ||
|
||
// then | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test('should return false for "false"', () => { | ||
// given | ||
const input = "false"; | ||
|
||
// when | ||
const result = envBool.parse(input); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should return false for an empty string", () => { | ||
// given | ||
const input = ""; | ||
|
||
// when | ||
const result = envBool.parse(input); | ||
|
||
// then | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test("should throw an error for invalid values", () => { | ||
// given | ||
const input = "invalid"; | ||
|
||
// when / then | ||
expect(() => envBool.parse(input)).toThrow(z.ZodError); | ||
}); | ||
}); | ||
|
||
describe("envToStrList", () => { | ||
test("should return an array of strings for a valid comma-separated string", () => { | ||
// given | ||
const input = "value1,value2,value3"; | ||
|
||
// when | ||
const result = envToStrList(input); | ||
|
||
// then | ||
expect(result).toEqual(["value1", "value2", "value3"]); | ||
}); | ||
|
||
test("should return an empty array when env is undefined and defaultEmpty is false", () => { | ||
// given | ||
const input = undefined; | ||
const defaultEmpty = false; | ||
|
||
// when | ||
const result = envToStrList(input, defaultEmpty); | ||
|
||
// then | ||
expect(result).toEqual([]); | ||
}); | ||
|
||
test("should return undefined when env is undefined and defaultEmpty is true", () => { | ||
// given | ||
const input = undefined; | ||
const defaultEmpty = true; | ||
|
||
// when | ||
const result = envToStrList(input, defaultEmpty); | ||
|
||
// then | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
test("should filter out empty values in a comma-separated string", () => { | ||
// given | ||
const input = "value1,,value3"; | ||
|
||
// when | ||
const result = envToStrList(input); | ||
|
||
// then | ||
expect(result).toEqual(["value1", "value3"]); | ||
}); | ||
|
||
test("should return an empty array when env is an empty string", () => { | ||
// given | ||
const input = ""; | ||
|
||
// when | ||
const result = envToStrList(input); | ||
|
||
// then | ||
expect(result).toEqual([]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.