TestBodyHashStoredInLockMetadata verifies that the body hash is computed and stored in the lock metadata when a workflow is compiled.
(t *testing.T)
| 181 | // TestBodyHashStoredInLockMetadata verifies that the body hash is computed and stored in the |
| 182 | // lock metadata when a workflow is compiled. |
| 183 | func TestBodyHashStoredInLockMetadata(t *testing.T) { |
| 184 | workflowMD := `--- |
| 185 | engine: copilot |
| 186 | on: |
| 187 | issues: |
| 188 | types: [opened] |
| 189 | --- |
| 190 | This is the body of the workflow. It contains the agent instructions. |
| 191 | |
| 192 | ## Steps |
| 193 | |
| 194 | 1. Do something |
| 195 | 2. Do something else |
| 196 | ` |
| 197 | |
| 198 | tmpDir := testutil.TempDir(t, "body-hash-test") |
| 199 | workflowPath := filepath.Join(tmpDir, "body-hash.md") |
| 200 | require.NoError(t, os.WriteFile(workflowPath, []byte(workflowMD), 0644)) |
| 201 | |
| 202 | compiler := NewCompiler() |
| 203 | err := compiler.CompileWorkflow(workflowPath) |
| 204 | require.NoError(t, err, "Workflow should compile without errors") |
| 205 | |
| 206 | lockPath := stringutil.MarkdownToLockFile(workflowPath) |
| 207 | lockContent, err := os.ReadFile(lockPath) |
| 208 | require.NoError(t, err, "Lock file should be readable") |
| 209 | |
| 210 | metadata, _, err := ExtractMetadataFromLockFile(string(lockContent)) |
| 211 | require.NoError(t, err, "Lock metadata should be parseable") |
| 212 | require.NotNil(t, metadata, "Lock metadata should exist") |
| 213 | |
| 214 | assert.Equal(t, LockSchemaV4, metadata.SchemaVersion, "Should use v4 schema with body hash") |
| 215 | assert.NotEmpty(t, metadata.BodyHash, "Body hash should be stored in lock metadata") |
| 216 | assert.Len(t, metadata.BodyHash, 64, "Body hash should be a 64-character hex string") |
| 217 | |
| 218 | // Verify the body hash is recomputable |
| 219 | currentBodyHash, err := parser.ComputeBodyHashFromFile(workflowPath) |
| 220 | require.NoError(t, err, "Body hash should be recomputable from workflow file") |
| 221 | |
| 222 | assert.Equal(t, currentBodyHash, metadata.BodyHash, |
| 223 | "Body hash in lock metadata should match recomputed hash") |
| 224 | } |
| 225 | |
| 226 | // TestBodyHashChangesWhenBodyChanges verifies that modifying the body text produces a different hash. |
| 227 | func TestBodyHashChangesWhenBodyChanges(t *testing.T) { |
nothing calls this directly
no test coverage detected