test(testrun): cover prepareBundleInputs aliases and missing-runtime error

This commit is contained in:
pj committed 2026-06-06 13:06:40 +05:30
1 parent 5ea68869ae
commit 2e06555714
1 file changed
+63 -7
+63 -7
View File
@@ -3,7 +3,7 @@ package testrun
import (
"os"
"path/filepath"
"strconv"
"strings"
"testing"
)
@@ -19,12 +19,68 @@ func TestResolveSeed_DerivesWhenZero(t *testing.T) {
}
}
// TestSeedDefineFormatsAsDecimal guards the contract that Execute uses to put
// the seed into the web bundle: strconv.FormatInt base 10, which web-runtime
// folds to a 32-bit PRNG seed.
func TestSeedDefineFormatsAsDecimal(t *testing.T) {
if got := strconv.FormatInt(resolveSeed(8675309), 10); got != "8675309" {
t.Fatalf("got %q, want 8675309", got)
func TestPrepareBundleInputs_MissingGojaRuntime(t *testing.T) {
root := t.TempDir()
specPath := filepath.Join(root, "spec.ts")
if err := os.WriteFile(specPath, []byte(""), 0o644); err != nil {
t.Fatal(err)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = os.Chdir(cwd) })
if err := os.Chdir(root); err != nil {
t.Fatal(err)
}
_, err = prepareBundleInputs(Options{Spec: specPath})
if err == nil || !strings.Contains(err.Error(), "goja-runtime.ts not found") ||
!strings.Contains(err.Error(), "checkout pkg/spec or set @sanderling/spec alias") {
t.Fatalf("got %v, want documented goja-runtime error", err)
}
}
func TestPrepareBundleInputs_DerivesSpecAliases(t *testing.T) {
root := t.TempDir()
srcDir := filepath.Join(root, "pkg", "spec", "src")
if err := os.MkdirAll(srcDir, 0o755); err != nil {
t.Fatal(err)
}
apiPath := filepath.Join(srcDir, "index.ts")
gojaPath := filepath.Join(srcDir, "goja-runtime.ts")
for _, p := range []string{apiPath, gojaPath} {
if err := os.WriteFile(p, []byte("export {}"), 0o644); err != nil {
t.Fatal(err)
}
}
specPath := filepath.Join(root, "examples", "spec.ts")
if err := os.MkdirAll(filepath.Dir(specPath), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(specPath, []byte(""), 0o644); err != nil {
t.Fatal(err)
}
prep, err := prepareBundleInputs(Options{Spec: specPath})
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"@sanderling/spec": apiPath,
"@sanderling/spec/defaults": filepath.Join(srcDir, "defaults/index.ts"),
"@sanderling/spec/defaults/properties": filepath.Join(srcDir, "defaults/properties.ts"),
}
for key, wantValue := range want {
if prep.aliases[key] != wantValue {
t.Errorf("alias %q = %q, want %q", key, prep.aliases[key], wantValue)
}
}
if len(prep.aliases) != len(want) {
t.Errorf("got %d aliases, want %d: %v", len(prep.aliases), len(want), prep.aliases)
}
if prep.gojaRuntimePath != gojaPath {
t.Errorf("gojaRuntimePath = %q, want %q", prep.gojaRuntimePath, gojaPath)
}
}