Files
Pulse/scripts/installtests/native_agent_frontend_test.go
pulse-triage[bot] 5e2eea6bb4 Reject incompatible frontend locks before native builds
Pulse #4997's refreshed lock installed and built before the existing serialization check rejected it on the native Unix runners. Run the same source-only check after pinned Node setup, before npm work in the native matrix and the Windows update packet producer. Qualify changes to the checker on both PR and main, without removing any installer, runtime or recovery check for passing graphs.

Contract-Neutral: Earlier execution of the existing Solid lock check in native CI only; application behavior, runtime and installer contracts, permissions, native test coverage for admitted graphs, and release decisions are unchanged.
Change-source: pulse-maintainer
2026-11-24 21:25:34 +00:00

351 lines
12 KiB
Go

package installtests
import (
"fmt"
"os"
"strings"
"testing"
"gopkg.in/yaml.v3"
)
type nativeAgentFrontendStep struct {
Name string
If string
Uses string
With map[string]string
WorkingDirectory string `yaml:"working-directory"`
Run string
ContinueOnError bool `yaml:"continue-on-error"`
}
const (
nativeSerializationScript = "scripts/check-frontend-serialization.mjs"
nativeSerializationCheck = "Check Solid serialization dependency edges"
)
func validateNativeImagePreflight(steps []nativeAgentFrontendStep) error {
goAt, checkAt, nodeAt := -1, -1, -1
for i, step := range steps {
switch step.Name {
case "Set up Go":
goAt = i
case "Check immutable integration images before frontend preparation":
if checkAt >= 0 {
return fmt.Errorf("immutable integration image preflight must run once")
}
checkAt = i
case "Set up Node.js for native runtime test assets":
nodeAt = i
}
}
if goAt < 0 || checkAt <= goAt || nodeAt <= checkAt {
return fmt.Errorf("immutable image checks must follow Go setup and precede frontend preparation")
}
check := steps[checkAt]
if check.If != "matrix.unix" || check.ContinueOnError || check.WorkingDirectory != "" ||
strings.TrimSpace(check.Run) != "go test -count=1 -timeout 3m -run '^TestIntegrationContainer' ./scripts/installtests" {
return fmt.Errorf("immutable image preflight must execute the uncached bounded source checks on every Unix platform and retain failure")
}
return validateNativeAgentFrontendPreparation(steps)
}
func TestNativeAgentRejectsBrokenImagesBeforeFrontendPreparation(t *testing.T) {
content, err := os.ReadFile(repoFile(".github", "workflows", "unified-agent-native.yml"))
if err != nil {
t.Fatal(err)
}
var workflow struct {
Jobs map[string]struct{ Steps []nativeAgentFrontendStep }
}
if err := yaml.Unmarshal(content, &workflow); err != nil {
t.Fatal(err)
}
steps := workflow.Jobs["native-agent"].Steps
if err := validateNativeImagePreflight(steps); err != nil {
t.Fatal(err)
}
checkAt, nodeAt := -1, -1
for i, step := range steps {
switch step.Name {
case "Check immutable integration images before frontend preparation":
checkAt = i
case "Set up Node.js for native runtime test assets":
nodeAt = i
}
}
for _, scenario := range []string{"missing", "late", "before Go", "Linux only", "hidden failure", "cached", "empty selection", "stub assets", "missing real build"} {
t.Run(scenario, func(t *testing.T) {
changed := append([]nativeAgentFrontendStep(nil), steps...)
switch scenario {
case "missing":
changed = append(changed[:checkAt], changed[checkAt+1:]...)
case "late":
changed[checkAt], changed[nodeAt] = changed[nodeAt], changed[checkAt]
case "before Go":
changed[checkAt], changed[checkAt-1] = changed[checkAt-1], changed[checkAt]
case "Linux only":
changed[checkAt].If = "runner.os == 'Linux'"
case "hidden failure":
changed[checkAt].ContinueOnError = true
case "cached":
changed[checkAt].Run = strings.Replace(changed[checkAt].Run, "-count=1 ", "", 1)
case "empty selection":
changed[checkAt].Run = strings.Replace(changed[checkAt].Run, "^TestIntegrationContainer", "^TestNonexistent", 1)
case "stub assets", "missing real build":
for i, step := range changed {
if step.Name == "Build real frontend assets for API-linked native tests" {
if scenario == "stub assets" {
changed[i].Run = "touch index.html"
} else {
changed = append(changed[:i], changed[i+1:]...)
}
break
}
}
}
if err := validateNativeImagePreflight(changed); err == nil {
t.Fatalf("accepted %s immutable-image preflight", scenario)
}
})
}
}
func validateNativeAgentFrontendTriggers(content []byte) error {
var workflow struct {
On map[string]struct{ Paths []string }
}
if err := yaml.Unmarshal(content, &workflow); err != nil {
return err
}
for _, event := range []string{"push", "pull_request"} {
paths := workflow.On[event].Paths
for _, required := range []string{"frontend-modern/package.json", "frontend-modern/package-lock.json", nativeSerializationScript} {
found := false
for _, pattern := range paths {
if strings.HasPrefix(pattern, "!") {
return fmt.Errorf("%s must not exclude native frontend inputs", event)
}
found = found || pattern == required
}
if !found {
return fmt.Errorf("%s must admit native frontend graph changes in %s", event, required)
}
}
}
return nil
}
func TestNativeAgentWorkflowAdmitsFrontendGraphChanges(t *testing.T) {
content, err := os.ReadFile(repoFile(".github", "workflows", "unified-agent-native.yml"))
if err != nil {
t.Fatal(err)
}
if err := validateNativeAgentFrontendTriggers(content); err != nil {
t.Fatal(err)
}
// Decode each event, rather than accepting a matching path elsewhere in
// the file. Both PR qualification and containing-main checks need it.
for _, event := range []string{"push", "pull_request"} {
for _, required := range []string{"frontend-modern/package.json", "frontend-modern/package-lock.json", nativeSerializationScript} {
t.Run(event+" missing "+required, func(t *testing.T) {
var workflow map[string]interface{}
if err := yaml.Unmarshal(content, &workflow); err != nil {
t.Fatal(err)
}
trigger := workflow["on"].(map[string]interface{})[event].(map[string]interface{})
paths := trigger["paths"].([]interface{})
var changed []interface{}
for _, pattern := range paths {
if pattern != required {
changed = append(changed, pattern)
}
}
if len(changed) != len(paths)-1 {
t.Fatal("control must remove exactly one original input")
}
trigger["paths"] = changed
mutant, err := yaml.Marshal(workflow)
if err != nil {
t.Fatal(err)
}
if err := validateNativeAgentFrontendTriggers(mutant); err == nil {
t.Fatal("accepted frontend graph change without native verification")
}
})
}
t.Run(event+" excluded frontend graph", func(t *testing.T) {
var workflow map[string]interface{}
if err := yaml.Unmarshal(content, &workflow); err != nil {
t.Fatal(err)
}
trigger := workflow["on"].(map[string]interface{})[event].(map[string]interface{})
trigger["paths"] = append(trigger["paths"].([]interface{}), "!frontend-modern/**")
mutant, err := yaml.Marshal(workflow)
if err != nil {
t.Fatal(err)
}
if err := validateNativeAgentFrontendTriggers(mutant); err == nil {
t.Fatal("accepted a later filter excluding the native frontend graph")
}
})
}
}
func validateNativeSerializationPreflight(steps []nativeAgentFrontendStep, setup, install string) error {
indices := make(map[string]int)
for i, step := range steps {
switch step.Name {
case setup, nativeSerializationCheck, install:
if _, found := indices[step.Name]; found {
return fmt.Errorf("native serialization preflight step must run once: %s", step.Name)
}
indices[step.Name] = i
}
}
setupAt, hasSetup := indices[setup]
checkAt, hasCheck := indices[nativeSerializationCheck]
installAt, hasInstall := indices[install]
if !hasSetup || !hasCheck || !hasInstall || !(setupAt < checkAt && checkAt < installAt) {
return fmt.Errorf("check the serialization graph after Node setup and before installing it")
}
check := steps[checkAt]
if check.If != "" || check.ContinueOnError || check.WorkingDirectory != "" ||
check.Uses != "" || strings.TrimSpace(check.Run) != "node "+nativeSerializationScript {
return fmt.Errorf("the existing serialization check must gate every consumer on its own checkout")
}
return nil
}
func TestNativeFrontendSerializationPreflight(t *testing.T) {
content, err := os.ReadFile(repoFile(".github", "workflows", "unified-agent-native.yml"))
if err != nil {
t.Fatal(err)
}
var workflow struct {
Jobs map[string]struct{ Steps []nativeAgentFrontendStep }
}
if err := yaml.Unmarshal(content, &workflow); err != nil {
t.Fatal(err)
}
for _, consumer := range []struct{ job, setup, install string }{
{"native-agent", "Set up Node.js for native runtime test assets", "Install frontend test dependencies"},
{"windows-update-producer", "Set up Node for real API embed", "Build real frontend assets"},
} {
t.Run(consumer.job, func(t *testing.T) {
steps := workflow.Jobs[consumer.job].Steps
if err := validateNativeSerializationPreflight(steps, consumer.setup, consumer.install); err != nil {
t.Fatal(err)
}
checkAt, installAt := -1, -1
for i, step := range steps {
switch step.Name {
case nativeSerializationCheck:
checkAt = i
case consumer.install:
installAt = i
}
}
for _, scenario := range []string{"missing", "duplicate", "after npm", "conditional", "ignored failure", "different lock"} {
t.Run(scenario, func(t *testing.T) {
changed := append([]nativeAgentFrontendStep(nil), steps...)
switch scenario {
case "missing":
changed = append(changed[:checkAt], changed[checkAt+1:]...)
case "duplicate":
changed = append(changed, changed[checkAt])
case "after npm":
changed[checkAt], changed[installAt] = changed[installAt], changed[checkAt]
case "conditional":
changed[checkAt].If = "matrix.unix"
case "ignored failure":
changed[checkAt].ContinueOnError = true
case "different lock":
changed[checkAt].Run += " --lockfile package-lock.json"
}
if err := validateNativeSerializationPreflight(changed, consumer.setup, consumer.install); err == nil {
t.Fatalf("accepted %s serialization preflight", scenario)
}
})
}
})
}
}
func validateNativeAgentFrontendPreparation(steps []nativeAgentFrontendStep) error {
indices := map[string]int{}
for i, step := range steps {
indices[step.Name] = i
}
names := []string{
"Set up Node.js for native runtime test assets",
"Install frontend test dependencies",
"Build real frontend assets for API-linked native tests",
"Test native agent runtime",
}
previous := -1
for _, name := range names {
i, ok := indices[name]
if !ok || i <= previous || steps[i].If != "" {
return fmt.Errorf("%s must cover every native platform, in preparation order", name)
}
previous = i
}
node, dependencies, build := steps[indices[names[0]]], steps[indices[names[1]]], steps[indices[names[2]]]
if node.Uses != "actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1" ||
node.With["node-version"] != "24" || node.With["cache"] != "npm" ||
node.With["cache-dependency-path"] != "frontend-modern/package-lock.json" {
return fmt.Errorf("native frontend preparation must use the pinned Node tool and locked cache")
}
if dependencies.WorkingDirectory != "frontend-modern" || strings.TrimSpace(dependencies.Run) != "npm ci" ||
build.WorkingDirectory != "frontend-modern" || strings.TrimSpace(build.Run) != "npm run build" {
return fmt.Errorf("native tests need the locked real frontend build, not placeholder assets")
}
return nil
}
func TestNativeAgentWorkflowBuildsRealFrontendBeforeRuntime(t *testing.T) {
content, err := os.ReadFile(repoFile(".github", "workflows", "unified-agent-native.yml"))
if err != nil {
t.Fatal(err)
}
var workflow struct {
Jobs map[string]struct{ Steps []nativeAgentFrontendStep }
}
if err := yaml.Unmarshal(content, &workflow); err != nil {
t.Fatal(err)
}
steps := workflow.Jobs["native-agent"].Steps
if err := validateNativeAgentFrontendPreparation(steps); err != nil {
t.Fatal(err)
}
for _, scenario := range []string{"missing build", "Windows-only preparation", "late build", "stub build"} {
t.Run(scenario, func(t *testing.T) {
changed := append([]nativeAgentFrontendStep(nil), steps...)
buildIndex, runtimeIndex := -1, -1
for i, step := range changed {
switch step.Name {
case "Build real frontend assets for API-linked native tests":
buildIndex = i
case "Test native agent runtime":
runtimeIndex = i
}
}
switch scenario {
case "missing build":
changed = append(changed[:buildIndex], changed[buildIndex+1:]...)
case "Windows-only preparation":
changed[buildIndex].If = "${{ !matrix.unix }}"
case "late build":
changed[buildIndex], changed[runtimeIndex] = changed[runtimeIndex], changed[buildIndex]
case "stub build":
changed[buildIndex].Run = "mkdir -p ../internal/api/frontend-modern/dist && touch ../internal/api/frontend-modern/dist/index.html"
}
if err := validateNativeAgentFrontendPreparation(changed); err == nil {
t.Fatalf("accepted %s instead of real all-platform preparation", scenario)
}
})
}
}