test: cover web-runtime seeded PRNG, weighted pick, and seed define wiring

This commit is contained in:
pj committed 2026-06-01 15:10:04 +05:30
1 parent 6fc1c971c7
commit d4ffdce8f9
3 files changed
+123

No files matched your search

+32
View File
@@ -122,6 +122,38 @@ func TestBundleWeb_IsDeterministic(t *testing.T) {
}
}
// TestBundleWeb_InjectsSeedDefine asserts that a SANDERLING_SEED define is
// inlined into the bundle so --seed reaches the web runtime PRNG. esbuild
// replaces process.env.SANDERLING_SEED with the quoted literal at build time.
func TestBundleWeb_InjectsSeedDefine(t *testing.T) {
directory := t.TempDir()
runtimePath := filepath.Join(directory, "web-runtime.ts")
specPath := filepath.Join(directory, "spec.ts")
runtime := fakeRuntime + "\n(globalThis as Record<string, unknown>).__seed = process.env.SANDERLING_SEED;\n"
if err := os.WriteFile(runtimePath, []byte(runtime), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(specPath, []byte(fakeSpec), 0o644); err != nil {
t.Fatal(err)
}
result, err := BundleWeb(WebOptions{
EntryFile: specPath,
WebRuntimeFile: runtimePath,
Defines: map[string]string{"SANDERLING_SEED": "8675309"},
})
if err != nil {
t.Fatalf("BundleWeb: %v", err)
}
source := string(result.JavaScript)
if !strings.Contains(source, "8675309") {
t.Errorf("seed literal not inlined into bundle\nsource head:\n%s", head(source, 500))
}
if strings.Contains(source, "process.env.SANDERLING_SEED") {
t.Errorf("define left unsubstituted in bundle")
}
}
func head(text string, max int) string {
if len(text) <= max {
return text
+22
View File
@@ -3,9 +3,31 @@ package testrun
import (
"os"
"path/filepath"
"strconv"
"testing"
)
func TestResolveSeed_UsesConfiguredWhenNonZero(t *testing.T) {
if got := resolveSeed(42); got != 42 {
t.Fatalf("got %d, want 42", got)
}
}
func TestResolveSeed_DerivesWhenZero(t *testing.T) {
if got := resolveSeed(0); got == 0 {
t.Fatal("expected a non-zero time-derived seed")
}
}
// 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 TestResolveSpecAPIPath_FindsUpwardSibling(t *testing.T) {
root := t.TempDir()
apiPath := filepath.Join(root, "pkg", "spec", "src", "index.ts")