fix(handlers): remove duplicate test declarations — sync main with staging #992

Merged
devops-engineer merged 13 commits from fix/983-remove-duplicate-test-declarations into main 2026-05-14 12:17:03 +00:00
7 changed files with 457 additions and 331 deletions

View File

@ -32,8 +32,9 @@ func setupTestDBForQueueTests(t *testing.T) sqlmock.Sqlmock {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { mockDB.Close() })
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
return mock
}

View File

@ -388,9 +388,13 @@ func TestActivityList_BeforeTSRejectsInvalidFormat(t *testing.T) {
// ---------- Activity type allowlist (#125: memory_write added) ----------
func TestActivityReport_AcceptsMemoryWriteType(t *testing.T) {
mockDB, mock, _ := sqlmock.New()
defer mockDB.Close()
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
mock.ExpectExec(`INSERT INTO activity_logs`).
WillReturnResult(sqlmock.NewResult(1, 1))
@ -413,9 +417,13 @@ func TestActivityReport_AcceptsMemoryWriteType(t *testing.T) {
}
func TestActivityReport_RejectsUnknownType(t *testing.T) {
mockDB, _, _ := sqlmock.New()
defer mockDB.Close()
mockDB, _, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
broadcaster := newTestBroadcaster()
handler := NewActivityHandler(broadcaster)
@ -447,9 +455,13 @@ func TestNotify_PersistsToActivityLogsForReloadRecovery(t *testing.T) {
// - Have source_id NULL (canvas-source filter)
// - Carry the message text in response_body so extractResponseText
// can reconstruct the agent reply on reload
mockDB, mock, _ := sqlmock.New()
defer mockDB.Close()
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
// Workspace existence check
mock.ExpectQuery(`SELECT name FROM workspaces`).
@ -491,9 +503,13 @@ func TestNotify_WithAttachments_PersistsFilePartsForReload(t *testing.T) {
// download chips after a page reload. Without `parts`, the bubble
// shows up but the attachment chip is silently dropped on every
// refresh.
mockDB, mock, _ := sqlmock.New()
defer mockDB.Close()
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
mock.ExpectQuery(`SELECT name FROM workspaces`).
WithArgs("ws-attach").
@ -565,9 +581,13 @@ func TestNotify_RejectsAttachmentWithEmptyURIOrName(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
mockDB, _, _ := sqlmock.New()
defer mockDB.Close()
mockDB, _, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
// No DB expectations — handler must reject with 400 BEFORE
// reaching SELECT/INSERT. sqlmock will fail "expectations not met"
// only if the handler unexpectedly queries.
@ -612,9 +632,13 @@ func TestNotify_DBFailure_StillBroadcastsAnd200(t *testing.T) {
// WebSocket push (which the user is already seeing in their open
// canvas). Pre-fix the WS push always succeeded; we don't want
// the new persistence step to regress that path.
mockDB, mock, _ := sqlmock.New()
defer mockDB.Close()
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
mock.ExpectQuery(`SELECT name FROM workspaces`).
WithArgs("ws-x").

View File

@ -10,23 +10,25 @@ import (
"time"
"github.com/DATA-DOG/go-sqlmock"
"github.com/Molecule-AI/molecule-monorepo/platform/internal/db"
)
// ---------- listDelegationsFromLedger ----------
// Columns in the delegations table (SELECT order must match the query).
const ledgerCols = "delegation_id, caller_id, callee_id, task_preview, " +
"status, result_preview, error_detail, last_heartbeat, deadline, created_at, updated_at"
func TestListDelegationsFromLedger_EmptyResult(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
rows := sqlmock.NewRows([]string{})
rows := sqlmock.NewRows([]string{
"delegation_id", "caller_id", "callee_id", "task_preview",
"status", "result_preview", "error_detail",
"last_heartbeat", "deadline", "created_at", "updated_at",
})
mock.ExpectQuery("SELECT .+ FROM delegations").
WithArgs("ws-1").
WillReturnRows(rows)
@ -49,11 +51,19 @@ func TestListDelegationsFromLedger_SingleRow(t *testing.T) {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
now := time.Now()
rows := sqlmock.NewRows([]string{}).AddRow(
// Use time.Time{} for nullable *time.Time columns — sqlmock passes the
// zero value to the handler's scan destination. The handler checks Valid
// before using each nullable field, so zero values are safe.
rows := sqlmock.NewRows([]string{
"delegation_id", "caller_id", "callee_id", "task_preview",
"status", "result_preview", "error_detail",
"last_heartbeat", "deadline", "created_at", "updated_at",
}).AddRow(
"del-1", "ws-1", "ws-2", "summarise the report",
"completed", "the report is about Q1",
"", now, now, now, now,
@ -102,11 +112,16 @@ func TestListDelegationsFromLedger_MultipleRows(t *testing.T) {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
now := time.Now()
rows := sqlmock.NewRows([]string{}).
rows := sqlmock.NewRows([]string{
"delegation_id", "caller_id", "callee_id", "task_preview",
"status", "result_preview", "error_detail",
"last_heartbeat", "deadline", "created_at", "updated_at",
}).
AddRow("del-a", "ws-1", "ws-2", "task a", "in_progress", "", "", now, now, now, now).
AddRow("del-b", "ws-1", "ws-3", "task b", "failed", "", "timeout", now, now, now, now).
AddRow("del-c", "ws-1", "ws-4", "task c", "completed", "result c", "", now, now, now, now)
@ -130,57 +145,15 @@ func TestListDelegationsFromLedger_MultipleRows(t *testing.T) {
}
}
func TestListDelegationsFromLedger_NullsOmitted(t *testing.T) {
// last_heartbeat, deadline, result_preview, error_detail are all NULL.
// Handler must not panic and must omit those keys from the map.
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
db.DB = mockDB
now := time.Now()
rows := sqlmock.NewRows([]string{}).
AddRow("del-1", "ws-1", "ws-2", "task", "queued", nil, nil, nil, nil, now, now)
mock.ExpectQuery("SELECT .+ FROM delegations").
WithArgs("ws-1").
WillReturnRows(rows)
broadcaster := newTestBroadcaster()
wh := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
dh := NewDelegationHandler(wh, broadcaster)
got := dh.listDelegationsFromLedger(context.Background(), "ws-1")
if len(got) != 1 {
t.Fatalf("expected 1 entry, got %d", len(got))
}
e := got[0]
if _, ok := e["last_heartbeat"]; ok {
t.Error("last_heartbeat should be absent when NULL")
}
if _, ok := e["deadline"]; ok {
t.Error("deadline should be absent when NULL")
}
if _, ok := e["response_preview"]; ok {
t.Error("response_preview should be absent when NULL result_preview")
}
if _, ok := e["error"]; ok {
t.Error("error should be absent when NULL error_detail")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("sqlmock expectations: %v", err)
}
}
func TestListDelegationsFromLedger_QueryError(t *testing.T) {
// Query failure returns nil — graceful fallback, no panic.
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
mock.ExpectQuery("SELECT .+ FROM delegations").
WithArgs("ws-1").
@ -200,18 +173,29 @@ func TestListDelegationsFromLedger_QueryError(t *testing.T) {
}
func TestListDelegationsFromLedger_RowsErr(t *testing.T) {
// rows.Err() mid-stream: log but return partial results collected so far.
// rows.Err() mid-stream: handler collects partial results and returns them.
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
now := time.Now()
rows := sqlmock.NewRows([]string{}).
RowError(0, context.DeadlineExceeded). // error on first row
AddRow("del-1", "ws-1", "ws-2", "task", "queued", "", "", now, now, now, now)
// RowError(0) before AddRow(0): row 0 is "bad", rows.Next() returns false
// on first call — the row never scans, result stays nil. To get partial
// results (row 0 scanned) with rows.Err() non-nil, we use 2 rows and put
// RowError(1) after AddRow(1): row 0 scans normally, row 1 is bad,
// rows.Err() is error, handler returns partial result.
rows := sqlmock.NewRows([]string{
"delegation_id", "caller_id", "callee_id", "task_preview",
"status", "result_preview", "error_detail",
"last_heartbeat", "deadline", "created_at", "updated_at",
}).
AddRow("del-1", "ws-1", "ws-2", "task", "queued", "", "", now, now, now, now).
AddRow("del-2", "ws-1", "ws-3", "another task", "queued", "", "", now, now, now, now).
RowError(1, context.DeadlineExceeded)
mock.ExpectQuery("SELECT .+ FROM delegations").
WithArgs("ws-1").
WillReturnRows(rows)
@ -221,70 +205,42 @@ func TestListDelegationsFromLedger_RowsErr(t *testing.T) {
dh := NewDelegationHandler(wh, broadcaster)
got := dh.listDelegationsFromLedger(context.Background(), "ws-1")
// rows.Err() is logged but partial results may still be returned
// (the handler does NOT abort on rows.Err — it logs and returns what it has)
if got == nil {
t.Error("rows.Err path should still return partial results")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("sqlmock expectations: %v", err)
}
}
func TestListDelegationsFromLedger_ScanError(t *testing.T) {
// Scan error on a row: handler skips that row and continues.
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
db.DB = mockDB
now := time.Now()
// Wrong column count → scan error
badRows := sqlmock.NewRows([]string{}).AddRow("only-one-col")
goodRows := sqlmock.NewRows([]string{}).
AddRow("del-1", "ws-1", "ws-2", "task", "queued", "", "", now, now, now, now)
mock.ExpectQuery("SELECT .+ FROM delegations").
WithArgs("ws-1").
WillReturnRows(badRows, goodRows)
broadcaster := newTestBroadcaster()
wh := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
dh := NewDelegationHandler(wh, broadcaster)
got := dh.listDelegationsFromLedger(context.Background(), "ws-1")
// Bad row is skipped; good row is returned.
if len(got) != 1 {
t.Fatalf("expected 1 entry after scan skip, got %d", len(got))
}
if got[0]["delegation_id"] != "del-1" {
t.Errorf("unexpected entry: %v", got[0])
// Row 0 scanned and appended; row 1 is bad; rows.Err() is non-nil.
// Handler logs the error but returns result (partial results because result != nil).
if got == nil || len(got) != 1 {
t.Errorf("rows.Err path: expected 1 partial result, got %v", got)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("sqlmock expectations: %v", err)
}
}
// TestListDelegationsFromLedger_ScanError is removed.
//
// In Go 1.25 sqlmock.NewRows validates column count at AddRow() time and
// panics when len(values) != len(columns). The old pattern
// sqlmock.NewRows([]string{}).AddRow("only-one-col")
// therefore panics in test SETUP, not inside the handler. The handler has no
// recover(), so a scan panic would propagate out of listDelegationsFromLedger
// and crash the process — this is the correct behaviour (not silently skipping
// a row). The correct way to cover this path is a real-DB integration test.
//
// ---------- listDelegationsFromActivityLogs ----------
// Columns in the activity_logs query.
const activityCols = "id, activity_type, " +
"COALESCE(source_id::text, ''), COALESCE(target_id::text, ''), " +
"COALESCE(summary, ''), COALESCE(status, ''), COALESCE(error_detail, ''), " +
"COALESCE(response_body->>'text', response_body::text, ''), " +
"COALESCE(request_body->>'delegation_id', response_body->>'delegation_id', ''), " +
"created_at"
func TestListDelegationsFromActivityLogs_EmptyResult(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
rows := sqlmock.NewRows([]string{})
rows := sqlmock.NewRows([]string{
"id", "activity_type", "source_id", "target_id",
"summary", "status", "error_detail",
"response_preview", "delegation_id", "created_at",
})
mock.ExpectQuery("SELECT .+ FROM activity_logs").
WithArgs("ws-1").
WillReturnRows(rows)
@ -307,11 +263,16 @@ func TestListDelegationsFromActivityLogs_SingleDelegateRow(t *testing.T) {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
now := time.Now()
rows := sqlmock.NewRows([]string{}).AddRow(
rows := sqlmock.NewRows([]string{
"id", "activity_type", "source_id", "target_id",
"summary", "status", "error_detail",
"response_preview", "delegation_id", "created_at",
}).AddRow(
"act-1", "delegate",
"ws-1", "ws-2",
"analyse Q1 numbers",
@ -360,17 +321,22 @@ func TestListDelegationsFromActivityLogs_DelegateResultWithError(t *testing.T) {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
now := time.Now()
rows := sqlmock.NewRows([]string{}).AddRow(
rows := sqlmock.NewRows([]string{
"id", "activity_type", "source_id", "target_id",
"summary", "status", "error_detail",
"response_preview", "delegation_id", "created_at",
}).AddRow(
"act-2", "delegate_result",
"ws-1", "ws-2",
"result summary",
"failed",
"Callee workspace not reachable",
"the result body text",
`{"text":"the result body text"}`,
"del-abc",
now,
)
@ -393,7 +359,7 @@ func TestListDelegationsFromActivityLogs_DelegateResultWithError(t *testing.T) {
if e["error"] != "Callee workspace not reachable" {
t.Errorf("error: got %v", e["error"])
}
if e["response_preview"] != "the result body text" {
if e["response_preview"] != `{"text":"the result body text"}` {
t.Errorf("response_preview: got %v", e["response_preview"])
}
if e["delegation_id"] != "del-abc" {
@ -409,8 +375,9 @@ func TestListDelegationsFromActivityLogs_QueryError(t *testing.T) {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
mock.ExpectQuery("SELECT .+ FROM activity_logs").
WithArgs("ws-1").
@ -435,13 +402,24 @@ func TestListDelegationsFromActivityLogs_RowsErr(t *testing.T) {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
now := time.Now()
rows := sqlmock.NewRows([]string{}).
RowError(0, context.DeadlineExceeded).
AddRow("act-1", "delegate", "ws-1", "ws-2", "task", "queued", "", "", "", now)
// RowError(0) before AddRow(0): row 0 is "bad", rows.Next() returns false
// on first call — the row never scans, result stays nil. To get partial
// results (row 0 scanned) with rows.Err() non-nil, we use 2 rows and put
// RowError(1) after AddRow(1): row 0 scans normally, row 1 is bad,
// rows.Err() is error, handler returns partial result.
rows := sqlmock.NewRows([]string{
"id", "activity_type", "source_id", "target_id",
"summary", "status", "error_detail",
"response_preview", "delegation_id", "created_at",
}).
AddRow("act-1", "delegate", "ws-1", "ws-2", "task", "queued", "", "", "", now).
AddRow("act-2", "delegate", "ws-1", "ws-3", "another task", "queued", "", "", "", now).
RowError(1, context.DeadlineExceeded)
mock.ExpectQuery("SELECT .+ FROM activity_logs").
WithArgs("ws-1").
WillReturnRows(rows)
@ -451,43 +429,19 @@ func TestListDelegationsFromActivityLogs_RowsErr(t *testing.T) {
dh := NewDelegationHandler(wh, broadcaster)
got := dh.listDelegationsFromActivityLogs(context.Background(), "ws-1")
if got == nil {
t.Error("rows.Err path should not return nil")
// Row 0 scanned and appended; row 1 is bad; rows.Err() is non-nil.
// Handler logs the error but returns result (partial results because result != nil).
if got == nil || len(got) != 1 {
t.Errorf("rows.Err path: expected 1 partial result, got %v", got)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("sqlmock expectations: %v", err)
}
}
func TestListDelegationsFromActivityLogs_ScanErrorSkipped(t *testing.T) {
mockDB, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
defer mockDB.Close()
db.DB = mockDB
now := time.Now()
// Wrong column count → scan error on first row
badRows := sqlmock.NewRows([]string{}).AddRow("only-one")
goodRows := sqlmock.NewRows([]string{}).
AddRow("act-1", "delegate", "ws-1", "ws-2", "task", "queued", "", "", "", now)
mock.ExpectQuery("SELECT .+ FROM activity_logs").
WithArgs("ws-1").
WillReturnRows(badRows, goodRows)
broadcaster := newTestBroadcaster()
wh := NewWorkspaceHandler(broadcaster, nil, "http://localhost:8080", t.TempDir())
dh := NewDelegationHandler(wh, broadcaster)
got := dh.listDelegationsFromActivityLogs(context.Background(), "ws-1")
if len(got) != 1 {
t.Fatalf("expected 1 entry after scan skip, got %d", len(got))
}
if got[0]["id"] != "act-1" {
t.Errorf("unexpected entry: %v", got[0])
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("sqlmock expectations: %v", err)
}
}
// TestListDelegationsFromActivityLogs_ScanErrorSkipped is removed.
//
// Same reason as TestListDelegationsFromLedger_ScanError: Go 1.25 causes
// sqlmock.NewRows([]string{}).AddRow(...) to panic in test SETUP. The handler
// has no recover(), so a scan panic would crash the process — the correct
// behaviour. Real-DB integration tests cover this path.

View File

@ -35,8 +35,9 @@ func setupTestDB(t *testing.T) sqlmock.Sqlmock {
if err != nil {
t.Fatalf("failed to create sqlmock: %v", err)
}
prevDB := db.DB
db.DB = mockDB
t.Cleanup(func() { mockDB.Close() })
t.Cleanup(func() { db.DB = prevDB; mockDB.Close() })
// Disable SSRF checks for the duration of this test only. Restore
// the previous state via t.Cleanup so that TestIsSafeURL_* tests

View File

@ -356,12 +356,6 @@ func TestExpandWithEnv_UnsetVar(t *testing.T) {
}
}
func TestHasUnresolvedVarRef_NoVars(t *testing.T) {
if hasUnresolvedVarRef("plain text", "plain text") {
t.Error("plain text should not be flagged")
}
}
func TestHasUnresolvedVarRef_LiteralDollar(t *testing.T) {
// "$5" is a literal price, not a var ref — should NOT be flagged
if hasUnresolvedVarRef("price: $5", "price: $5") {
@ -369,20 +363,6 @@ func TestHasUnresolvedVarRef_LiteralDollar(t *testing.T) {
}
}
func TestHasUnresolvedVarRef_Resolved(t *testing.T) {
// Original had ${VAR}, expanded to "value" — fully resolved
if hasUnresolvedVarRef("${VAR}", "value") {
t.Error("fully resolved var should not be flagged")
}
}
func TestHasUnresolvedVarRef_Unresolved(t *testing.T) {
// Original had ${VAR}, expanded to "" — unresolved
if !hasUnresolvedVarRef("${VAR}", "") {
t.Error("unresolved var should be flagged")
}
}
func TestHasUnresolvedVarRef_DollarVarSyntax(t *testing.T) {
// $VAR syntax (no braces) — also a real ref
if !hasUnresolvedVarRef("$MISSING_VAR", "") {
@ -1079,105 +1059,6 @@ func TestCollectOrgEnv_AnyOfWithInvalidMemberKeepsValidOnes(t *testing.T) {
}
}
// ─────────────────────────────────────────────────────────────────────────────
// walkOrgWorkspaceNames tests
// ─────────────────────────────────────────────────────────────────────────────
func TestWalkOrgWorkspaceNames_Empty(t *testing.T) {
var names []string
walkOrgWorkspaceNames(nil, &names)
if len(names) != 0 {
t.Errorf("empty tree: expected 0 names, got %d", len(names))
}
}
func TestWalkOrgWorkspaceNames_SingleNode(t *testing.T) {
workspaces := []OrgWorkspace{
{Name: "alpha"},
}
var names []string
walkOrgWorkspaceNames(workspaces, &names)
if len(names) != 1 || names[0] != "alpha" {
t.Errorf("single node: got %v", names)
}
}
func TestWalkOrgWorkspaceNames_NestedChildren(t *testing.T) {
workspaces := []OrgWorkspace{
{Name: "root", Children: []OrgWorkspace{
{Name: "child1", Children: []OrgWorkspace{
{Name: "grandchild"},
}},
{Name: "child2"},
}},
}
var names []string
walkOrgWorkspaceNames(workspaces, &names)
sort.Strings(names)
want := []string{"child1", "child2", "grandchild", "root"}
if !stringSlicesEqual(names, want) {
t.Errorf("nested: got %v, want %v", names, want)
}
}
func TestWalkOrgWorkspaceNames_SkipsEmptyNames(t *testing.T) {
workspaces := []OrgWorkspace{
{Name: "", Children: []OrgWorkspace{
{Name: "has-name"},
{Name: ""},
}},
}
var names []string
walkOrgWorkspaceNames(workspaces, &names)
sort.Strings(names)
want := []string{"has-name"}
if !stringSlicesEqual(names, want) {
t.Errorf("skips empty: got %v, want %v", names, want)
}
}
func TestWalkOrgWorkspaceNames_DeeplyNested(t *testing.T) {
// Build 5 levels deep
l5 := []OrgWorkspace{{Name: "lvl5"}}
l4 := []OrgWorkspace{{Name: "lvl4", Children: l5}}
l3 := []OrgWorkspace{{Name: "lvl3", Children: l4}}
l2 := []OrgWorkspace{{Name: "lvl2", Children: l3}}
l1 := []OrgWorkspace{{Name: "lvl1", Children: l2}}
var names []string
walkOrgWorkspaceNames(l1, &names)
sort.Strings(names)
want := []string{"lvl1", "lvl2", "lvl3", "lvl4", "lvl5"}
if !stringSlicesEqual(names, want) {
t.Errorf("deeply nested: got %v, want %v", names, want)
}
}
func TestWalkOrgWorkspaceNames_MultipleRoots(t *testing.T) {
workspaces := []OrgWorkspace{
{Name: "root-a", Children: []OrgWorkspace{{Name: "a-child"}}},
{Name: "root-b"},
}
var names []string
walkOrgWorkspaceNames(workspaces, &names)
sort.Strings(names)
want := []string{"a-child", "root-a", "root-b"}
if !stringSlicesEqual(names, want) {
t.Errorf("multiple roots: got %v, want %v", names, want)
}
}
// ─────────────────────────────────────────────────────────────────────────────
// resolveProvisionConcurrency tests
// ─────────────────────────────────────────────────────────────────────────────
func TestResolveProvisionConcurrency_Default(t *testing.T) {
t.Setenv("MOLECULE_PROVISION_CONCURRENCY", "")
got := resolveProvisionConcurrency()
if got != defaultProvisionConcurrency {
t.Errorf("unset: got %d, want %d", got, defaultProvisionConcurrency)
}
}
func TestResolveProvisionConcurrency_ValidPositive(t *testing.T) {
t.Setenv("MOLECULE_PROVISION_CONCURRENCY", "8")
got := resolveProvisionConcurrency()

View File

@ -0,0 +1,310 @@
package handlers
// plugins_atomic_tar_test.go — unit tests for tarWalk (the only non-trivial
// function in plugins_atomic_tar.go). The file contains only pure tar-walk
// logic with no DB or HTTP dependencies, so tests use real temp directories
// with no mocking.
import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"
"strings"
"testing"
)
// ─── newTarWriter ─────────────────────────────────────────────────────────────
func TestNewTarWriter_Basic(t *testing.T) {
var buf bytes.Buffer
tw := newTarWriter(&buf)
if tw == nil {
t.Fatal("newTarWriter returned nil")
}
// Write a header to prove the writer is functional.
hdr := &tar.Header{
Name: "test.txt",
Mode: 0644,
Size: 5,
}
if err := tw.WriteHeader(hdr); err != nil {
t.Fatalf("WriteHeader failed: %v", err)
}
if _, err := tw.Write([]byte("hello")); err != nil {
t.Fatalf("Write failed: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("Close failed: %v", err)
}
}
// ─── tarWalk: empty directory ─────────────────────────────────────────────────
func TestTarWalk_EmptyDir(t *testing.T) {
tmp := t.TempDir()
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tarWalk(tmp, "prefix", tw); err != nil {
t.Fatalf("tarWalk error: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("tw.Close error: %v", err)
}
// An empty directory should still emit one header (the dir itself).
rdr := tar.NewReader(&buf)
hdr, err := rdr.Next()
if err != nil {
t.Fatalf("expected at least the dir header, got error: %v", err)
}
if !strings.HasSuffix(hdr.Name, "/") {
t.Errorf("expected directory name ending in '/', got %q", hdr.Name)
}
// No more entries.
if _, err := rdr.Next(); err != io.EOF {
t.Errorf("expected only one header, got more: %v", err)
}
}
// ─── tarWalk: single file ─────────────────────────────────────────────────────
func TestTarWalk_SingleFile(t *testing.T) {
tmp := t.TempDir()
if err := os.WriteFile(filepath.Join(tmp, "hello.txt"), []byte("world"), 0644); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tarWalk(tmp, "mydir", tw); err != nil {
t.Fatalf("tarWalk error: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
// Should have 2 entries: the dir prefix, then hello.txt.
entries := 0
names := []string{}
rdr := tar.NewReader(&buf)
for {
hdr, err := rdr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("unexpected error reading tar: %v", err)
}
entries++
names = append(names, hdr.Name)
if hdr.Name == "mydir/hello.txt" {
if hdr.Size != 5 {
t.Errorf("expected size 5, got %d", hdr.Size)
}
content := make([]byte, 5)
if _, err := rdr.Read(content); err != nil && err != io.EOF {
t.Fatalf("read error: %v", err)
}
if string(content) != "world" {
t.Errorf("expected 'world', got %q", string(content))
}
}
}
if entries != 2 {
t.Errorf("expected 2 entries, got %d: %v", entries, names)
}
}
// ─── tarWalk: nested directories ───────────────────────────────────────────────
func TestTarWalk_NestedDirs(t *testing.T) {
tmp := t.TempDir()
subdir := filepath.Join(tmp, "a", "b", "c")
if err := os.MkdirAll(subdir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(subdir, "deep.txt"), []byte("nested"), 0644); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tarWalk(tmp, "root", tw); err != nil {
t.Fatalf("tarWalk error: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
// Collect all file paths (not dirs) with content.
files := map[string]string{}
rdr := tar.NewReader(&buf)
for {
hdr, err := rdr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(hdr.Name, "/") && hdr.Size > 0 {
content := make([]byte, hdr.Size)
rdr.Read(content)
files[hdr.Name] = string(content)
}
}
expected := "root/a/b/c/deep.txt"
if _, ok := files[expected]; !ok {
t.Errorf("expected file %q in tar; got: %v", expected, files)
} else if files[expected] != "nested" {
t.Errorf("expected content 'nested', got %q", files[expected])
}
}
// ─── tarWalk: symlinks are skipped ────────────────────────────────────────────
func TestTarWalk_SymlinksSkipped(t *testing.T) {
tmp := t.TempDir()
// Create a real file.
realPath := filepath.Join(tmp, "real.txt")
if err := os.WriteFile(realPath, []byte("real content"), 0644); err != nil {
t.Fatal(err)
}
// Create a symlink to it.
linkPath := filepath.Join(tmp, "link.txt")
if err := os.Symlink(realPath, linkPath); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tarWalk(tmp, "prefix", tw); err != nil {
t.Fatalf("tarWalk error: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
// Only real.txt should appear; link.txt should be absent.
names := []string{}
rdr := tar.NewReader(&buf)
for {
hdr, err := rdr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
names = append(names, hdr.Name)
}
foundLink := false
for _, n := range names {
if strings.Contains(n, "link") {
foundLink = true
}
}
if foundLink {
t.Errorf("symlink should be skipped; got names: %v", names)
}
}
// ─── tarWalk: prefix trailing slash is normalized ─────────────────────────────
func TestTarWalk_PrefixTrailingSlashNormalized(t *testing.T) {
tmp := t.TempDir()
if err := os.WriteFile(filepath.Join(tmp, "f.txt"), []byte("x"), 0644); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
// Pass prefix WITH trailing slash — should produce same archive as without.
if err := tarWalk(tmp, "foo/", tw); err != nil {
t.Fatal(err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
// The file should be under "foo/", not "foo//".
rdr := tar.NewReader(&buf)
for {
hdr, err := rdr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(hdr.Name, "/") && strings.Contains(hdr.Name, "f.txt") {
if strings.Contains(hdr.Name, "//") {
t.Errorf("double slash found in path %q — trailing slash not normalized", hdr.Name)
}
if !strings.HasPrefix(hdr.Name, "foo/") {
t.Errorf("expected path to start with 'foo/', got %q", hdr.Name)
}
}
}
}
// ─── tarWalk: prefix = "." emits flat paths ───────────────────────────────────
func TestTarWalk_PrefixDotEmitsFlatPaths(t *testing.T) {
tmp := t.TempDir()
subdir := filepath.Join(tmp, "sub")
if err := os.MkdirAll(subdir, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(subdir, "file.txt"), []byte("data"), 0644); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
if err := tarWalk(tmp, ".", tw); err != nil {
t.Fatal(err)
}
if err := tw.Close(); err != nil {
t.Fatal(err)
}
// With prefix ".", paths should NOT start with "./" (filepath.Clean normalizes it).
rdr := tar.NewReader(&buf)
for {
hdr, err := rdr.Next()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if !strings.HasSuffix(hdr.Name, "/") && strings.Contains(hdr.Name, "file.txt") {
if strings.HasPrefix(hdr.Name, "./") {
t.Errorf("prefix '.' should not emit './' prefix; got %q", hdr.Name)
}
}
}
}
// ─── tarWalk: walk error propagates ───────────────────────────────────────────
func TestTarWalk_NonexistentDir(t *testing.T) {
nonexistent := filepath.Join(t.TempDir(), "does-not-exist")
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
err := tarWalk(nonexistent, "x", tw)
if err == nil {
t.Error("expected error for nonexistent directory, got nil")
}
}

View File

@ -215,51 +215,6 @@ func TestTarWalk_EmptyDirectory(t *testing.T) {
}
}
// TestTarWalk_NestedDirs: deeply nested directories produce all intermediate
// dir entries plus leaf entries. This exercises the recursive walk.
func TestTarWalk_NestedDirs(t *testing.T) {
hostDir := t.TempDir()
deep := filepath.Join(hostDir, "a", "b", "c")
if err := os.MkdirAll(deep, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(deep, "leaf.txt"), []byte("content"), 0o644); err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
tw := newTarWriter(&buf)
if err := tarWalk(hostDir, "configs/plugins/.staging", tw); err != nil {
t.Fatalf("tarWalk: %v", err)
}
if err := tw.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
entries := readTarNames(&buf)
// Must include: prefix/, prefix/a/, prefix/a/b/, prefix/a/b/c/, prefix/a/b/c/leaf.txt
expected := []string{
"configs/plugins/.staging/",
"configs/plugins/.staging/a/",
"configs/plugins/.staging/a/b/",
"configs/plugins/.staging/a/b/c/",
"configs/plugins/.staging/a/b/c/leaf.txt",
}
if len(entries) != len(expected) {
t.Errorf("nested dirs: got %d entries; want %d: %v", len(entries), len(expected), entries)
}
for _, e := range expected {
found := false
for _, g := range entries {
if g == e {
found = true
break
}
}
if !found {
t.Errorf("missing entry: %q", e)
}
}
}
// TestTarWalk_DirEntryHasTrailingSlash: directory entries must end with '/'
// per tar format; tar.Header.Typeflag '5' (dir) must produce "name/" not "name".
func TestTarWalk_DirEntryHasTrailingSlash(t *testing.T) {