go-html/cmd/codegen/main_test.go
Snider 023c8db79d
All checks were successful
Security Scan / security (pull_request) Successful in 8s
Test / test (pull_request) Successful in 48s
fix(dx): update CLAUDE.md and improve test coverage
- Fix WASM size gate: 3 MB → 3.5 MB to match size_test.go
- Fix dependencies: remove stale replace directive, add go-io and go-log
- Add error handling and I/O conventions to coding standards
- Add tests: reader error (cmd/codegen), duplicate tag dedup, bundle
  error propagation (codegen) — coverage: codegen 89→96%, cmd/codegen
  71→78%

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-17 08:27:58 +00:00

66 lines
1.4 KiB
Go

package main
import (
"bytes"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type errReader struct{}
func (errReader) Read([]byte) (int, error) {
return 0, errors.New("read failed")
}
func TestRun_Good(t *testing.T) {
input := strings.NewReader(`{"H":"nav-bar","C":"main-content"}`)
var output bytes.Buffer
err := run(input, &output)
require.NoError(t, err)
js := output.String()
assert.Contains(t, js, "NavBar")
assert.Contains(t, js, "MainContent")
assert.Contains(t, js, "customElements.define")
assert.Equal(t, 2, strings.Count(js, "extends HTMLElement"))
}
func TestRun_Bad_InvalidJSON(t *testing.T) {
input := strings.NewReader(`not json`)
var output bytes.Buffer
err := run(input, &output)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid JSON")
}
func TestRun_Bad_InvalidTag(t *testing.T) {
input := strings.NewReader(`{"H":"notag"}`)
var output bytes.Buffer
err := run(input, &output)
assert.Error(t, err)
assert.Contains(t, err.Error(), "hyphen")
}
func TestRun_Good_Empty(t *testing.T) {
input := strings.NewReader(`{}`)
var output bytes.Buffer
err := run(input, &output)
require.NoError(t, err)
assert.Empty(t, output.String())
}
func TestRun_Bad_ReadError(t *testing.T) {
var output bytes.Buffer
err := run(errReader{}, &output)
assert.Error(t, err)
assert.Contains(t, err.Error(), "reading stdin")
}