|
| 1 | +// |
| 2 | +// Copyright 2026 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package aiagentconfig |
| 17 | + |
| 18 | +import ( |
| 19 | + "crypto/sha256" |
| 20 | + "encoding/base64" |
| 21 | + "encoding/hex" |
| 22 | + "encoding/json" |
| 23 | + "os" |
| 24 | + "path/filepath" |
| 25 | + "sort" |
| 26 | + "strings" |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +func TestBuildEvidence(t *testing.T) { |
| 34 | + rootDir := t.TempDir() |
| 35 | + |
| 36 | + // Create test files |
| 37 | + file1Content := []byte("# Project Rules\nAlways use Go.") |
| 38 | + file2Content := []byte(`{"allow": ["read"]}`) |
| 39 | + |
| 40 | + require.NoError(t, os.WriteFile(filepath.Join(rootDir, "CLAUDE.md"), file1Content, 0o600)) |
| 41 | + require.NoError(t, os.MkdirAll(filepath.Join(rootDir, ".claude"), 0o755)) |
| 42 | + require.NoError(t, os.WriteFile(filepath.Join(rootDir, ".claude", "settings.json"), file2Content, 0o600)) |
| 43 | + |
| 44 | + gitCtx := &GitContext{ |
| 45 | + Repository: "https://github.com/org/repo", |
| 46 | + CommitSHA: "abc123", |
| 47 | + } |
| 48 | + |
| 49 | + evidence, err := BuildEvidence(rootDir, []string{"CLAUDE.md", ".claude/settings.json"}, gitCtx) |
| 50 | + require.NoError(t, err) |
| 51 | + |
| 52 | + // Verify envelope |
| 53 | + assert.Equal(t, EvidenceID, evidence.ID) |
| 54 | + assert.Equal(t, EvidenceSchemaURL, evidence.Schema) |
| 55 | + |
| 56 | + // Verify data |
| 57 | + assert.Equal(t, "v1alpha", evidence.Data.SchemaVersion) |
| 58 | + assert.Equal(t, "claude", evidence.Data.Agent.Name) |
| 59 | + assert.NotEmpty(t, evidence.Data.CapturedAt) |
| 60 | + assert.NotEmpty(t, evidence.Data.ConfigHash) |
| 61 | + |
| 62 | + // Verify git context |
| 63 | + require.NotNil(t, evidence.Data.GitContext) |
| 64 | + assert.Equal(t, "https://github.com/org/repo", evidence.Data.GitContext.Repository) |
| 65 | + assert.Equal(t, "abc123", evidence.Data.GitContext.CommitSHA) |
| 66 | + |
| 67 | + // Verify config files |
| 68 | + require.Len(t, evidence.Data.ConfigFiles, 2) |
| 69 | + |
| 70 | + cf1 := evidence.Data.ConfigFiles[0] |
| 71 | + assert.Equal(t, "CLAUDE.md", cf1.Path) |
| 72 | + assert.Equal(t, int64(len(file1Content)), cf1.Size) |
| 73 | + hash1 := sha256.Sum256(file1Content) |
| 74 | + assert.Equal(t, hex.EncodeToString(hash1[:]), cf1.SHA256) |
| 75 | + assert.Equal(t, base64.StdEncoding.EncodeToString(file1Content), cf1.Base64Content) |
| 76 | + |
| 77 | + cf2 := evidence.Data.ConfigFiles[1] |
| 78 | + assert.Equal(t, ".claude/settings.json", cf2.Path) |
| 79 | + hash2 := sha256.Sum256(file2Content) |
| 80 | + assert.Equal(t, hex.EncodeToString(hash2[:]), cf2.SHA256) |
| 81 | + |
| 82 | + // Verify config hash is deterministic |
| 83 | + hashes := []string{hex.EncodeToString(hash1[:]), hex.EncodeToString(hash2[:])} |
| 84 | + sort.Strings(hashes) |
| 85 | + combined := sha256.Sum256([]byte(strings.Join(hashes, ""))) |
| 86 | + assert.Equal(t, hex.EncodeToString(combined[:]), evidence.Data.ConfigHash) |
| 87 | +} |
| 88 | + |
| 89 | +func TestBuildEvidenceWithoutGitContext(t *testing.T) { |
| 90 | + rootDir := t.TempDir() |
| 91 | + require.NoError(t, os.WriteFile(filepath.Join(rootDir, "CLAUDE.md"), []byte("content"), 0o600)) |
| 92 | + |
| 93 | + evidence, err := BuildEvidence(rootDir, []string{"CLAUDE.md"}, nil) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + assert.Nil(t, evidence.Data.GitContext) |
| 97 | + assert.Len(t, evidence.Data.ConfigFiles, 1) |
| 98 | +} |
| 99 | + |
| 100 | +func TestBuildEvidenceJSONFormat(t *testing.T) { |
| 101 | + rootDir := t.TempDir() |
| 102 | + require.NoError(t, os.WriteFile(filepath.Join(rootDir, "CLAUDE.md"), []byte("content"), 0o600)) |
| 103 | + |
| 104 | + evidence, err := BuildEvidence(rootDir, []string{"CLAUDE.md"}, nil) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + // Verify it marshals to valid JSON with the correct envelope field |
| 108 | + jsonData, err := json.Marshal(evidence) |
| 109 | + require.NoError(t, err) |
| 110 | + |
| 111 | + var raw map[string]any |
| 112 | + require.NoError(t, json.Unmarshal(jsonData, &raw)) |
| 113 | + |
| 114 | + assert.Equal(t, EvidenceID, raw["chainloop.material.evidence.id"]) |
| 115 | + assert.Equal(t, EvidenceSchemaURL, raw["schema"]) |
| 116 | + assert.NotNil(t, raw["data"]) |
| 117 | +} |
0 commit comments