Skip to content

Commit

Permalink
Bug search hist (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
talepre authored Nov 7, 2024
1 parent a6e7928 commit f821dbf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 53 deletions.
37 changes: 4 additions & 33 deletions packages/server/src/handlers/search-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,12 @@ import { SetupServerApi, setupServer } from "msw/node";
import { env } from "../env/server";
import { searchHandler } from "./search-handler";

const validHits = new Array(6).fill(0).map((_, i) => ({
const validHits = new Array(5).fill(0).map((_, i) => ({
displayName: `Hit ${i}`,
highlight: `highlight ${i}`,
href: "https://example.com",
}));

const withInvalidHit = [
{
displayName: "Invalid hit",
highlight: "Invalid highlight",
href: "notaurl",
},
...validHits,
];

describe("Search handler", () => {
let server: SetupServerApi;

Expand All @@ -39,12 +30,12 @@ describe("Search handler", () => {

afterAll(() => server.close());

test("Should return html containing first 5 hits only", async () => {
test("Should show total hits", async () => {
server.use(
http.get(env.SEARCH_API_URL, () =>
HttpResponse.json({
hits: validHits,
total: validHits.length,
total: 10,
}),
),
);
Expand All @@ -55,28 +46,8 @@ describe("Search handler", () => {
query: "",
});

expect(html).toContain("10 treff for");
expect(html).toContain("highlight 4");
expect(html).not.toContain("highlight 5");
});

test("Should not include invalid hits", async () => {
server.use(
http.get(env.SEARCH_API_URL, () =>
HttpResponse.json({
hits: withInvalidHit,
total: withInvalidHit.length,
}),
),
);

const html = await searchHandler({
context: "privatperson",
language: "nb",
query: "",
});

expect(html).toContain("Hit 4");
expect(html).not.toContain("Invalid hit");
});

test("Should encode/decode the query as appropriate", async () => {
Expand Down
28 changes: 8 additions & 20 deletions packages/server/src/handlers/search-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ export type SearchResult = z.infer<typeof resultSchema>;
const resultSchema = z.object({
total: z.number(),
hits: z.array(
z
.optional(
z.object({
displayName: z.string(),
highlight: z.string(),
href: z.string().url(),
}),
)
.catch((ctx) => {
console.error(
`Error validating search hit - ${JSON.stringify(ctx.input)}`,
);
return undefined;
}),
z.object({
displayName: z.string(),
highlight: z.string(),
href: z.string().url(),
}),
),
});

Expand Down Expand Up @@ -68,15 +59,12 @@ export const searchHandler = async ({
return SearchErrorView().render({ language });
}

// zod does not seem to generate a correct return type with a catch clause included
const hits = (result.data as SearchResult).hits
.filter((result) => !!result)
.slice(0, 5);
const searchResult = result.data as SearchResult;

return SearchHits({
results: {
total: hits.length,
hits,
total: searchResult.total,
hits: searchResult.hits,
},
query: queryDecoded,
context,
Expand Down

0 comments on commit f821dbf

Please sign in to comment.