Files
pmg/config/config_test.go
T
987bda5d6a feat: Add dependency cooldown for npm packages (#200)
* feat: Add dependency cooldown for npm packages

Strip recently-published package versions from npm registry metadata
responses so npm's resolver naturally falls back to older versions.
Overrides the Accept header to force full packument responses (which
include the "time" field needed for publish-date checks).

Reports cooldown blocks only when all versions are stripped (remaining == 0),
matching npm's --min-release-age behavior for silent fallback.

* fix: Report oldest version in cooldown block (shortest wait)

When all versions are blocked by cooldown, report the oldest version
since it exits the cooldown window first — giving the user the
shortest wait time instead of the longest.

* fix: Handle resp.Body.Close error return for errcheck linter

* test: Add dependency cooldown assertions to template config tests

* fix: config template for dependency cooldown

* fix: Prevent npm from caching cooldown-stripped metadata responses

* fix: Restore body on ReadAll failure and log Close errors in response modifier

* fix: Close response body before replacing to prevent connection leak

* fix: Correct daysLeft ceiling math and update ContentLength on error recovery

* fix: Clear Status on status code change and update ContentLength in modifier error path

* refactor: address review comments on dependency cooldown PR

- Make NpmCooldownHandler and constructor package-private
- Pass cooldown days as parameter instead of reading config internally
- Convert standalone functions to methods on npmCooldownHandler
- Set Accept-Encoding: identity to prevent gzip responses breaking JSON parsing
- Return 503 with descriptive message when upstream body read fails

* fix: log errors in stripCooldownVersions instead of swallowing them

* fix: Config preserve fallback defaults

* fix: Code review fixes

* fix: correct cooldown tip to show wait time instead of incorrect trusted_packages advice

* fix: prevent integer overflow in cooldown duration calculation with large days values

* refactor: deduplicate CooldownBlock into internal/models, fix misleading variable names

- Move CooldownBlock struct to internal/models to eliminate duplication
  between proxy/interceptors and internal/ui packages
- Simplify proxy_flow.go by using direct assignment instead of field copy
- Rename latestStripped/latestDate to oldestVer/oldestDate for clarity

* fix: Dependency Cooldown Check Encapsulation (#207)

* fix: Encapsulate cooldown check

* feat: Add --skip-dependency-cooldown override

* fix: Code review fixes

---------

Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
2026-04-08 21:04:17 +05:30

156 lines
4.7 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfigIsNeverNil(t *testing.T) {
config := Get()
assert.NotNil(t, config)
}
func TestConfigHasDefaultValues(t *testing.T) {
t.Run("with non-existent config directory", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "/tmp/pmg-test/random-does-not-exist")
initConfig()
config := Get()
assert.Equal(t, true, config.Config.Transitive)
assert.Equal(t, 5, config.Config.TransitiveDepth)
assert.Equal(t, false, config.Config.IncludeDevDependencies)
assert.Equal(t, false, config.Config.Paranoid)
assert.Equal(t, []TrustedPackage{}, config.Config.TrustedPackages)
assert.Equal(t, "/tmp/pmg-test/random-does-not-exist", config.configDir)
assert.Equal(t, "/tmp/pmg-test/random-does-not-exist/config.yml", config.configFilePath)
})
t.Run("when no config directory is set", func(t *testing.T) {
t.Setenv("PMG_CONFIG_DIR", "")
initConfig()
config := Get()
userConfigDir, err := os.UserConfigDir()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, filepath.Join(userConfigDir, "safedep/pmg"), config.configDir)
assert.Equal(t, filepath.Join(userConfigDir, "safedep/pmg/config.yml"), config.configFilePath)
})
}
func TestPartialConfigFallsBackToDefaults(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("PMG_CONFIG_DIR", tmpDir)
configPath := filepath.Join(tmpDir, "config.yml")
// Write a minimal config that only sets a couple of fields,
// simulating a user who upgraded PMG without re-running setup
partialConfig := []byte("transitive: false\nparanoid: true\n")
err := os.WriteFile(configPath, partialConfig, 0o644)
require.NoError(t, err)
initConfig()
config := Get()
// Explicitly set values should be respected
assert.Equal(t, false, config.Config.Transitive)
assert.Equal(t, true, config.Config.Paranoid)
// Missing keys should fall back to DefaultConfig() values, not Go zero values
defaults := DefaultConfig().Config
assert.Equal(t, defaults.TransitiveDepth, config.Config.TransitiveDepth)
assert.Equal(t, defaults.ProxyMode, config.Config.ProxyMode)
assert.Equal(t, defaults.Verbosity, config.Config.Verbosity)
assert.Equal(t, defaults.EventLogRetentionDays, config.Config.EventLogRetentionDays)
assert.Equal(t, defaults.DependencyCooldown.Enabled, config.Config.DependencyCooldown.Enabled)
assert.Equal(t, defaults.DependencyCooldown.Days, config.Config.DependencyCooldown.Days)
assert.Equal(t, defaults.Sandbox.Enabled, config.Config.Sandbox.Enabled)
}
func TestPartialConfigWithNestedOverride(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("PMG_CONFIG_DIR", tmpDir)
configPath := filepath.Join(tmpDir, "config.yml")
// Override only one nested field; other nested and top-level fields should keep defaults
partialConfig := []byte("dependency_cooldown:\n days: 10\n")
err := os.WriteFile(configPath, partialConfig, 0o644)
require.NoError(t, err)
initConfig()
config := Get()
defaults := DefaultConfig().Config
// Explicitly set nested value should be respected
assert.Equal(t, 10, config.Config.DependencyCooldown.Days)
// Sibling nested field should fall back to default
assert.Equal(t, defaults.DependencyCooldown.Enabled, config.Config.DependencyCooldown.Enabled)
// Top-level fields should fall back to defaults
assert.Equal(t, defaults.Transitive, config.Config.Transitive)
assert.Equal(t, defaults.TransitiveDepth, config.Config.TransitiveDepth)
assert.Equal(t, defaults.ProxyMode, config.Config.ProxyMode)
}
func TestWriteTemplateConfigMergesExistingConfig(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("PMG_CONFIG_DIR", tmpDir)
configPath := filepath.Join(tmpDir, "config.yml")
// Write a partial user config
userConfig := []byte("transitive: false\ntransitive_depth: 10\n")
err := os.WriteFile(configPath, userConfig, 0o644)
require.NoError(t, err)
// Re-init so paths point to tmpDir
initConfig()
// Run WriteTemplateConfig — should merge, not skip
err = WriteTemplateConfig()
require.NoError(t, err)
// Read back
result, err := os.ReadFile(configPath)
require.NoError(t, err)
raw := string(result)
// User values preserved
assert.Contains(t, raw, "transitive: false")
assert.Contains(t, raw, "transitive_depth: 10")
// New keys from template added
assert.Contains(t, raw, "proxy_mode:")
assert.Contains(t, raw, "sandbox:")
assert.Contains(t, raw, "verbosity:")
}
func TestWriteTemplateConfigCreatesNewFile(t *testing.T) {
tmpDir := t.TempDir()
t.Setenv("PMG_CONFIG_DIR", tmpDir)
initConfig()
err := WriteTemplateConfig()
require.NoError(t, err)
configPath := filepath.Join(tmpDir, "config.yml")
result, err := os.ReadFile(configPath)
require.NoError(t, err)
// Should be the full template
assert.Equal(t, templateConfig, string(result))
}