diff --git a/backend/tests/interviewExperienceController.unit.test.js b/backend/tests/interviewExperienceController.unit.test.js index 0953057a..2c61e709 100644 --- a/backend/tests/interviewExperienceController.unit.test.js +++ b/backend/tests/interviewExperienceController.unit.test.js @@ -206,7 +206,43 @@ describe("createInterviewExperience", () => { expect(res.status).toHaveBeenCalledWith(201); }); - it("isolates anonymous submissions by clientKey so different visitors with the same key both persist", async () => { + it("returns the existing submission when the same authenticated user retries the same idempotency key", async () => { + InterviewExperience.findOne = vi.fn().mockResolvedValue(sampleDoc); + InterviewExperience.create = vi.fn(); + + const userA = { _id: "507f1f77bcf86cd799439011" }; + const req = makeReq( + { + company: "Google", + role: "SDE-2", + summary: "Summary", + idempotencyKey: "submit-key-abc12345", + }, + {}, + {}, + userA, + ); + const res = makeRes(); + + await createInterviewExperience(req, res); + + expect(InterviewExperience.findOne).toHaveBeenCalledWith({ + idempotencyKey: "submit-key-abc12345", + userId: "507f1f77bcf86cd799439011", + }); + expect(InterviewExperience.create).not.toHaveBeenCalled(); + expect(res.status).toHaveBeenCalledWith(200); + expect(res.json).toHaveBeenCalledWith( + expect.objectContaining({ + success: true, + experience: expect.objectContaining({ + id: "507f1f77bcf86cd799439011", + }), + }), + ); + }); + + it("does not deduplicate an anonymous request against an authenticated submission with the same idempotency key", async () => { InterviewExperience.findOne = vi.fn().mockResolvedValue(null); InterviewExperience.create = vi.fn().mockResolvedValue(sampleDoc); @@ -214,7 +250,7 @@ describe("createInterviewExperience", () => { company: "Google", role: "SDE-2", summary: "Summary", - clientKey: "22222222-2222-4222-8222-222222222222", + clientKey: "11111111-1111-4111-8111-111111111111", idempotencyKey: "shared-key-abc12345", }); const res = makeRes(); @@ -223,9 +259,10 @@ describe("createInterviewExperience", () => { expect(InterviewExperience.findOne).toHaveBeenCalledWith({ idempotencyKey: "shared-key-abc12345", - clientKey: "22222222-2222-4222-8222-222222222222", + clientKey: "11111111-1111-4111-8111-111111111111", }); expect(InterviewExperience.create).toHaveBeenCalled(); + expect(res.status).toHaveBeenCalledWith(201); }); });