Skip to content

[Test Improver] Add normalizeArch tests and expand SanitizeInstance table-driven tests #35

@github-actions

Description

@github-actions

🤖 This was created by Test Improver, an automated AI assistant focused on improving tests for this repository.

Goal and Rationale

Two pure functions still lacked thorough direct test coverage:

  • normalizeArch (internal/host/detect.go): maps raw uname -m / PowerShell arch strings to "amd64" or "arm64". Incorrect normalization silently causes wrong runner binaries — a critical path. Previously zero direct tests.
  • SanitizeInstance (internal/autostart/sanitize.go): sanitizes runner names for systemd/launchd/Windows scheduled task names. Existing test had only 3 cases; edge conditions (consecutive-special collapse, leading/trailing stripping, empty-result errors) were uncovered.

Note: This supersedes issues #23, #26, and #29 (earlier failed attempts where git push failed). The test logic is equivalent but refined.

Approach

internal/host/host_test.go — adds Test_normalizeArch (11 cases):

  • All 4 valid inputs: x86_64, amd64, aarch64, arm64
  • Case-insensitive variants: X86_64, AMD64, AARCH64, ARM64
  • Error cases: i386, UNKNOWN, empty string

internal/autostart/sanitize_test.go — rewrites TestSanitizeInstance (15 cases):

  • Basic valid inputs, underscores/spaces/@ → dashes
  • Consecutive special chars collapse (a___ba-b)
  • Leading/trailing stripping (_runner_runner)
  • Error cases: ---, ___, empty string

Reproducibility

go test ./internal/host/... -run Test_normalizeArch -v
go test ./internal/autostart/... -run TestSanitizeInstance -v

To create a pull request with the changes:

# Download the artifact from the workflow run
gh run download 24566929637 -n agent -D /tmp/agent-24566929637

# Create a new branch
git checkout -b test-assist/normalizeArch-sanitize-2026-04-17

# Apply the patch (--3way handles cross-repo patches where files may already exist)
git am --3way /tmp/agent-24566929637/aw-test-assist-normalizearch-sanitize-2026-04-17.patch

# Push the branch to origin
git push origin test-assist/normalizeArch-sanitize-2026-04-17

# Create the pull request
gh pr create --title '[Test Improver] Add normalizeArch tests and expand SanitizeInstance table-driven tests' --base main --head test-assist/normalizeArch-sanitize-2026-04-17 --repo an-lee/gh-sr
Show patch (141 lines)
From 7562910e4e3334bb8091d3e92c2072ab6b763436 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]`@users`.noreply.github.com>
Date: Fri, 17 Apr 2026 13:17:24 +0000
Subject: [PATCH] test: add normalizeArch tests and expand SanitizeInstance
 table-driven tests

- Add Test_normalizeArch in internal/host/host_test.go (11 cases):
  all valid arch strings (x86_64/amd64/aarch64/arm64), uppercase variants,
  and error cases for unsupported arch and empty input
- Expand TestSanitizeInstance in internal/autostart/sanitize_test.go to
  table-driven with 15 cases: basic valid, special-char replacement,
  consecutive-special collapse, leading/trailing strip, and error cases

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 internal/autostart/sanitize_test.go | 53 +++++++++++++++++++++++------
 internal/host/host_test.go          | 43 +++++++++++++++++++++++
 2 files changed, 86 insertions(+), 10 deletions(-)

diff --git a/internal/autostart/sanitize_test.go b/internal/autostart/sanitize_test.go
index 2ef4243..d4dd840 100644
--- a/internal/autostart/sanitize_test.go
+++ b/internal/autostart/sanitize_test.go
@@ -4,16 +4,49 @@ import "testing"
 
 func TestSanitizeInstance(t *testing.T) {
 	t.Parallel()
-	got, err := SanitizeInstance("ci-1")
-	if err != nil || got != "ci-1" {
-		t.Fatalf("ci-1: got %q err %v", got, err)
+	cases := []struct {
+		input   string
+		want    string
+		wantErr bool
+	}{
+		// Basic valid inputs pass through unchanged
+		{"ci-1", "ci-1", false},
+		{"runner", "runner", false},
+		{"UPPER", "UPPER", false},
+		{"123", "123", false},
+		// Special characters replaced with dashes
+		{"my_runner-2", "my-runner-2", false},
+		{"hello world", "hello-world", false},
+		{"a@b", "a-b", false},
+		// Consecutive special chars collapse to a single dash
+		{"a___b", "a-b", false},
+		{"a..b", "a-b", false},
+		{"a__b__c", "a-b-c", false},
+		// Leading/trailing special chars are stripped
+		{"_runner_", "runner", false},
+		{".ci.", "ci", false},
+		// Error cases: all chars sanitize away to empty
+		{"---", "", true},
+		{"___", "", true},
+		{"", "", true},
 	}
-	got, err = SanitizeInstance("my_runner-2")
-	if err != nil || got != "my-runner-2" {
-		t.Fatalf("my_runner-2: got %q err %v", got, err)
-	}
-	_, err = SanitizeInstance("---")
-	if err == nil {
-		t.Fatal("expected error for empty after trim")
+	for _, tc := range cases {
+		t.Run(tc.input, func(t *testing.T) {
+			t.Parallel()
+			got, err := SanitizeInstance(tc.input)
+			if tc.wantErr {
+				if err == nil {
+					t.Errorf("SanitizeInstance(%q): expected error, got %q", tc.input, got)
+				}
+				return
+			}
+			if err != nil {
+				t.Errorf("SanitizeInstance(%q): unexpected error: %v", tc.input, err)
+				return
+			}
+			if got != tc.want {
+				t.Errorf("SanitizeInstance(%q) = %q, want %q", tc.input, got, tc.want)
+			}
+		})
 	}
 }
diff --git a/internal/host/host_test.go b/internal/host/host_test.go
index da80c38..d0c7737 100644
--- a/internal/host/host_test.go
+++ b/internal/host/host_test.go
@@ -9,6 +9,49 @@ import (
 	"github.com/an-lee/gh-sr/internal/config"
 )
 
+func Test_normalizeArch(t *testing.T) {
+	t.Parallel()
+	cases := []struct {
+		input   string
+		want    string
+		wantErr bool
+	}{
+		// Valid amd64 inputs
+		{"x86_64", "amd64", false},
+		{"amd64", "amd64", false},
+		{"X86_64", "amd64", false},
+		{"AMD64", "amd64", false},
+		// Valid arm64 inputs
+		{"aarch64", "arm64", false},
+		{"arm64", "arm64", false},
+		{"AARCH64", "arm64", false},
+		{"ARM64", "arm64", false},
+		// Error cases
+		{"i386", "", true},
+		{"UNKNOWN", "", true},
+		{"", "", true},
+	}
+	for _, tc := range cases {
+		t.Run(tc.input, func(t *testing.T) {
+			t.Parallel()
+			got, err := normalizeArch(tc.input)
+			if tc.wantErr {
+				if err == nil {
+					t.Errorf("normalizeArch(%q): expected error, got %q", tc.input, got)
+				}
+				return
+			}
+			if err != nil {
+				t.Errorf("normalizeArch(%q): unexpected error: %v", tc.input, err)
+				return
+			}
+			if got != tc.want {
+				t.Errorf("normalizeArch(%q) = %q, want %q", tc.input, got, tc.want)
+			}
+		})
+	}
+}
+
 func Test_parseAddr(t *testing.T) {
 	t.Parallel()
 	u, a := parseAddr("user@host.example:2222")
-- 
2.53.0

Generated by Daily Test Improver · ● 1.7M ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@97143ac59cb3a13ef2a77581f929f06719c7402a

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions