Files
orangesite/hn/hn_test.go
sneak 3ab9637246 bring repo into policy compliance; vendor assets; Gitea CI
Vendor the front-end assets and drop the third-party CDN dependencies
(BootstrapCDN is being sunset): the bootstrap 4.0.0 css/js, jquery
3.2.1 slim, and popper 1.12.9 now live under static/ and are served
from the app, byte-for-byte identical to the previous SRI-pinned files.

Migrate CI from Drone to a Gitea Actions workflow that runs
docker build . on push, with the checkout action pinned by SHA.

Bring the repo up to standard:

- add REPO_POLICIES.md, .editorconfig, .dockerignore, .golangci.yml,
  and a comprehensive root-anchored .gitignore
- rewrite the Makefile with the required test/lint/fmt/fmt-check/check/
  docker/hooks targets (golangci-lint, 30s test timeout, verbose rerun
  on failure, check modifies nothing)
- rewrite the Dockerfile as a hash-pinned multistage build: a lint
  stage (golangci-lint), a glibc build+test stage (the legacy sqlite
  driver needs cgo+glibc), and a debian-slim runtime carrying the
  binary, templates, and static assets
- add real tests for the hn package
- bring the code into golangci-lint (default: all) compliance: fix the
  malformed gorm struct tags, check previously-ignored errors, dispatch
  the zerolog error event, avoid a uint->Duration overflow, split long
  functions, and add doc comments — all behaviour-preserving
- expand the README with the required sections
2026-07-26 23:55:21 +07:00

74 lines
1.5 KiB
Go

package hn_test
import (
"testing"
"time"
"git.eeqj.de/sneak/orangesite/hn"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func TestNewFetcher(t *testing.T) {
t.Parallel()
f := hn.NewFetcher(nil)
if f == nil {
t.Fatal("NewFetcher returned nil")
}
}
func TestNewRequestHandlerSet(t *testing.T) {
t.Parallel()
rhs := hn.NewRequestHandlerSet("v1.2.3", nil)
if rhs == nil {
t.Fatal("NewRequestHandlerSet returned nil")
}
}
// TestHNFrontPageRoundTrip exercises the gorm schema against an in-memory
// sqlite database: migrate, insert, and read back a front-page row.
func TestHNFrontPageRoundTrip(t *testing.T) {
t.Parallel()
db, err := gorm.Open("sqlite3", ":memory:")
if err != nil {
t.Fatalf("open in-memory db: %v", err)
}
defer func() { _ = db.Close() }()
err = db.AutoMigrate(&hn.HNFrontPage{}).Error
if err != nil {
t.Fatalf("automigrate: %v", err)
}
want := hn.HNFrontPage{
HNID: 42,
Appeared: time.Now(),
Disappeared: time.Time{},
HighestRank: 1,
Rank: 3,
Title: "hello world",
Score: 100,
URL: "https://example.com",
}
err = db.Create(&want).Error
if err != nil {
t.Fatalf("create: %v", err)
}
var got hn.HNFrontPage
err = db.Model(&hn.HNFrontPage{}).Where("hn_id = ?", 42).First(&got).Error
if err != nil {
t.Fatalf("query: %v", err)
}
if got.Title != want.Title || got.Score != want.Score || got.URL != want.URL {
t.Errorf("round-trip mismatch: got %+v, want title=%q score=%d url=%q",
got, want.Title, want.Score, want.URL)
}
}