Skip to content

Commit d190674

Browse files
committed
Fix suggestion handling when there's a search prefix
1 parent 52840e8 commit d190674

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

e2e/home.spec.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
1-
import { test, expect } from '@playwright/test';
1+
import { test, expect } from "@playwright/test";
22

3-
test('home page', async ({ page }) => {
4-
await page.goto('/');
5-
await expect(page).toHaveTitle(/Search the CPAN - metacpan.org/, { timeout: 10 });
3+
test("home page", async ({ page }) => {
4+
await page.goto("/");
5+
await expect(page).toHaveTitle(/Search the CPAN - metacpan.org/, {
6+
timeout: 10,
7+
});
8+
});
9+
10+
test("suggest is correct", async ({ page }) => {
11+
await page.goto("/");
12+
const searchInput = page.getByPlaceholder("Search the CPAN");
13+
await expect(searchInput).toBeVisible();
14+
searchInput.fill("HTML:Restrict");
15+
await searchInput.press("Enter");
16+
17+
await expect(page.getByRole('alert')).toContainText('Did you mean: HTML::Restrict');
18+
});
19+
20+
test("suggest accounts for prefix and makes suggestion", async ({ page }) => {
21+
await page.goto("/");
22+
const searchInput = page.getByPlaceholder("Search the CPAN");
23+
await expect(searchInput).toBeVisible();
24+
searchInput.fill("distribution:HTML:Restrict");
25+
await searchInput.press("Enter");
26+
27+
await expect(page.getByRole('alert')).toContainText('Did you mean: distribution:HTML::Restrict');
28+
});
29+
30+
test("suggest accounts for prefix but cannot make suggestion", async ({ page }) => {
31+
await page.goto("/");
32+
const searchInput = page.getByPlaceholder("Search the CPAN");
33+
await expect(searchInput).toBeVisible();
34+
searchInput.fill("distribution:HTMLRestrict");
35+
await searchInput.press("Enter");
36+
37+
await expect(page.getByRole('alert')).toBeHidden();
38+
});
39+
40+
test("suggest ignores misspelled prefix and makes suggestion", async ({ page }) => {
41+
await page.goto("/");
42+
const searchInput = page.getByPlaceholder("Search the CPAN");
43+
await expect(searchInput).toBeVisible();
44+
searchInput.fill("disstribution:HTML:Restrict");
45+
await searchInput.press("Enter");
46+
47+
await expect(page.getByRole('alert')).toContainText('Did you mean: disstribution::HTML::Restrict');
648
});

lib/MetaCPAN/Web/Controller/Search.pm

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,14 @@ sub index : Path : Args(0) {
8888

8989
if ( !$results->{total} && !$authors->{total} ) {
9090
my $suggest = $query;
91+
my $prefix = q{};
92+
if ( $suggest =~ s{^(author|distribution|module|version):}{} ) {
93+
$prefix = $1 . ':';
94+
}
9195
$suggest =~ s/\s*:+\s*/::/g;
92-
if ( $suggest ne $query ) {
96+
if ( $prefix . $suggest ne $query ) {
9397
$c->stash( {
94-
suggest => $suggest,
98+
suggest => $prefix . $suggest,
9599
} );
96100
}
97101
$c->stash( {

0 commit comments

Comments
 (0)