From 2e06555714ddf57aeed2fdbd2ff222520ef04f18 Mon Sep 17 00:00:00 2001 From: PJ Date: Sat, 6 Jun 2026 12:45:55 +0530 Subject: [PATCH] test(testrun): cover prepareBundleInputs aliases and missing-runtime error --- internal/testrun/testrun_test.go | 70 ++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/internal/testrun/testrun_test.go b/internal/testrun/testrun_test.go index 5cd63bf..bd6eee6 100644 --- a/internal/testrun/testrun_test.go +++ b/internal/testrun/testrun_test.go @@ -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) } }