package main import ( "context" "encoding/json" "strings" "testing" . "claude-licence-manager/internal/licences" ) // design 39 bus-and-switch §15, §16, §20: a rotation reads the grant from the vault and hands it back, the grant first // and then the access, at the next generation; the licence keeps no token once the call returns. func TestARotationPutsTwiceAndKeepsNoToken(t *testing.T) { mm := newMesh(t) ctx := context.Background() if _, err := mm.m.Consider(ctx, []Holdings{mm.login("laptop", "rt-a", true, t0)}); err != nil { t.Fatal(err) } key := KeyFor(licence1) mm.grants.puts = nil if r, err := mm.m.Rotate(ctx, licence1, true); err != nil || r["refreshed"] != true { t.Fatalf("%v %v", r, err) } want := []string{SecretName(key, PartGrant) + "@2", SecretName(key, PartAccess) + "@2"} if strings.Join(mm.grants.puts, " ") != strings.Join(want, " ") { t.Fatalf("the rotation put %v, want %v", mm.grants.puts, want) } if g, _ := mm.grants.Get(ctx, key, PartGrant); !strings.Contains(g, `"refreshToken":"rt-2"`) { t.Fatalf("the vault holds the grant %s", g) } if a, _ := mm.grants.Get(ctx, key, PartAccess); !strings.Contains(a, `"accessToken":"at-2"`) || strings.Contains(a, "refreshToken\":\"rt") { t.Fatalf("the vault holds the access %s", a) } l, _ := mm.store.Licence(ctx, licence1) raw, _ := json.Marshal(l) for _, token := range []string{"rt-", "at-"} { if strings.Contains(string(raw), token) { t.Fatalf("the licence keeps %s: %s", token, raw) } } if l.Generation != 2 { t.Fatalf("the licence's generation is %d, want 2", l.Generation) } }