Files
sam af7577625f
deploy / plan (push) Successful in 11s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 7m26s
refactor(messaging): store Telegram's connection in extension-settings (#1802)
2026-11-08 17:12:59 +11:00

69 lines
2.6 KiB
Go

package controlplanestore
import (
"encoding/json"
"reflect"
"testing"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/controlplanerpc"
)
func TestSplitLegacyChannelSettings(t *testing.T) {
legacy := `{"operator":"sam","telegram":{"allowed_user_ids":[1,2],"token_ref":{"engine":"env","key":"BOT"},"credential_configured":true}}`
tests := []struct {
name, org, stored string
wantChanged bool
wantMoved telegramConnection
wantDoc string
}{
{
"instance connection moves out", storecontract.DefaultOrgID, legacy, true,
telegramConnection{Token: config.SecretRef{Engine: "env", Key: "BOT"}, Users: []int64{1, 2}},
`{"operator":"sam"}`,
},
{"org keeps its users flat and moves nothing", "acme", legacy, true, telegramConnection{}, `{"allowed_user_ids":[1,2],"operator":"sam"}`},
{"migrated document is left alone", storecontract.DefaultOrgID, `{"operator":"sam"}`, false, telegramConnection{}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc, moved, changed, err := splitLegacyChannelSettings(tt.org, []byte(tt.stored))
if err != nil {
t.Fatal(err)
}
if changed != tt.wantChanged || !reflect.DeepEqual(moved, tt.wantMoved) || string(doc) != tt.wantDoc {
t.Fatalf("got (%s, %+v, %v), want (%s, %+v, %v)", doc, moved, changed, tt.wantDoc, tt.wantMoved, tt.wantChanged)
}
})
}
}
func TestWithTelegramConnection(t *testing.T) {
moved := telegramConnection{Token: config.SecretRef{Engine: "env", Key: "BOT"}, Users: []int64{1, 2}}
tests := []struct {
name string
have []controlplanerpc.ExtensionSetting
want controlplanerpc.ExtensionSetting
}{
{
"enabled when the package has no entry", nil,
controlplanerpc.ExtensionSetting{Name: "telegram", Enabled: true, Settings: map[string]string{"token_ref": "env:BOT", "allowed_user_ids": "1,2"}},
},
{
"values already set win and enabled state is kept",
[]controlplanerpc.ExtensionSetting{{Name: "telegram", Settings: map[string]string{"token_ref": "bws:OTHER"}}},
controlplanerpc.ExtensionSetting{Name: "telegram", Settings: map[string]string{"token_ref": "bws:OTHER", "allowed_user_ids": "1,2"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := withTelegramConnection(controlplanerpc.ExtensionSettings{Extensions: tt.have}, moved)
if len(got.Extensions) != 1 || !reflect.DeepEqual(got.Extensions[0], tt.want) {
encoded, _ := json.Marshal(got)
t.Fatalf("got %s, want %+v", encoded, tt.want)
}
})
}
}