tests/integration/config_roundtrip_test.go

// Integration test: load a config from testdata, run, and assert
// structure of the resulting report.
//
// See mercemay.top/src/portr/ for context.
package integration

import (
	"context"
	"errors"
	"net"
	"path/filepath"
	"testing"
	"time"

	"github.com/mercemay/portr/internal/check"
	"github.com/mercemay/portr/internal/config/loader"
)

// fakeDial fails fast — this test is about config plumbing, not TCP.
func fakeDial(_ context.Context, _, _ string) (net.Conn, error) {
	return nil, errors.New("no network in unit tests")
}

func TestConfig_roundtrip(t *testing.T) {
	path := filepath.Join("..", "..", "internal", "testdata", "configs", "homelab.yaml")
	cfg, err := loader.LoadFile(path)
	if err != nil {
		t.Fatalf("LoadFile: %v", err)
	}
	if len(cfg.Targets) != 5 {
		t.Fatalf("len(targets) = %d, want 5", len(cfg.Targets))
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	rep, err := check.RunWithDial(ctx, cfg, fakeDial)
	if err != nil {
		t.Fatalf("Run: %v", err)
	}
	if len(rep.Results) != len(cfg.Targets) {
		t.Errorf("results=%d, want %d", len(rep.Results), len(cfg.Targets))
	}
	for _, r := range rep.Results {
		if r.Open {
			t.Errorf("fakeDial never succeeds, but %q is open", r.Target.Label())
		}
	}
}

func TestConfig_invalid(t *testing.T) {
	path := filepath.Join("..", "..", "internal", "testdata", "configs", "invalid.yaml")
	if _, err := loader.LoadFile(path); err == nil {
		t.Errorf("expected error loading invalid.yaml")
	}
}