Files
ARTEX/db/task_scope_test.go
T
protonmatter 133dc68668 Test that a failed coverage read stays enabled, including auto-scope writes.
A missing task and a query error both leave the flag on. The worker keeps the coverage tools and still records auto scope when the stored flag is off.
2026-11-17 15:52:04 -05:00

57 lines
1.8 KiB
Go

package db
import "testing"
func TestCoverageEnabledWithoutTask(t *testing.T) {
on, err := (&AssetStore{}).CoverageEnabled(0)
if err != nil || !on {
t.Fatalf("enabled=%v err=%v", on, err)
}
}
func TestCoverageEnabledMissingRowStaysOn(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Skipf("postgres unavailable (%v)", err)
}
defer d.Close()
on, err := d.Assets().CoverageEnabled(9_000_000_000_001)
if err != nil || !on {
t.Fatalf("missing task: enabled=%v err=%v, want enabled with no error", on, err)
}
}
func TestCoverageEnabledQueryErrorStaysOn(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Skipf("postgres unavailable (%v)", err)
}
if err := d.Close(); err != nil {
t.Fatal(err)
}
on, err := d.Assets().CoverageEnabled(1)
if err == nil || !on {
t.Fatalf("failed query: enabled=%v err=%v, want enabled and the error", on, err)
}
}
// TestStripHostPort covers the ip:port / [ipv6]:port stripping used by the
// ip/cidr scope path so a target like "10.0.188.136:3000" no longer 404s.
func TestStripHostPort(t *testing.T) {
cases := []struct{ in, want string }{
{"10.0.188.136:3000", "10.0.188.136"}, // the reported IP case
{"10.0.188.136", "10.0.188.136"}, // bare IPv4 unchanged
{"[2001:db8::1]:8080", "2001:db8::1"}, // bracketed IPv6 + port
{"2001:db8::1", "2001:db8::1"}, // bare IPv6 unchanged (has colons)
{" 1.2.3.4:80 ", "1.2.3.4"}, // trims surrounding space
{"example.com:443", "example.com"}, // domain + port → bare host
{"api.example.com:8080", "api.example.com"}, // subdomain + port
{"example.com", "example.com"}, // bare domain unchanged
}
for _, c := range cases {
if got := stripHostPort(c.in); got != c.want {
t.Errorf("stripHostPort(%q) = %q, want %q", c.in, got, c.want)
}
}
}