feat(canonical): validate profile name, apply env-var substitution before render

This commit is contained in:
2026-05-23 11:55:26 +01:00
parent 5e35dc47fe
commit 50059d7517
14 changed files with 423 additions and 23 deletions
+54
View File
@@ -49,3 +49,57 @@ func TestInitImportWritesCanonical(t *testing.T) {
_, err := os.Stat(filepath.Join(root, ".git"))
require.NoError(t, err)
}
func makeStubReg() *adapter.Registry {
reg := adapter.NewRegistry()
reg.Register(&importableAdapter{
name: "stub",
res: &adapter.ImportResult{},
})
return reg
}
// TestInitRefusesOverwriteWithoutForce: second init without --force must error.
func TestInitRefusesOverwriteWithoutForce(t *testing.T) {
root := t.TempDir()
home := t.TempDir()
// First init — should succeed.
cmd := NewInit(makeStubReg())
cmd.SetArgs([]string{"--root", root, "--home", home, "--from", "stub", "--no-prompt"})
require.NoError(t, cmd.Execute())
// Second init — must fail without --force.
cmd2 := NewInit(makeStubReg())
cmd2.SetArgs([]string{"--root", root, "--home", home, "--from", "stub", "--no-prompt"})
err := cmd2.Execute()
require.Error(t, err)
assert.Contains(t, err.Error(), "already initialised")
assert.Contains(t, err.Error(), "--force")
}
// TestInitForcePreservesExistingProfile: --force re-imports but does NOT overwrite imported.yaml.
func TestInitForcePreservesExistingProfile(t *testing.T) {
root := t.TempDir()
home := t.TempDir()
// First init writes placeholder profile.
cmd := NewInit(makeStubReg())
cmd.SetArgs([]string{"--root", root, "--home", home, "--from", "stub", "--no-prompt"})
require.NoError(t, cmd.Execute())
// User edits the profile.
profPath := filepath.Join(root, "profiles", "imported.yaml")
userEdit := []byte("name: imported\ngateway:\n url: \"https://my.gateway\"\n token: \"tok\"\n default_model: \"m\"\nmodels: []\n")
require.NoError(t, os.WriteFile(profPath, userEdit, 0o600))
// Re-init with --force.
cmd2 := NewInit(makeStubReg())
cmd2.SetArgs([]string{"--root", root, "--home", home, "--from", "stub", "--no-prompt", "--force"})
require.NoError(t, cmd2.Execute())
// User edits must be preserved.
got, err := os.ReadFile(profPath)
require.NoError(t, err)
assert.Equal(t, string(userEdit), string(got))
}