package main import ( "encoding/json" "os" "path/filepath" "strings" "testing" ) func TestTextFallback(t *testing.T) { tests := []struct { name string json string lang string want string wantOK bool }{ {"plain string for every language", `"ArozOS"`, "ja", "ArozOS", true}, {"own translation", `{"en":"Hello","ja":"こんにちは"}`, "ja", "こんにちは", true}, {"falls back to English", `{"en":"Hello"}`, "zh-tw", "Hello", false}, {"English itself is never missing", `{"en":"Hello"}`, "en", "Hello", true}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { var txt Text if err := json.Unmarshal([]byte(tc.json), &txt); err != nil { t.Fatalf("unmarshal: %v", err) } got, ok := txt.get(tc.lang) if got != tc.want || ok != tc.wantOK { t.Errorf("get(%q) = %q, %v; want %q, %v", tc.lang, got, ok, tc.want, tc.wantOK) } }) } } func TestHref(t *testing.T) { en := Language{Code: "en", HTML: "en"} ja := Language{Code: "ja", Dir: "ja", HTML: "ja"} g := &generator{ site: Site{BaseURL: "https://os.aroz.org", Downloads: "https://example.com/dl/"}, pages: map[string]*Page{"index": {}, "media": {}}, } tests := []struct { name string lang Language in string want string }{ {"page in root language", en, "@media", "media.html"}, {"index in root language", en, "@index", "./"}, {"page with fragment", ja, "@media#photo", "media.html#photo"}, {"root file from a language folder", ja, "/img/og.png", "../img/og.png"}, {"release download", en, "dl:web.tar.gz", "https://example.com/dl/web.tar.gz"}, {"external link untouched", ja, "https://github.com", "https://github.com"}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { g.lang = tc.lang g.root = "" if tc.lang.Dir != "" { g.root = "../" } got, err := g.href(tc.in) if err != nil { t.Fatalf("href(%q): %v", tc.in, err) } if got != tc.want { t.Errorf("href(%q) = %q; want %q", tc.in, got, tc.want) } }) } if _, err := g.href("@nope"); err == nil { t.Errorf("href to a missing page should fail") } } func TestPageURLAcrossLanguages(t *testing.T) { en := Language{Code: "en", HTML: "en"} ja := Language{Code: "ja", Dir: "ja", HTML: "ja"} g := &generator{site: Site{BaseURL: "https://os.aroz.org/"}} tests := []struct { name string from, to Language slug, out string }{ {"root to language index", en, ja, "index", "ja/"}, {"language to root index", ja, en, "index", "../"}, {"language to root page", ja, en, "office", "../office.html"}, {"root to language page", en, ja, "office", "ja/office.html"}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { g.lang = tc.from g.root = "" if tc.from.Dir != "" { g.root = "../" } if got := g.pageURL(tc.slug, tc.to); got != tc.out { t.Errorf("pageURL = %q; want %q", got, tc.out) } }) } if got := g.absURL("office", ja); got != "https://os.aroz.org/ja/office.html" { t.Errorf("absURL = %q", got) } if got := g.absURL("index", en); got != "https://os.aroz.org/" { t.Errorf("absURL index = %q", got) } } func TestWebpSize(t *testing.T) { vp8x := make([]byte, 30) copy(vp8x, "RIFF\x00\x00\x00\x00WEBPVP8X") // 2200 x 1497 stored as width-1 and height-1 in 24 bits each. vp8x[24], vp8x[25], vp8x[26] = 0x97, 0x08, 0x00 vp8x[27], vp8x[28], vp8x[29] = 0xd8, 0x05, 0x00 tests := []struct { name string in []byte w, h int ok bool }{ {"extended format", vp8x, 2200, 1497, true}, {"not a webp", []byte(strings.Repeat("x", 30)), 0, 0, false}, {"too short", []byte("RIFF"), 0, 0, false}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { w, h, ok := webpSize(tc.in) if w != tc.w || h != tc.h || ok != tc.ok { t.Errorf("webpSize = %d, %d, %v; want %d, %d, %v", w, h, ok, tc.w, tc.h, tc.ok) } }) } } func TestHighlight(t *testing.T) { got := string(highlight(`requirelib("llm"); // load `)) for _, want := range []string{ `requirelib(`, `"llm"`, `// load <it>`, } { if !strings.Contains(got, want) { t.Errorf("highlight output %q lacks %q", got, want) } } } func TestStaleFilesAreRemoved(t *testing.T) { out := t.TempDir() old := filepath.Join(out, "gone.html") if err := os.WriteFile(old, []byte("x"), 0o644); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(out, manifestName), []byte(`["gone.html","kept.html"]`), 0o644); err != nil { t.Fatal(err) } g := &generator{outDir: out} if err := g.write("kept.html", []byte("y")); err != nil { t.Fatal(err) } if err := g.cleanStale(); err != nil { t.Fatalf("cleanStale: %v", err) } if _, err := os.Stat(old); !os.IsNotExist(err) { t.Errorf("gone.html should have been removed") } if _, err := os.Stat(filepath.Join(out, "kept.html")); err != nil { t.Errorf("kept.html should still exist: %v", err) } }