53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"git.ipao.vip/rogee/go-sip/internal/testfixture"
|
|
"testing"
|
|
)
|
|
|
|
func TestBusinessDuplicateGetsOwnReceiptWithoutNewTask(t *testing.T) {
|
|
s := testStore(t)
|
|
raw, err := testfixture.Execute()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := testfixture.InboundKey("tenant-demo-key")
|
|
if _, err := s.IngestCommand(raw, key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var envelope map[string]any
|
|
if err := json.Unmarshal(raw, &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
envelope["command_id"] = "second-command"
|
|
raw, err = json.Marshal(envelope)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := s.IngestCommand(raw, key)
|
|
if err != nil || !result.Duplicate {
|
|
t.Fatalf("duplicate: %+v %v", result, err)
|
|
}
|
|
var tasks, receipts int
|
|
if err := s.DB().QueryRow(`SELECT COUNT(*) FROM tasks`).Scan(&tasks); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.DB().QueryRow(`SELECT COUNT(*) FROM outbox WHERE event_id='second-command-result'`).Scan(&receipts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tasks != 1 || receipts != 1 {
|
|
t.Fatalf("tasks=%d receipts=%d", tasks, receipts)
|
|
}
|
|
envelope["command_id"] = "changed-business-command"
|
|
envelope["payload"].(map[string]any)["callee"] = "15830461047"
|
|
changed, err := json.Marshal(envelope)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.IngestCommand(changed, key); !errors.Is(err, ErrCommandConflict) {
|
|
t.Fatalf("changed business payload accepted: %v", err)
|
|
}
|
|
}
|