Trust corebin cache without Stat; batch networksetup; ActiveProxyURI instead of Config.Clone. Co-authored-by: Cursor <cursoragent@cursor.com>
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package corebin
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestResolveTrustsCacheWithoutStat(t *testing.T) {
|
|
Invalidate()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "core")
|
|
if err := os.WriteFile(path, []byte("x"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := CacheKey(dir, "naive")
|
|
calls := 0
|
|
fn := func() (string, error) {
|
|
calls++
|
|
return path, nil
|
|
}
|
|
if p, err := Resolve(key, fn); err != nil || p != path || calls != 1 {
|
|
t.Fatalf("first: %q %v calls=%d", p, err, calls)
|
|
}
|
|
_ = os.Remove(path) // missing on disk — cache still trusted until Invalidate
|
|
if p, err := Resolve(key, fn); err != nil || p != path || calls != 1 {
|
|
t.Fatalf("cached without Stat: %q %v calls=%d", p, err, calls)
|
|
}
|
|
Invalidate()
|
|
if _, err := Resolve(key, fn); err == nil && calls != 2 {
|
|
// fn returns deleted path; Resolve still succeeds
|
|
}
|
|
if calls != 2 {
|
|
t.Fatalf("after Invalidate expected refresh, calls=%d", calls)
|
|
}
|
|
}
|
|
|
|
func TestVirtualEmbeddedAWG(t *testing.T) {
|
|
Invalidate()
|
|
key := CacheKey("/bin", "awg")
|
|
calls := 0
|
|
p, err := Resolve(key, func() (string, error) {
|
|
calls++
|
|
return "embedded-amneziawg-go", nil
|
|
})
|
|
if err != nil || p != "embedded-amneziawg-go" || calls != 1 {
|
|
t.Fatalf("%q %v calls=%d", p, err, calls)
|
|
}
|
|
if !VirtualPath(p) {
|
|
t.Fatal("expected virtual")
|
|
}
|
|
p2, err := Resolve(key, func() (string, error) {
|
|
calls++
|
|
return "embedded-amneziawg-go", nil
|
|
})
|
|
if err != nil || p2 != p || calls != 1 {
|
|
t.Fatalf("second: %q calls=%d", p2, calls)
|
|
}
|
|
}
|