motorino/internal/build/date_test.go
tgiachi 26edf78449 feat(news): hide drafts and future posts, give the news one language
- Draft: true and a date in the future keep a post out of the pages, the lists,
  the categories and the feed. motorino build -drafts (next commit) shows them.
- news_language in site.json, and an optional Language key in a post, choose the
  language of the news pages and of the interface strings they use. The news
  still exist once, not once per language.
- New header keys Mastodon (an https address), Draft and Language; a post knows
  its reading time and whether its description was written.

Refs #9
2026-11-16 12:04:50 +01:00

73 lines
2.6 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"testing"
"time"
"git.kmalloc.org/squid/motorino/internal/site"
)
// dateLang returns a language with the strings that write a date.
func dateLang(layout string, months ...string) site.Lang {
l := site.Lang{Code: "xx", Strings: map[string]string{}}
if layout != "" {
l.Strings["date_long"] = layout
}
for i, m := range months {
l.Strings["month_"+string(rune('0'+(i+1)/10))+string(rune('0'+(i+1)%10))] = m
}
return l
}
// allMonths lists twelve month names for a test language.
var allMonths = []string{"gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"}
// TestLongDate checks the long date in a language, as written in the date.
func TestLongDate(t *testing.T) {
date, err := time.Parse("2006-01-02T15:04:05-0700", "2026-11-05T23:30:00-0500")
if err != nil {
t.Fatal(err)
}
if got := longDate(date, dateLang("{d} {month} {y}", allMonths...)); got != "5 novembre 2026" {
t.Errorf("longDate() = %q, want the day without a zero, the month name and the year", got)
}
if got := longDate(date, dateLang("{month} {d}, {y}", allMonths...)); got != "novembre 5, 2026" {
t.Errorf("longDate() with another layout = %q", got)
}
}
// TestLongDateFallsBack checks the ISO date when the language has no layout or a month.
func TestLongDateFallsBack(t *testing.T) {
date, _ := time.Parse("2006-01-02T15:04:05-0700", "2026-11-14T12:00:00+0100")
if got := longDate(date, dateLang("")); got != "2026-11-14" {
t.Errorf("no layout: longDate() = %q, want the ISO date", got)
}
if got := longDate(date, dateLang("{d} {month} {y}", "uno", "due")); got != "2026-11-14" {
t.Errorf("a missing month name: longDate() = %q, want the ISO date", got)
}
}
// TestMonthYear checks the label of an archive month.
func TestMonthYear(t *testing.T) {
l := dateLang("{d} {month} {y}", allMonths...)
l.Strings["date_month_year"] = "{month} {y}"
if got := monthYear(2026, 11, l); got != "novembre 2026" {
t.Errorf("monthYear() = %q", got)
}
if got := monthYear(2026, 3, dateLang("")); got != "2026-03" {
t.Errorf("monthYear() without strings = %q, want 2026-03", got)
}
}
// TestReadingText checks the reading time sentence and its absence.
func TestReadingText(t *testing.T) {
l := site.Lang{Strings: map[string]string{"reading_time": "{n} min di lettura"}}
if got := readingText(3, l); got != "3 min di lettura" {
t.Errorf("readingText() = %q", got)
}
if got := readingText(3, site.Lang{Strings: map[string]string{}}); got != "" {
t.Errorf("readingText() without a string = %q, want empty", got)
}
}