Skip to content

Commit 78f08f6

Browse files
ampagentPierrunoYT
andcommitted
test(config): clarify provider transaction publication results
Describe whole-document repair publication accurately and document committed results on lock release errors. Strengthen publication-failure coverage with an existing provider row, zero result, unchanged config, and restored credential assertions. Removing the zero-result guard fails the targeted regression. Amp-Thread-ID: https://ampcode.com/threads/T-01a0e862-4304-73bb-9aee-5e56cb537f3a Co-authored-by: Pierre Bruno <pierrebruno@hotmail.ch>
1 parent 7e754ae commit 78f08f6

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎internal/config/provider_commit.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ var acquireProviderWriteLock = lockProviderWrite
4848
// config stays invalid because the row cannot be removed. Operations that ADD
4949
// or publish state pass false, so a new profile or credential is never written
5050
// into a config whose persisted names are already ambiguous. When it is true
51-
// the publish also goes through writeProviderNameRepair, which rewrites only
52-
// the fields this operation changed instead of restating the whole file.
51+
// the publish goes through writeProviderNameRepair, which re-encodes the whole
52+
// config but permits remaining invalid names only when the operation strictly
53+
// reduces existing name problems without introducing or worsening another.
5354
func runProviderProfileOperation(path string, allowMissing bool, allowInvalidInput bool, mutate func(*providerProfileOperation) error) (result FileConfig, err error) {
5455
path = strings.TrimSpace(path)
5556
if path == "" {
@@ -229,6 +230,8 @@ type ProviderCommit struct {
229230
// ProviderCommitResult reports the config as written and the profile exactly as
230231
// it was persisted: the adopted name, and the key moved into the credential
231232
// store (APIKey cleared, APIKeyStored set) unless KeepStoredKey was requested.
233+
// A nonzero result alongside an error means publication succeeded but lock
234+
// release failed; a rejected mutation or publication returns the zero result.
232235
type ProviderCommitResult struct {
233236
Config FileConfig
234237
Persisted ProviderProfile

‎internal/config/provider_commit_test.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/exec"
88
"path/filepath"
9+
"reflect"
910
"runtime"
1011
"strings"
1112
"sync"
@@ -458,6 +459,8 @@ func TestCommitProviderProfileRollsBackKeyWhenPublicationFails(t *testing.T) {
458459
dir := t.TempDir()
459460
t.Setenv("ZERO_CRED_STORAGE", "encrypted-file")
460461
path := filepath.Join(dir, "config.json")
462+
before := FileConfig{Providers: []ProviderProfile{{Name: "work", APIKeyStored: true}}}
463+
writeConfigFixture(t, path, before, 0o600)
461464
store, err := ProviderKeyStoreAt(dir)
462465
if err != nil {
463466
t.Fatal(err)
@@ -469,9 +472,16 @@ func TestCommitProviderProfileRollsBackKeyWhenPublicationFails(t *testing.T) {
469472
publishProviderConfig = func(string, FileConfig) error { return errors.New("disk full") }
470473
t.Cleanup(func() { publishProviderConfig = oldPublish })
471474

472-
if _, err := CommitProviderProfile(path, ProviderCommit{Profile: ProviderProfile{Name: "work", APIKey: "sk-new"}}); err == nil {
475+
result, err := CommitProviderProfile(path, ProviderCommit{Profile: ProviderProfile{Name: "work", APIKey: "sk-new"}})
476+
if err == nil {
473477
t.Fatal("CommitProviderProfile error = nil, want publication failure")
474478
}
479+
if !reflect.DeepEqual(result, ProviderCommitResult{}) {
480+
t.Fatalf("rejected publication returned a committed result: %+v", result)
481+
}
482+
if got := readConfigFixture(t, path); !reflect.DeepEqual(got, before) {
483+
t.Fatalf("rejected publication changed config: %+v", got)
484+
}
475485
if key, ok, err := store.Get("work"); err != nil || !ok || key != "sk-original" {
476486
t.Fatalf("rollback key = %q ok=%v err=%v, want original", key, ok, err)
477487
}

0 commit comments

Comments
 (0)