Skip to content

[test] Add tests for difc package: getItems, AddIntegrityTags, Intersect, checkFlowHelper#1860

Merged
lpcox merged 4 commits intomainfrom
test/improve-difc-coverage-20260313-f6e3c7e49d22438d
Mar 13, 2026
Merged

[test] Add tests for difc package: getItems, AddIntegrityTags, Intersect, checkFlowHelper#1860
lpcox merged 4 commits intomainfrom
test/improve-difc-coverage-20260313-f6e3c7e49d22438d

Conversation

@github-actions
Copy link
Contributor

Test Coverage Improvement: internal/difc package

Summary

Comprehensive test coverage improvement for the internal/difc package, focusing on getItems as the primary target (most complex function with lowest coverage) plus several closely related under-tested functions.

Metric Before After
Package coverage 93.8% 99.7%
getItems 58.3% 95.8%
AddIntegrityTags 0.0% 100.0%
Intersect 66.7% 100.0%
checkFlowHelper 90.9% 100.0%
resolve 88.5% 100.0%
Overall 80.0% 100.0%
GetItems 66.7% 100.0%
ToCollectionLabeledData 66.7% 100.0%
extractIndexFromPath 89.5% 100.0%
pathEntryToResource 87.5% 100.0%
ParsePathLabels 83.3% 100.0%
NewPathLabeledData 83.3% 100.0%

Why getItems?

getItems (internal/difc/path_labels.go:192) was selected as the primary target:

  • Lowest coverage among complex functions at 58.3%
  • Highest complexity: JSON pointer navigation with 6+ distinct branches (map traversal, array index traversal, out-of-bounds errors, non-numeric index errors, unexpected type errors, final non-array error)
  • Core DIFC logic: used by resolve() which drives the entire path-label system

Tests Added

New file: internal/difc/path_labels_coverage_test.go (28 test functions)

Covers all missing branches in getItems:

  • ✅ Path segment not found in map → error
  • ✅ Final path navigates to non-array value → error
  • ✅ Array index navigation through []interface{} → success
  • ✅ Array index out of bounds → error
  • ✅ Non-numeric array index → error
  • ✅ Unexpected type at path segment (default case) → error
  • ✅ Empty path segment (consecutive slashes) → continue/skip

Covers lazy-resolve branches:

  • Overall() called before GetItems() — lazy resolve
  • GetItems() called on unresolved data — lazy resolve
  • ToCollectionLabeledData() on unresolved data — lazy resolve

Covers other gaps:

  • resolve() early return when already resolved (direct call)
  • resolve() skips labeled paths that don't match items_path
  • pathEntryToResource(nil) → "unlabeled" resource
  • ParsePathLabels with invalid JSON → error
  • NewPathLabeledData resolve-error propagation
  • extractIndexFromPath third HasPrefix branch (no slash separator)
  • extractIndexFromPath empty remainder → "no index" error
  • unwrapMCPResponse when content[0] is not a map
  • ✅ MCP-wrapped single-item response
  • Overall() with empty items collection
  • Overall() union semantics across multiple items
  • ToResult() preserves original MCP-wrapped data

Additions to internal/difc/agent_test.go

  • AddIntegrityTags — was at 0% coverage; now 100%
    • Empty slice, nil slice, non-empty slice, duplicates, independence from secrecy

Additions to internal/difc/labels_test.go

  • Intersect — covers the non-nil intersection path with tag removal
  • checkFlowHelper nil-source + non-empty-target + integrity mode (lines 213-215 previously untested)

Coverage Report

Before: 93.8% coverage (internal/difc package)
After:  99.7% coverage (internal/difc package)
Improvement: +5.9%

Remaining gap (0.3%): GetOrCreate.double-check-after-write-lock — the concurrent double-check-locking pattern that requires precise goroutine race timing to cover deterministically. This is intentional defensive code and the existing 100-goroutine concurrent test provides sufficient confidence.

Test Execution

All 3 modified/new test files compile and pass cleanly. Tests follow the project's conventions:

  • Table-driven tests where applicable
  • require for critical assertions (stop on failure)
  • assert for non-critical assertions (continue on failure)
  • Bound asserters where multiple assertions exist
  • Descriptive test names using the TestFunctionName_Scenario pattern

Generated by Test Coverage Improver
Previous run: internal/guard.parseLabelAgentResponse (2026-03-12, 43.3% → 100%)
This run: internal/difc.getItems + difc package (2026-03-13, 93.8% → 99.7%)

Generated by Test Coverage Improver ·

Warning

⚠️ Firewall blocked 5 domains

The following domains were blocked by the firewall during workflow execution:

  • go.yaml.in
  • golang.org
  • gopkg.in
  • invalidhostthatdoesnotexist12345.com
  • proxy.golang.org

To allow these domains, add them to the network.allowed list in your workflow frontmatter:

network:
  allowed:
    - defaults
    - "go.yaml.in"
    - "golang.org"
    - "gopkg.in"
    - "invalidhostthatdoesnotexist12345.com"
    - "proxy.golang.org"

See Network Configuration for more information.

…eckFlowHelper

Improve test coverage for internal/difc from 93.8% to 99.7%.

Functions targeted (previously under-covered):
- getItems (58.3% → 95.8%): add tests for all error paths (path not found
  in map, final path not array, array navigation success, out-of-bounds index,
  non-numeric index, unexpected type, empty path segment)
- AddIntegrityTags (0% → 100%): add basic and edge-case tests
- Intersect (66.7% → 100%): add tests for non-nil intersection with tag removal
- checkFlowHelper (90.9% → 100%): add nil-source/non-empty-target integrity case
- resolve (88.5% → 100%): add tests for already-resolved early return and
  non-matching labeled path skip
- Overall, GetItems, ToCollectionLabeledData: cover lazy-resolve branches
- extractIndexFromPath: cover third HasPrefix branch and empty-remainder case
- pathEntryToResource: cover nil-entry path
- ParsePathLabels: cover invalid JSON error case
- NewPathLabeledData: cover resolve-error propagation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review March 13, 2026 17:15
Copilot AI review requested due to automatic review settings March 13, 2026 17:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds additional DIFC unit tests to increase branch/edge-case coverage for path-based labeling, label set operations, and agent integrity tag mutation.

Changes:

  • Introduces a new path_labels_coverage_test.go suite covering numerous PathLabeledData/getItems/MCP unwrap branches.
  • Adds coverage for Label.Intersect and an untested checkFlowHelper integrity edge case.
  • Adds coverage for AgentLabels.AddIntegrityTags, including duplicate/no-op scenarios.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
internal/difc/path_labels_coverage_test.go New coverage-focused tests for path label resolution, MCP unwrap behavior, and several error branches.
internal/difc/labels_test.go Adds tests for Label.Intersect and an uncovered checkFlowHelper integrity case.
internal/difc/agent_test.go Adds tests for AgentLabels.AddIntegrityTags behavior and isolation from secrecy labels.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +301 to +306
// HasPrefix("/items3", "/items/") = false
// HasPrefix("/items3", "/items") = true AND len > len → third branch
pld := &PathLabeledData{}
idx, err := pld.extractIndexFromPath("items3", "items")
require.NoError(t, err)
assert.Equal(t, 3, idx)
Comment on lines +387 to +389
items1 := pld.GetItems()

// Call GetItems again — hits the p.resolved guard (early return in resolve)
}

// TestLabel_Intersect tests the Intersect method which keeps only tags present
// in both labels. Previous coverage was 66.7%.
Comment on lines +1086 to +1090
// TestAgentLabels_AddIntegrityTags tests the AddIntegrityTags method which was
// previously at 0% coverage.
func TestAgentLabels_AddIntegrityTags(t *testing.T) {
tests := []struct {
name string
@lpcox
Copy link
Collaborator

lpcox commented Mar 13, 2026

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link
Contributor

Copilot AI commented Mar 13, 2026

@lpcox I've opened a new pull request, #1863, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 2 commits March 13, 2026 17:31
…ments

Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
…omments (#1863)

`extractIndexFromPath` had a buggy third `HasPrefix` branch that matched
`/items3` against itemsPath `/items`, incorrectly associating it as
index 3. Per RFC 6901, `/items3` is a distinct key — only paths
separated by `/` (e.g., `/items/3`) should match.

## Changes

- **`path_labels.go`**: Remove the erroneous third branch; valid matches
now require `itemsPath + "/"` prefix
- **`path_labels_coverage_test.go`**:
- Rename `TestExtractIndexFromPath_ThirdHasPrefix` →
`TestExtractIndexFromPath_NoSlashSeparator`; flip expectation from
success to error
- Fix `TestPathLabeledData_ResolveNotCalledTwice` comment — the guard
being exercised is in `GetItems()`, not inside `resolve()`
- **`labels_test.go`**, **`agent_test.go`**: Remove stale historical
coverage percentages from test comments

```go
// Before: "/items3" incorrectly matched itemsPath "/items" → index 3
} else if strings.HasPrefix(path, itemsPath+"/") {
    remainder = strings.TrimPrefix(path, itemsPath)
} else if strings.HasPrefix(path, itemsPath) && len(path) > len(itemsPath) { // removed
    remainder = path[len(itemsPath):]                                          // removed
} else {

// After: only explicit "/" separator matches
} else if strings.HasPrefix(path, itemsPath+"/") {
    remainder = strings.TrimPrefix(path, itemsPath)
} else {
```

<!-- START COPILOT CODING AGENT TIPS -->
---

🔒 GitHub Advanced Security automatically protects Copilot coding agent
pull requests. You can protect all pull requests by enabling Advanced
Security for your repositories. [Learn more about Advanced
Security.](https://gh.io/cca-advanced-security)
@lpcox lpcox merged commit 8830dae into main Mar 13, 2026
13 checks passed
@lpcox lpcox deleted the test/improve-difc-coverage-20260313-f6e3c7e49d22438d branch March 13, 2026 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants