103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadConfigAppliesBoundedHistoryDefaults(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "config.json")
|
|
raw := []byte(`{"http_base":"https://example.test","endpoints":{"a":["wss://a"],"b":["wss://b"]}}`)
|
|
if err := os.WriteFile(path, raw, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg, err := loadConfig(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if cfg.HistoryWorkers != 1 || cfg.HistoryPageSize != 50 || cfg.HistoryInterval != "100ms" ||
|
|
cfg.HistorySettle != "5m" || cfg.HistoryTimeout != "3m" ||
|
|
cfg.PreflightWorkers != 32 || cfg.WSPingInterval != "20s" {
|
|
t.Fatalf("unexpected defaults: %+v", cfg)
|
|
}
|
|
}
|
|
|
|
func TestNewManifestDistributesUsersAndEndpoints(t *testing.T) {
|
|
cfg := &Config{Tenant: "006", UsersPerMachine: 3, Endpoints: map[string][]string{
|
|
"a": {"ws://a1", "ws://a2"}, "b": {"ws://b1"},
|
|
}}
|
|
manifest := newManifest(cfg)
|
|
if len(manifest.Accounts) != 6 {
|
|
t.Fatalf("accounts=%d", len(manifest.Accounts))
|
|
}
|
|
if got := accountsForMachine(manifest, "a"); got[0].Username != "lta0001" || got[1].Endpoint != "ws://a2" || got[2].Endpoint != "ws://a1" {
|
|
t.Fatalf("unexpected A accounts: %+v", got)
|
|
}
|
|
if err := manifest.validate(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestManifestRejectsDuplicateAccount(t *testing.T) {
|
|
manifest := &Manifest{Version: manifestVersion, Accounts: []AccountRecord{
|
|
{Machine: "a", Index: 1, Username: "lta0001"},
|
|
{Machine: "a", Index: 1, Username: "duplicate"},
|
|
}}
|
|
if err := manifest.validate(); err == nil {
|
|
t.Fatal("expected duplicate account validation error")
|
|
}
|
|
}
|
|
|
|
func TestApplyConfigEndpointsRefreshesManifest(t *testing.T) {
|
|
cfg := &Config{Tenant: "006", UsersPerMachine: 1, Endpoints: map[string][]string{
|
|
"a": {"ws://new-a"}, "b": {"ws://new-b"},
|
|
}}
|
|
manifest := &Manifest{Tenant: "006", Accounts: []AccountRecord{
|
|
{Machine: "a", Index: 1, Username: "lta0001", Endpoint: "ws://old"},
|
|
{Machine: "b", Index: 1, Username: "ltb0001", Endpoint: "ws://old"},
|
|
}}
|
|
if err := applyConfigEndpoints(cfg, manifest); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if manifest.Accounts[0].Endpoint != "ws://new-a" || manifest.Accounts[1].Endpoint != "ws://new-b" {
|
|
t.Fatalf("endpoints were not refreshed: %+v", manifest.Accounts)
|
|
}
|
|
}
|
|
|
|
func TestRegistrationPhoneUsesDistinctMachineRanges(t *testing.T) {
|
|
cfg := &Config{UsersPerMachine: 1000, PhonePrefix: "1990927"}
|
|
a, err := registrationPhone(cfg, "a", 1)
|
|
if err != nil || a != "19909270001" {
|
|
t.Fatalf("machine A phone=%q err=%v", a, err)
|
|
}
|
|
b, err := registrationPhone(cfg, "b", 1)
|
|
if err != nil || b != "19909275001" {
|
|
t.Fatalf("machine B phone=%q err=%v", b, err)
|
|
}
|
|
last, err := registrationPhone(cfg, "b", 1000)
|
|
if err != nil || last != "19909276000" {
|
|
t.Fatalf("last phone=%q err=%v", last, err)
|
|
}
|
|
}
|
|
|
|
func TestReconcileManifestExpandsSmokeRun(t *testing.T) {
|
|
existing := &Manifest{Version: manifestVersion, Tenant: "allrich", GroupTopic: "grp-one", Accounts: []AccountRecord{
|
|
{Machine: "a", Index: 1, Username: "lta0001", UserID: "usr-a", Token: "token-a", P2PTopic: "p2p-one"},
|
|
{Machine: "b", Index: 1, Username: "ltb0001", UserID: "usr-b", Token: "token-b", P2PTopic: "p2p-one"},
|
|
}}
|
|
cfg := &Config{Tenant: "allrich", UsersPerMachine: 2, PhonePrefix: "1990927", Endpoints: map[string][]string{
|
|
"a": {"wss://a"}, "b": {"wss://b"},
|
|
}}
|
|
expanded, err := reconcileManifest(cfg, existing)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(expanded.Accounts) != 4 || expanded.GroupTopic != "grp-one" || expanded.Accounts[0].Token != "token-a" {
|
|
t.Fatalf("unexpected expanded manifest: %+v", expanded)
|
|
}
|
|
if expanded.Accounts[3].Phone != "19909275002" {
|
|
t.Fatalf("machine B phone=%q", expanded.Accounts[3].Phone)
|
|
}
|
|
}
|