gitlink-cli/shortcuts/workflow/rules/license_test.go

102 lines
3.0 KiB
Go

package rules
import (
"testing"
)
func TestLicenseFileDetection(t *testing.T) {
upstream := map[string]interface{}{
"existing-files": map[string]interface{}{
"data": []interface{}{
map[string]interface{}{"name": "src/main.go", "content": "package main"},
map[string]interface{}{"name": "LICENSE", "content": "MIT License\n\nPermission is hereby granted..."},
map[string]interface{}{"name": "README.md", "content": "# Project"},
},
},
}
resp, err := LicenseCheckRule(upstream, "license-check")
if err != nil {
t.Fatalf("LicenseCheckRule failed: %v", err)
}
analysis := resp.Analysis.(map[string]interface{})
if v := analysis["has_license"]; v != true {
t.Fatal("expected has_license=true")
}
if v := analysis["license_type"]; v != "MIT" {
t.Fatalf("expected license_type=MIT, got %v", v)
}
}
func TestSensitiveContentDetection(t *testing.T) {
upstream := map[string]interface{}{
"existing-files": map[string]interface{}{
"data": []interface{}{
map[string]interface{}{"name": "config.go", "content": `api_key = "sk-1234567890abcdef"`},
},
},
}
resp, err := LicenseCheckRule(upstream, "license-check")
if err != nil {
t.Fatalf("LicenseCheckRule failed: %v", err)
}
analysis := resp.Analysis.(map[string]interface{})
findings := analysis["findings"].([]riskEntry)
if len(findings) == 0 {
t.Fatal("expected findings for hardcoded key")
}
}
func TestPlaceholderExclusion(t *testing.T) {
upstream := map[string]interface{}{
"existing-files": map[string]interface{}{
"data": []interface{}{
map[string]interface{}{"name": "config.go", "content": `api_key = "your_token_here"`},
},
},
}
resp, err := LicenseCheckRule(upstream, "license-check")
if err != nil {
t.Fatalf("LicenseCheckRule failed: %v", err)
}
analysis := resp.Analysis.(map[string]interface{})
findings := analysis["findings"].([]riskEntry)
// Placeholder should not generate riskEntry findings.
// But the api_key pattern may still match — check that it's excluded.
for _, f := range findings {
if f.File == "config.go" {
t.Logf("finding: %+v", f)
// Should NOT be a high risk for the api_key pattern.
}
}
// The placeholder exclusion should filter out the match from content patterns.
// But filename-based detections may still fire. Let's just check there are no
// high risk findings for the placeholder content.
for _, f := range findings {
if f.Risk == "high" && f.File == "config.go" {
t.Errorf("placeholder should be excluded, but got high risk finding: %s", f.Message)
}
}
}
func TestLicenseFileMissing(t *testing.T) {
upstream := map[string]interface{}{
"existing-files": map[string]interface{}{
"data": []interface{}{
map[string]interface{}{"name": "README.md"},
},
},
}
resp, err := LicenseCheckRule(upstream, "license-check")
if err != nil {
t.Fatalf("LicenseCheckRule failed: %v", err)
}
analysis := resp.Analysis.(map[string]interface{})
if v := analysis["has_license"]; v != false {
t.Fatal("expected has_license=false")
}
}