package workercluster import ( "encoding/json" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TestEntries_RoundTrip encodes an Entry with each payload populated, // decodes it, and verifies the wire shape is stable. func TestEntries_RoundTrip(t *testing.T) { adopted, err := json.Marshal(ConfigAdoptPayload{Version: 7, Checks: []CriticalCheckConfig{{ID: 1}}}) require.NoError(t, err) member, err := json.Marshal(MemberPayload{Member: Member{WorkerID: "w-1", Role: "voter"}}) require.NoError(t, err) outbox, err := json.Marshal(OutboxPayload{Entry: OutboxMeta{IncidentID: 1, Channel: "telegram"}}) require.NoError(t, err) observe, err := json.Marshal(IncidentObservePayload{CheckID: 1, WorkerID: "w-1", Label: "ok"}) require.NoError(t, err) state, err := json.Marshal(IncidentTransitionPayload{CheckID: 1, State: IncidentState{CheckID: 1, State: "open"}}) require.NoError(t, err) report, err := json.Marshal(PartitionReportPayload{State: PartitionState{State: "steady"}}) require.NoError(t, err) diag, err := json.Marshal(DiagnosticsPayload{Diag: WorkerDiagnostics{WorkerID: "w-1"}}) require.NoError(t, err) set, err := json.Marshal(ObserverSetPayload{Set: ObserverSet{Version: 3, Voters: []string{"w-1", "w-2", "w-3"}}}) require.NoError(t, err) entry := Entry{ Type: EntryConfigAdopt, Version: 7, Adopted: adopted, Member: member, Outbox: outbox, Observe: observe, State: state, Report: report, Diag: diag, Set: set, } raw, err := EncodeEntry(&entry) require.NoError(t, err) got, err := DecodeEntry(raw) require.NoError(t, err) assert.Equal(t, entry.Type, got.Type) assert.JSONEq(t, string(adopted), string(got.Adopted)) assert.JSONEq(t, string(member), string(got.Member)) assert.JSONEq(t, string(outbox), string(got.Outbox)) assert.JSONEq(t, string(observe), string(got.Observe)) assert.JSONEq(t, string(state), string(got.State)) assert.JSONEq(t, string(report), string(got.Report)) assert.JSONEq(t, string(diag), string(got.Diag)) assert.JSONEq(t, string(set), string(got.Set)) } // TestEntries_StableTypeStrings locks down the wire-format strings so // future phases can extend the enum without breaking on-the-wire // compatibility for the kinds we already ship. func TestEntries_StableTypeStrings(t *testing.T) { expected := map[EntryType]string{ EntryConfigAdopt: "config.adopt", EntryObserverSetUpdate: "observer_set.update", EntryMembershipProposeAdd: "membership.propose_add", EntryMembershipDemote: "membership.demote", EntryMembershipRemove: "membership.remove", EntryIncidentObserve: "incident.observe", EntryIncidentTransition: "incident.transition", EntryOutboxEnqueue: "outbox.enqueue", EntryOutboxDelivered: "outbox.delivered", EntryOutboxAck: "outbox.ack", EntryPartitionReport: "partition.report", EntryDiagnosticsUpdate: "diagnostics.update", } for k, v := range expected { assert.Equal(t, v, string(k), "entry type wire string") } }