mirror of
https://github.com/tiennm99/thptqg.git
synced 2026-10-11 03:13:48 +00:00
The app was a single-entry React SPA: one index.html that the assembler copied to every dataset path, with a hand-rolled router resolving the dataset from window.location and the title patched in at runtime because one file had to serve every route. SvelteKit prerenders a real page per route instead. The entry generator in routes/[dataset]/+page.ts reads the same datasets.json the assembler does, so the set of pages and the set of databases cannot drift apart, and each dataset page ships its own <title> and description. Everything framework-free moved across unchanged in behaviour and became typed: the admission blocks, subject list, query classifier and SQL presets. `Student` now mirrors the 22-column table, so a mistyped column name fails the build rather than rendering blank. Tailwind replaces the stylesheet. Theme tokens are CSS variables, which keeps dark mode a single block of overrides rather than a `dark:` variant on every class. The score tiers stay hand-written CSS: the class is chosen at runtime from a score, and no utility generator can see that. Adds the tests the frontend never had, over the three modules where a silent wrong answer is possible — most importantly that toAscii here folds exactly as ToAscii does in the parser, which is what makes accent-insensitive search find anything. The assembler stops copying index.html per dataset and checks that the build prerendered each one instead. The database presence, raw-artifact and idempotence guards are untouched. Verified: 25 tests, ESLint and svelte-check clean, and a full `assemble site` producing /thptqg/, /thptqg/2016/, /thptqg/2017/ and 404.html with absolute asset URLs. Not verified in a browser — this machine has none.
158 lines
5.3 KiB
Go
158 lines
5.3 KiB
Go
package site
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tiennm99/thptqg/assembler/internal/registry"
|
|
)
|
|
|
|
var datasets = []registry.Dataset{{ID: "2016"}, {ID: "2017"}}
|
|
|
|
// fakeBuild stands in for a web build: the prerendered hub and one page per
|
|
// dataset, an asset, and whatever databases the caller wants staged.
|
|
func fakeBuild(t *testing.T, dbs ...string) Paths {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
dist := filepath.Join(root, "web", "dist")
|
|
write(t, filepath.Join(dist, "index.html"), "<html>hub</html>")
|
|
for _, d := range datasets {
|
|
write(t, filepath.Join(dist, d.ID, "index.html"), "<html>"+d.ID+"</html>")
|
|
}
|
|
write(t, filepath.Join(dist, "_app", "immutable", "entry.js"), "console.log(1)")
|
|
for _, name := range dbs {
|
|
write(t, filepath.Join(dist, "db", name), "gzipped-bytes")
|
|
}
|
|
return Paths{Web: filepath.Join(root, "web"), Dist: dist, Site: filepath.Join(root, "_site")}
|
|
}
|
|
|
|
func write(t *testing.T, path, body string) {
|
|
t.Helper()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestAssembleProducesAPageForEveryDataset(t *testing.T) {
|
|
p := fakeBuild(t, "2016.db.gz", "2017.db.gz")
|
|
if err := Assemble(p, datasets); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{
|
|
"index.html",
|
|
"404.html",
|
|
filepath.Join("2016", "index.html"),
|
|
filepath.Join("2017", "index.html"),
|
|
filepath.Join("_app", "immutable", "entry.js"),
|
|
filepath.Join("db", "2016.db.gz"),
|
|
} {
|
|
if _, err := os.Stat(filepath.Join(p.Site, want)); err != nil {
|
|
t.Errorf("missing from the artifact: %s", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestMissingDatasetPageFailsTheBuild: the pages come from the web build's
|
|
// entry generator, which reads the same registry this does. If the two fall out
|
|
// of step, that dataset's URL 404s — so the build stops instead.
|
|
func TestMissingDatasetPageFailsTheBuild(t *testing.T) {
|
|
p := fakeBuild(t, "2016.db.gz", "2017.db.gz")
|
|
if err := os.RemoveAll(filepath.Join(p.Dist, "2017")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
err := Assemble(p, datasets)
|
|
if err == nil {
|
|
t.Fatal("expected an error when a dataset page was not prerendered")
|
|
}
|
|
if !strings.Contains(err.Error(), "2017") {
|
|
t.Errorf("the error should name the missing page, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestMissingDatabaseFailsTheBuild is the guard that closes the widest hole.
|
|
//
|
|
// Without it the site assembles happily with an empty db/ directory: every page
|
|
// renders, every query 404s, and CI stays green. The row-count and size guards
|
|
// cannot catch this — they only run when a database was built at all.
|
|
func TestMissingDatabaseFailsTheBuild(t *testing.T) {
|
|
p := fakeBuild(t, "2016.db.gz") // 2017 never built
|
|
err := Assemble(p, datasets)
|
|
if err == nil {
|
|
t.Fatal("expected an error when a database is missing")
|
|
}
|
|
if !strings.Contains(err.Error(), "2017.db.gz") {
|
|
t.Errorf("the error should name the missing database, got: %v", err)
|
|
}
|
|
}
|
|
|
|
// TestEmptyDatabaseFailsTheBuild: a zero-byte file satisfies "exists" but is
|
|
// not a database.
|
|
func TestEmptyDatabaseFailsTheBuild(t *testing.T) {
|
|
p := fakeBuild(t, "2016.db.gz", "2017.db.gz")
|
|
write(t, filepath.Join(p.Dist, "db", "2017.db.gz"), "")
|
|
if err := Assemble(p, datasets); err == nil {
|
|
t.Fatal("expected an error for a zero-byte database")
|
|
}
|
|
}
|
|
|
|
// TestRawDatabaseFailsTheBuild: the compression step deletes its source, so a
|
|
// raw .db here means an interrupted run left one behind — and it is 100+ MB.
|
|
func TestRawDatabaseFailsTheBuild(t *testing.T) {
|
|
for _, name := range []string{"2016.db", "2016.db-journal", "2016.db-wal", "2016.db-shm"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
p := fakeBuild(t, "2016.db.gz", "2017.db.gz")
|
|
write(t, filepath.Join(p.Dist, "db", name), "raw sqlite")
|
|
err := Assemble(p, datasets)
|
|
if err == nil {
|
|
t.Fatalf("expected an error for %s", name)
|
|
}
|
|
if !strings.Contains(err.Error(), "uncompressed") {
|
|
t.Errorf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestGzipIsNotMistakenForRaw: the reject pattern is anchored, so a .db.gz must
|
|
// pass. Getting this wrong would fail every build.
|
|
func TestGzipIsNotMistakenForRaw(t *testing.T) {
|
|
if rawDatabase.MatchString("2016.db.gz") {
|
|
t.Error("a .db.gz must not be treated as an uncompressed database")
|
|
}
|
|
for _, name := range []string{"2016.db", "x.db-journal", "x.db-wal", "x.db-shm"} {
|
|
if !rawDatabase.MatchString(name) {
|
|
t.Errorf("%s should be treated as an uncompressed artifact", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAssembleRejectsAMissingBuild(t *testing.T) {
|
|
root := t.TempDir()
|
|
p := Paths{Web: root, Dist: filepath.Join(root, "dist"), Site: filepath.Join(root, "_site")}
|
|
if err := Assemble(p, datasets); err == nil {
|
|
t.Fatal("expected an error when there is no web build")
|
|
}
|
|
}
|
|
|
|
// TestAssembleIsIdempotent: the site directory is rebuilt from scratch, so a
|
|
// previous run's leftovers cannot survive into the artifact.
|
|
func TestAssembleIsIdempotent(t *testing.T) {
|
|
p := fakeBuild(t, "2016.db.gz", "2017.db.gz")
|
|
if err := Assemble(p, datasets); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
stale := filepath.Join(p.Site, "2015", "index.html")
|
|
write(t, stale, "old dataset")
|
|
if err := Assemble(p, datasets); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := os.Stat(stale); !os.IsNotExist(err) {
|
|
t.Error("a directory from a previous run survived into the new artifact")
|
|
}
|
|
}
|