go-html/cmd/codegen/main_test.go
Snider 4c657377c4 fix(wasm): reduce binary from 1.56MB to 830KB gzip
Move heavyweight imports out of WASM build via //go:build !js tags:
- register.go: encoding/json + text/template (codegen)
- pipeline.go: go-i18n/reversal (grammar imprint)
- layout.go: replace fmt.Sprintf with string concat

Add cmd/codegen/ CLI as build-time replacement for in-browser
registerComponents(). Reads slot JSON from stdin, writes WC bundle
JS to stdout.

Result: 2.90MB raw / 830KB gzip (was 6.04MB / 1.56MB).
Makefile gate passes (limit 1MB gzip).

Co-Authored-By: Virgil <virgil@lethean.io>
2026-02-20 08:29:54 +00:00

51 lines
1.1 KiB
Go

package main
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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())
}