Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix NoMatch Bug
  • Loading branch information
junaidiqbalmoj committed Dec 4, 2025
commit 69fe51f65f20c7aebb00a50174e8703bb6af8d72
37 changes: 34 additions & 3 deletions libs/api/src/blob-ingestion/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { validateBlobRequest } from "./validation.js";

vi.mock("@hmcts/location", () => ({
getLocationById: vi.fn((id: number) => {
// Return a location for ID 123, null for others
// Return a Promise to match actual function signature
if (id === 123) {
return { id: 123, name: "Test Court" };
return Promise.resolve({ id: 123, name: "Test Court" });
}
return null;
return Promise.resolve(undefined);
})
}));

Expand Down Expand Up @@ -223,4 +223,35 @@ describe("validateBlobRequest", () => {
expect(result.isValid).toBe(false);
expect(result.errors).toContainEqual({ field: "court_id", message: "court_id must be a valid number" });
});

it("should await getLocationById and not treat Promise as truthy", async () => {
const { getLocationById } = await import("@hmcts/location");

// Mock non-existent location
vi.mocked(getLocationById).mockResolvedValue(undefined);

const request = { ...validRequest, court_id: "999" };
const result = await validateBlobRequest(request, 1000);

// This test FAILS before the fix because Promise is truthy
expect(result.locationExists).toBe(false);
expect(getLocationById).toHaveBeenCalledWith(999);
});

it("should correctly identify existing location", async () => {
const { getLocationById } = await import("@hmcts/location");

vi.mocked(getLocationById).mockResolvedValue({
locationId: 123,
name: "Test Court",
welshName: "Llys Prawf",
regions: [],
subJurisdictions: []
});

const request = { ...validRequest, court_id: "123" };
const result = await validateBlobRequest(request, 1000);

expect(result.locationExists).toBe(true);
});
});
2 changes: 1 addition & 1 deletion libs/api/src/blob-ingestion/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function validateBlobRequest(request: BlobIngestionRequest, rawBody
if (Number.isNaN(locationId)) {
errors.push({ field: "court_id", message: "court_id must be a valid number" });
} else {
const location = getLocationById(locationId);
const location = await getLocationById(locationId);
locationExists = !!location;
// Note: We don't add an error if location doesn't exist
// This is handled by setting no_match=true
Expand Down