Skip to content

[test] Add tests for logger.ObservedURLDomainsLogger#8054

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
test-coverage-observed-url-domains-logger-8317a2c0eca322b9
Draft

[test] Add tests for logger.ObservedURLDomainsLogger#8054
github-actions[bot] wants to merge 1 commit into
mainfrom
test-coverage-observed-url-domains-logger-8317a2c0eca322b9

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

Test Coverage Improvement: ObservedURLDomainsLogger

Function Analyzed

  • Package: internal/logger
  • File: internal/logger/observed_url_domains_logger.go
  • Previous Coverage: 93.0% (package) — target functions were at 0%
  • New Coverage: 97.8% (package) — target functions at 100%
  • Complexity: Medium-High (deduplication logic, fallback paths, atomic file writes, global state management)

Why This Function?

All public functions in observed_url_domains_logger.go had 0% coverage despite being non-trivial: they manage global state, implement a deduplication set with change-detection, perform atomic file writes via a temp-rename pattern, and include silent fallback handling when log directory creation fails. This made them an ideal high-value target for coverage improvement.

Tests Added

  • ✅ Default and toggled audit-enabled state (SetURLDomainAuditEnabled / URLDomainAuditEnabled)
  • ✅ Successful logger initialization and teardown lifecycle
  • ✅ Silent fallback when log directory cannot be created
  • ✅ Empty / nil / missing domain inputs (no-op, no panic)
  • ✅ Deduplication within a single LogDomains call
  • ✅ Deduplication across multiple LogDomains calls (no double-write)
  • ✅ Accumulation of new domains across calls
  • ✅ Filtering of empty-string domain entries
  • ✅ No-write optimisation when nothing changed
  • ✅ Multiple server IDs tracked independently
  • ✅ Sorted JSON output (alphabetical domains per server)
  • ✅ Warning log path when writeToFile fails after directory removal
  • ✅ Global helper delegation and no-panic when no global logger is set
  • ✅ Fallback mode (useFallback=true) is a silent no-op

Coverage Report

Before: internal/logger  93.0%   (observed_url_domains_logger.go functions: 0%)
After:  internal/logger  97.8%   (observed_url_domains_logger.go functions: 100% on most)
Improvement: +4.8pp package-wide; +100pp for target functions

Per-function breakdown (after)

Function Coverage
SetURLDomainAuditEnabled 100.0%
URLDomainAuditEnabled 100.0%
handleObservedURLDomainsLoggerError 100.0%
InitObservedURLDomainsLogger 100.0%
LogDomains 100.0%
LogObservedURLDomains 100.0%
Close 100.0%
CloseObservedURLDomainsLogger 100.0%
setupObservedURLDomainsLogger 85.7% (init writeToFile failure; untestable without mocks)
writeToFile 87.5% (json.Marshal failure branch; never occurs in practice)

Test Execution

All 24 new tests pass:

--- PASS: TestURLDomainAuditEnabled_DefaultFalse
--- PASS: TestURLDomainAuditEnabled_Toggle
--- PASS: TestInitObservedURLDomainsLogger_Success
--- PASS: TestInitObservedURLDomainsLogger_FallbackOnBadDir
--- PASS: TestCloseObservedURLDomainsLogger_NilGlobal
--- PASS: TestCloseObservedURLDomainsLogger_ClearsGlobal
--- PASS: TestLogDomains_EmptyServerID
--- PASS: TestLogDomains_EmptyDomains
--- PASS: TestLogDomains_NilDomains
--- PASS: TestLogDomains_FallbackMode_ReturnsNil
--- PASS: TestLogDomains_AddsSingleDomain
--- PASS: TestLogDomains_DeduplicatesWithinCall
--- PASS: TestLogDomains_DeduplicatesAcrossCalls
--- PASS: TestLogDomains_AccumulatesNewDomains
--- PASS: TestLogDomains_SkipsEmptyDomainStrings
--- PASS: TestLogDomains_NoWriteWhenNothingNew
--- PASS: TestLogDomains_MultipleServerIDs
--- PASS: TestLogDomains_SortedOutputInFile
--- PASS: TestLogObservedURLDomains_NoGlobalLogger_NoPanic
--- PASS: TestLogObservedURLDomains_DelegatesToGlobalLogger
--- PASS: TestLogObservedURLDomains_EmptyServerID_NoPanic
--- PASS: TestLogObservedURLDomains_FallbackMode_NoPanic
--- PASS: TestLogObservedURLDomains_WriteError_WarningLogged
--- PASS: TestObservedURLDomainsLogger_Close_ReturnsNil
PASS
ok  github.com/github/gh-aw-mcpg/internal/logger  0.367s

Generated by Test Coverage Improver
Next run will target the next most complex under-tested function

Warning

Firewall blocked 6 domains

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

  • go.opentelemetry.io
  • go.yaml.in
  • golang.org
  • google.golang.org
  • gopkg.in
  • proxy.golang.org

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

network:
  allowed:
    - defaults
    - "go.opentelemetry.io"
    - "go.yaml.in"
    - "golang.org"
    - "google.golang.org"
    - "gopkg.in"
    - "proxy.golang.org"

See Network Configuration for more information.

Generated by Test Coverage Improver · 446.5 AIC · ⊞ 7.1K ·

Adds comprehensive tests for the ObservedURLDomainsLogger type and its
public helpers in internal/logger/observed_url_domains_logger.go, which
previously had 0% test coverage.

Coverage improvement for internal/logger: 93.0% → 97.8%

Target functions brought to 100% coverage:
- SetURLDomainAuditEnabled
- URLDomainAuditEnabled
- handleObservedURLDomainsLoggerError
- InitObservedURLDomainsLogger
- LogDomains
- LogObservedURLDomains
- Close
- CloseObservedURLDomainsLogger

Tests cover:
- Default and toggled audit-enabled state
- Successful logger initialization and teardown lifecycle
- Silent fallback when log directory cannot be created
- Empty/nil/missing domain inputs
- Deduplication within and across LogDomains calls
- Accumulation of new domains across multiple calls
- Filtering of empty-string domain entries
- No-write optimization when nothing changed
- Multiple server IDs tracked independently
- Sorted JSON output (alphabetical domains per server)
- Warning log path when writeToFile fails (directory removed)
- Global helper delegation and no-panic when no global logger

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.

0 participants