Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"test:live": "LIVE_SCRAPER=1 bun test --isolate test/live-scraper.test.ts",
"record:fixtures": "bun run scripts/record-scraper-pages.ts",
"sync:db": "bun run scripts/sync-db.ts",
"fix:assisting-systems": "bun run scripts/fix-assisting-systems.ts",
"smoke:sqlite": "bun scripts/smoke-sqlite.ts"
},
"prettier": {
Expand Down
43 changes: 43 additions & 0 deletions backend/scripts/fix-assisting-systems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Repairs trim_details rows stored before the trailing-<br> fix in
* scrapeTrimDetailsByTrimUrl: a cell whose last <br> had no entry after it
* became a value ending in ", ", which the UI then rendered verbatim.
*
* Only rows that still carry the old shape are touched, and only their
* trailing separator is removed, so a legitimately comma-ended value cannot be
* mangled. Idempotent: a second run finds nothing to do.
*
* bun run scripts/fix-assisting-systems.ts
*/
import {AppDataSource} from "../src/data-source";
import {TrimDetails} from "../src/entity/entities";

await AppDataSource.initialize();

try {
// A stored value ends in the separator only because of the bug, so the
// trailing ", " is the whole of the damage.
const affected = await AppDataSource.manager
.createQueryBuilder(TrimDetails, "details")
.where("details.assistingSystems LIKE :suffix", {suffix: "%,%"})
.getMany();

console.log(`rows ending in a separator: ${affected.length}`);
for (const details of affected) {
console.log(` ${details.id}: ${JSON.stringify(details.assistingSystems)}`);
}

for (const details of affected) {
details.assistingSystems = details.assistingSystems.replace(/,[\s,]*$/, "").trimEnd();
await AppDataSource.manager.save(details);
}
console.log(`updated ${affected.length}`);

const remaining = await AppDataSource.manager
.createQueryBuilder(TrimDetails, "details")
.where("details.assistingSystems LIKE :suffix", {suffix: "%,%"})
.getCount();
console.log(`still ending in a separator: ${remaining}`);
} finally {
await AppDataSource.destroy();
}
31 changes: 26 additions & 5 deletions backend/src/data/fetch-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,15 @@ export class FetchProvider {
where: {
id: generationId,
},
// The model carries its brand, and a deep link to a generation has no
// other way to name the marque: there is no page above it to inherit
// the path from, so a missing brand here shows up as a path that
// starts at the model.
relations: {
trims: true,
model: true,
model: {
brand: true,
},
},
});

Expand All @@ -251,8 +257,16 @@ export class FetchProvider {
where: {
id: trimId,
},
// The whole ancestor chain, not just the generation. A deep link to a
// trim is the one page with no page above it to inherit a path from,
// so anything missing here is a breadcrumb that cannot be rendered
// or linked.
relations: {
generation: true,
generation: {
model: {
brand: true,
},
},
trimDetails: true,
},
});
Expand Down Expand Up @@ -590,9 +604,16 @@ export class FetchProvider {
else if (label.includes('front brakes')) trimDetails.frontBrakes = value;
else if (label.includes('rear brakes')) trimDetails.rearBrakes = value;
else if (label.includes('assisting systems')) {
const systems = Array.from(td.querySelectorAll('br')).map(() => '');
const text = td.innerHTML.replace(/<br\s*\/?>/g, '|').replace(/<[^>]*>/g, '');
trimDetails.assistingSystems = text.replace(/\|/g, ', ').trim();
// The list is <br>-separated inside one cell. Turning each
// break into a separator, then dropping empty entries, is
// what keeps a trailing <br> - which the real pages carry -
// from becoming a value that ends in ", ".
const marked = td.innerHTML.replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]*>/g, '');
trimDetails.assistingSystems = marked
.split('\n')
.map((entry) => entry.replace(/\s+/g, ' ').trim())
.filter((entry) => entry !== '')
.join(', ');
} else if (label.includes('steering type') && !label.includes('power')) trimDetails.steeringType = value;
else if (label.includes('power steering')) trimDetails.powerSteering = value;
// The site labels this "Tire size" now; "Tires size"/"Tyres size" before.
Expand Down
25 changes: 25 additions & 0 deletions backend/test/fetch-provider.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {EntityManager} from "typeorm";

import {Brand, Generation, Model, Trim, TrimDetails} from "../src/entity/entities";
import {
ASSISTING_SYSTEMS_SHAPES,
BRANDS_WITH_GAPS,
BRANDS_WITH_IMAGE_URL_SHAPES,
DETAILS_WITH_GAPS,
Expand Down Expand Up @@ -574,6 +575,30 @@ describe("FetchProvider", () => {
expect(found?.trimDetails?.seats).toBe("");
});

// One test per shape rather than one page carrying them all: a page
// yields a single value per label, so several rows of the same label
// would only ever assert the last one.
test.each(ASSISTING_SYSTEMS_SHAPES)(
"joins the assisting systems list without stray separators: $expected",
async ({html, expected}) => {
const {trim} = await seedFullChain();
RecordedScraper.serveHtml(`
<div class="cardetailsout">
<div class="cardetails">
<div class="row"><div class="par">Brand</div><div class="val">BMW</div></div>
<div class="row"><div class="par">Assisting systems</div>
<div class="val">${html}</div>
</div>
</div>
</div>
`);

const found = await new FetchProvider().getTrimWithDetails(trim.id);

expect(found?.trimDetails?.assistingSystems).toBe(expected);
},
);

test("does not crash when the specification is missing entirely", async () => {
const {trim} = await seedFullChain();
RecordedScraper.serveHtml(EMPTY_PAGE);
Expand Down
20 changes: 19 additions & 1 deletion backend/test/helpers/edge-cases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,22 @@ export const DETAILS_WITH_GAPS = `
<div class="row"><div class="par">Tire size</div><div class="val">205/55 R16</div></div>
</div>
</div>
`;
`;

/**
* The <br>-separated "Assisting systems" cell in the shapes the real trim pages
* produce. Separate from DETAILS_WITH_GAPS because one page yields one value
* per label: adding these rows there would only test whichever came last.
*
* The trailing and doubled breaks are the point. A live trim page ends the
* cell with a break, and the source nests its entries across lines, so both
* produce an empty entry that must not become a trailing ", " or a blank
* between two systems.
*/
export const ASSISTING_SYSTEMS_SHAPES: {html: string, expected: string}[] = [
{html: 'ABS<br>ESP<br>TCS', expected: 'ABS, ESP, TCS'},
{html: 'ABS (Anti-lock braking system)<br>', expected: 'ABS (Anti-lock braking system)'},
{html: 'ABS<br>\n ESP', expected: 'ABS, ESP'},
{html: 'ABS<br><br>ESP', expected: 'ABS, ESP'},
{html: 'ABS', expected: 'ABS'},
];
109 changes: 109 additions & 0 deletions frontend/src/app/app.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.bar {
position: sticky;
top: 0;
z-index: 50;
display: flex;
align-items: center;
gap: 1rem 1.5rem;
flex-wrap: wrap;
min-height: var(--bar-h);
padding: 0.625rem var(--gutter);
/* Opaque, because content scrolls underneath it and a blur would let the
letters of a brand grid show through the path. */
background: var(--bg);
border-bottom: 1px solid var(--line);
}

.mark {
display: inline-flex;
align-items: center;
gap: 0.5rem;
flex-shrink: 0;
}

.mark__glyph {
width: 21px;
height: 21px;
color: var(--signal);
}

.mark__name {
font-size: 0.9375rem;
font-weight: 600;
letter-spacing: -0.02em;
}

.mark__name-rest {
color: var(--faint);
font-weight: 400;
}

.mark:hover .mark__name-rest {
color: var(--dim);
}

/* The path runs to the end of the bar and truncates, so a long trim name never
pushes the layout around or wraps into a second row on desktop. */
.path {
flex: 1;
justify-content: flex-end;
align-items: baseline;
gap: 0 0.5rem;
min-width: 0;
margin: 0;
overflow: hidden;
flex-wrap: nowrap;
}

.path__item {
display: inline-flex;
align-items: baseline;
gap: 0.5rem;
min-width: 0;
}

/* The separator belongs to the item it follows, so hiding an item on narrow
screens takes its separator with it. */
.path__item + .path__item::before {
content: '/';
flex-shrink: 0;
color: var(--line);
}

.path__crumb {
color: var(--dim);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
transition: color 0.15s var(--ease);
}

.path__crumb--here {
color: var(--text);
font-weight: 500;
/* The current level keeps its full weight so the eye lands there first. */
}

.path__crumb:not(.path__crumb--here):hover {
color: var(--signal);
}

/* Below this width there is no room to put the path beside the wordmark, so it
drops to its own row. Only the last two levels survive: the full path runs to
five items deep and would truncate every one of them to nothing. */
@media (max-width: 46rem) {
.bar {
align-items: center;
row-gap: 0.375rem;
}

.path {
order: 3;
flex-basis: 100%;
justify-content: flex-start;
}

.path__item:not(:nth-last-child(-n + 2)) {
display: none;
}
}
31 changes: 28 additions & 3 deletions frontend/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
<main>
</main>
<header class="bar">
<a routerLink="/browse" class="mark" aria-label="AutoDataWrapper, all brands">
<svg viewBox="0 0 24 24" aria-hidden="true" class="mark__glyph">
<path d="M3.5 17a8.5 8.5 0 1 1 17 0" fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round"/>
<path d="M12 12l4.2-3.4" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
<circle cx="12" cy="12" r="1.7" fill="currentColor"/>
</svg>
<span class="mark__name">AutoData<span class="mark__name-rest">Wrapper</span></span>
</a>

@if (nav.crumbs().length > 0) {
<nav class="path" aria-label="Path to this car">
@for (crumb of nav.crumbs(); track $index) {
<span class="path__item">
@if (crumb.link) {
<a class="path__crumb" [routerLink]="crumb.link">{{ crumb.label }}</a>
} @else {
<span class="path__crumb path__crumb--here" aria-current="page">{{ crumb.label }}</span>
}
</span>
}
</nav>
}
</header>

<router-outlet/>
<main>
<router-outlet/>
</main>
14 changes: 8 additions & 6 deletions frontend/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {Component, signal} from '@angular/core';
import {RouterOutlet} from '@angular/router';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {RouterLink, RouterOutlet} from '@angular/router';
import {NavState} from './nav-state';

@Component({
selector: 'app-root',
imports: [RouterOutlet],
imports: [RouterLink, RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
styleUrl: './app.component.css',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AppComponent {
protected readonly title = signal('AutoDataWrapper');
}
protected readonly nav = inject(NavState);
}
Loading
Loading