go-html/cmd/wasm/size_test.go
Snider 6e59bf8bf8
All checks were successful
Security Scan / security (push) Successful in 7s
Test / test (push) Successful in 1m33s
refactor(conventions): replace os.ReadFile with coreio.Local, fmt.Errorf with log.E
Replace os.ReadFile in cmd/wasm/size_test.go with coreio.Local.Read().
Replace fmt.Errorf/errors.New with log.E() in codegen, cmd/wasm/register,
and cmd/codegen. Add forge.lthn.ai/core/go-io as a dependency.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-16 19:17:34 +00:00

54 lines
1.3 KiB
Go

// SPDX-Licence-Identifier: EUPL-1.2
//go:build !js
package main
import (
"bytes"
"compress/gzip"
"os"
"os/exec"
"path/filepath"
"testing"
coreio "forge.lthn.ai/core/go-io"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
wasmGzLimit = 1_048_576 // 1 MB gzip transfer size limit
wasmRawLimit = 3_670_016 // 3.5 MB raw size limit
)
func TestWASMBinarySize_Good(t *testing.T) {
if testing.Short() {
t.Skip("skipping WASM build test in short mode")
}
dir := t.TempDir()
out := filepath.Join(dir, "gohtml.wasm")
cmd := exec.Command("go", "build", "-ldflags=-s -w", "-o", out, ".")
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm")
output, err := cmd.CombinedOutput()
require.NoError(t, err, "WASM build failed: %s", output)
rawStr, err := coreio.Local.Read(out)
require.NoError(t, err)
raw := []byte(rawStr)
var buf bytes.Buffer
gz, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
require.NoError(t, err)
_, err = gz.Write(raw)
require.NoError(t, err)
require.NoError(t, gz.Close())
t.Logf("WASM size: %d bytes raw, %d bytes gzip", len(raw), buf.Len())
assert.Less(t, buf.Len(), wasmGzLimit,
"WASM gzip size %d exceeds 1MB limit", buf.Len())
assert.Less(t, len(raw), wasmRawLimit,
"WASM raw size %d exceeds 3MB limit", len(raw))
}