Private
Public Access
CI / rust (push) Successful in 3m6s
CI / go (push) Successful in 33s
CI / js (push) Successful in 34s
Standalone clients that resolve a machine unique ID (OS machine ID first, falling back to a generated UUID persisted to disk), detect hostname/IP/ container/orchestrator, and register+re-ping origin.warky.dev's public service-checkin endpoint on a daily interval. Ported from icy2's internal origincheckin package but extended to the full check-in contract and made dependency-light for external reuse. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package originclient
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func withStubMachineID(t *testing.T, id string, err error) {
|
|
t.Helper()
|
|
original := machineIDFunc
|
|
machineIDFunc = func() (string, error) { return id, err }
|
|
t.Cleanup(func() { machineIDFunc = original })
|
|
}
|
|
|
|
func TestResolveMachineID_ExplicitOverrideWins(t *testing.T) {
|
|
withStubMachineID(t, "os-machine-id", nil)
|
|
got := ResolveMachineID("explicit-id", filepath.Join(t.TempDir(), "install-id"), nil)
|
|
assert.Equal(t, "explicit-id", got)
|
|
}
|
|
|
|
func TestResolveMachineID_UsesOSMachineIDWhenAvailable(t *testing.T) {
|
|
withStubMachineID(t, "os-machine-id", nil)
|
|
got := ResolveMachineID("", filepath.Join(t.TempDir(), "install-id"), nil)
|
|
assert.Equal(t, "os-machine-id", got)
|
|
}
|
|
|
|
func TestResolveMachineID_FallsBackAndPersistsWhenOSIDUnavailable(t *testing.T) {
|
|
withStubMachineID(t, "", errMachineIDUnavailable)
|
|
path := filepath.Join(t.TempDir(), "nested", "install-id")
|
|
|
|
first := ResolveMachineID("", path, nil)
|
|
assert.NotEmpty(t, first)
|
|
|
|
second := ResolveMachineID("", path, nil)
|
|
assert.Equal(t, first, second, "second call should read the persisted id rather than generating a new one")
|
|
}
|
|
|
|
func TestResolveMachineID_NilLoggerDoesNotPanic(t *testing.T) {
|
|
withStubMachineID(t, "", errMachineIDUnavailable)
|
|
assert.NotPanics(t, func() {
|
|
ResolveMachineID("", filepath.Join(t.TempDir(), "install-id"), nil)
|
|
})
|
|
}
|
|
|
|
func TestResolveMachineID_LogsWarningWhenOSIDUnavailable(t *testing.T) {
|
|
withStubMachineID(t, "", errMachineIDUnavailable)
|
|
var warned bool
|
|
logger := LoggerFunc(func(msg string, err error) { warned = true })
|
|
|
|
ResolveMachineID("", filepath.Join(t.TempDir(), "install-id"), logger)
|
|
|
|
assert.True(t, warned)
|
|
}
|
|
|
|
func TestPersistInstallID(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "nested", "dir", "install-id")
|
|
require.NoError(t, persistInstallID(path, "abc-123"))
|
|
}
|