209 lines
8.7 KiB
Go
209 lines
8.7 KiB
Go
// Package postgres contains source-level contract tests for the
|
|
// question_table_exports outbox: the store must enqueue inside
|
|
// FinishTurn's transaction, guarded by shouldCreateAnswerMessage.
|
|
// No database connection is required.
|
|
package postgres
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
|
)
|
|
|
|
// TestFinishTurnBeginTurnSnapshot verifies that the BeginTurn INSERT
|
|
// into messages includes trigger_person_snapshot, actor_union_id_snapshot,
|
|
// department_snapshot, and region_snapshot columns.
|
|
func TestFinishTurnBeginTurnSnapshot(t *testing.T) {
|
|
data, err := os.ReadFile("store.go")
|
|
if err != nil {
|
|
t.Fatalf("read store.go: %v", err)
|
|
}
|
|
src := string(data)
|
|
|
|
// The BeginTurn message INSERT must include trigger_person_snapshot
|
|
if !strings.Contains(src, "trigger_person_snapshot") {
|
|
t.Error("BeginTurn message INSERT must include trigger_person_snapshot column")
|
|
}
|
|
if !strings.Contains(src, "actor_union_id_snapshot") {
|
|
t.Error("BeginTurn message INSERT must include actor_union_id_snapshot column")
|
|
}
|
|
if !strings.Contains(src, "department_snapshot") {
|
|
t.Error("BeginTurn message INSERT must include department_snapshot column")
|
|
}
|
|
if !strings.Contains(src, "region_snapshot") {
|
|
t.Error("BeginTurn message INSERT must include region_snapshot column")
|
|
}
|
|
|
|
// The VALUES arguments must include turn.ActorUnionIDSnapshot
|
|
if !strings.Contains(src, "turn.ActorUnionIDSnapshot") {
|
|
t.Error("BeginTurn message INSERT VALUES must include turn.ActorUnionIDSnapshot argument")
|
|
}
|
|
}
|
|
|
|
func TestShouldEnqueueQuestionTableExportOnlyForDingTalkSucceededAnswers(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
turn core.Turn
|
|
want bool
|
|
}{
|
|
{name: "dingtalk success", turn: core.Turn{Channel: "dingtalk", Status: "succeeded", Answer: "answer", AnswerMessageID: "answer-1"}, want: true},
|
|
{name: "dingtalk case and space", turn: core.Turn{Channel: " DingTalk ", Status: "succeeded", Answer: "answer", AnswerMessageID: "answer-1"}, want: true},
|
|
{name: "web success", turn: core.Turn{Channel: "web", Status: "succeeded", Answer: "answer", AnswerMessageID: "answer-1"}},
|
|
{name: "http success", turn: core.Turn{Channel: "http", Status: "succeeded", Answer: "answer", AnswerMessageID: "answer-1"}},
|
|
{name: "empty channel", turn: core.Turn{Status: "succeeded", Answer: "answer", AnswerMessageID: "answer-1"}},
|
|
{name: "dingtalk failed", turn: core.Turn{Channel: "dingtalk", Status: "failed", Answer: "answer", AnswerMessageID: "answer-1"}},
|
|
{name: "dingtalk empty answer", turn: core.Turn{Channel: "dingtalk", Status: "succeeded", Answer: " ", AnswerMessageID: "answer-1"}},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := shouldEnqueueQuestionTableExport(tc.turn, true); got != tc.want {
|
|
t.Fatalf("shouldEnqueueQuestionTableExport()=%v, want %v", got, tc.want)
|
|
}
|
|
if got := shouldEnqueueQuestionTableExport(tc.turn, false); got {
|
|
t.Fatal("disabled question-table export must not enqueue")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestFinishTurnOutboxInsideTransaction verifies that the
|
|
// question_table_exports INSERT is inside the FinishTurn transaction
|
|
// (between tx begin and tx commit), uses 'pending' status, and has
|
|
// ON CONFLICT (tenant_key,channel,turn_id) DO NOTHING.
|
|
func TestFinishTurnOutboxInsideTransaction(t *testing.T) {
|
|
data, err := os.ReadFile("store.go")
|
|
if err != nil {
|
|
t.Fatalf("read store.go: %v", err)
|
|
}
|
|
src := string(data)
|
|
|
|
// Extract the FinishTurn function body using deterministic boundaries
|
|
finishStart := strings.Index(src, "func (s *Store) FinishTurn(")
|
|
if finishStart < 0 {
|
|
t.Fatal("cannot find FinishTurn signature")
|
|
}
|
|
finishEnd := strings.Index(src[finishStart:], "func (s *Store) RecordDelivery(")
|
|
if finishEnd < 0 {
|
|
t.Fatal("cannot find RecordDelivery signature after FinishTurn")
|
|
}
|
|
body := src[finishStart : finishStart+finishEnd]
|
|
|
|
// The outbox INSERT must use question_table_exports
|
|
if !strings.Contains(body, "question_table_exports") {
|
|
t.Error("FinishTurn must INSERT into question_table_exports")
|
|
}
|
|
|
|
// Must use 'pending' status for the outbox
|
|
if !strings.Contains(body, "'pending'") {
|
|
t.Error("question_table_exports INSERT must use status='pending'")
|
|
}
|
|
|
|
// Must use ON CONFLICT (tenant_key,channel,turn_id) DO NOTHING
|
|
if !strings.Contains(body, "ON CONFLICT (tenant_key,channel,turn_id) DO NOTHING") &&
|
|
!strings.Contains(body, "ON CONFLICT (tenant_key, channel, turn_id) DO NOTHING") {
|
|
t.Error("question_table_exports INSERT must use ON CONFLICT (tenant_key,channel,turn_id) DO NOTHING")
|
|
}
|
|
|
|
// The outbox INSERT must be after the tx begin and before tx commit
|
|
txBegin := strings.Index(body, "BeginTx")
|
|
txCommit := strings.Index(body, "tx.Commit")
|
|
outboxPos := strings.Index(body, "question_table_exports")
|
|
if txBegin < 0 || txCommit < 0 || outboxPos < 0 {
|
|
t.Fatal("cannot locate transaction boundaries or outbox insert")
|
|
}
|
|
if outboxPos < txBegin || outboxPos > txCommit {
|
|
t.Error("question_table_exports INSERT must be inside the transaction (between BeginTx and Commit)")
|
|
}
|
|
}
|
|
|
|
// TestFinishTurnOutboxGuardedByShouldCreateAnswerMessage verifies that
|
|
// the outbox INSERT is structurally inside the shouldCreateAnswerMessage
|
|
// guard. Failed, processing, and empty-answer turns must not enqueue.
|
|
func TestFinishTurnOutboxGuardedByShouldCreateAnswerMessage(t *testing.T) {
|
|
data, err := os.ReadFile("store.go")
|
|
if err != nil {
|
|
t.Fatalf("read store.go: %v", err)
|
|
}
|
|
src := string(data)
|
|
|
|
// The shouldCreateAnswerMessage function must guard on status succeeded,
|
|
// non-empty answer, and non-empty answer message ID
|
|
if !strings.Contains(src, "shouldCreateAnswerMessage") {
|
|
t.Fatal("shouldCreateAnswerMessage function must exist")
|
|
}
|
|
|
|
// Extract shouldCreateAnswerMessage function body using deterministic boundaries
|
|
shouldFuncStart := strings.Index(src, "func shouldCreateAnswerMessage(")
|
|
if shouldFuncStart < 0 {
|
|
t.Fatal("cannot find shouldCreateAnswerMessage signature")
|
|
}
|
|
// Find the closing brace of the function: the first top-level `}` after the signature
|
|
// The function body is simple: one return statement, so find the `}` that follows
|
|
shouldFuncBodyStart := strings.Index(src[shouldFuncStart:], "{")
|
|
if shouldFuncBodyStart < 0 {
|
|
t.Fatal("cannot find shouldCreateAnswerMessage opening brace")
|
|
}
|
|
shouldFuncBodyStart += shouldFuncStart + 1 // position after "{"
|
|
shouldFuncEnd := strings.Index(src[shouldFuncBodyStart:], "\n}\n")
|
|
if shouldFuncEnd < 0 {
|
|
t.Fatal("cannot find shouldCreateAnswerMessage closing brace")
|
|
}
|
|
shouldFuncEnd += shouldFuncBodyStart + 1 // position of the closing "}"
|
|
shouldBody := src[shouldFuncBodyStart:shouldFuncEnd]
|
|
|
|
if !strings.Contains(shouldBody, "succeeded") {
|
|
t.Error("shouldCreateAnswerMessage must check status == succeeded")
|
|
}
|
|
if !strings.Contains(shouldBody, "turn.Answer") {
|
|
t.Error("shouldCreateAnswerMessage must check non-empty answer")
|
|
}
|
|
if !strings.Contains(shouldBody, "turn.AnswerMessageID") {
|
|
t.Error("shouldCreateAnswerMessage must check non-empty AnswerMessageID")
|
|
}
|
|
|
|
// In FinishTurn, extract the guarded block using deterministic boundaries
|
|
finishStart := strings.Index(src, "func (s *Store) FinishTurn(")
|
|
if finishStart < 0 {
|
|
t.Fatal("cannot find FinishTurn signature")
|
|
}
|
|
finishEnd := strings.Index(src[finishStart:], "func (s *Store) RecordDelivery(")
|
|
if finishEnd < 0 {
|
|
t.Fatal("cannot find RecordDelivery signature after FinishTurn")
|
|
}
|
|
finishBody := src[finishStart : finishStart+finishEnd]
|
|
|
|
// Locate the guarded block: start at "if shouldCreateAnswerMessage(turn) {"
|
|
guardStart := strings.Index(finishBody, "if shouldCreateAnswerMessage(turn) {")
|
|
if guardStart < 0 {
|
|
t.Fatal("cannot find shouldCreateAnswerMessage guard in FinishTurn")
|
|
}
|
|
|
|
// The guarded block ends before the "UPDATE turns SET" statement that follows it
|
|
guardEnd := strings.Index(finishBody[guardStart:], "UPDATE turns SET")
|
|
if guardEnd < 0 {
|
|
t.Fatal("cannot find UPDATE turns SET after guarded block")
|
|
}
|
|
guardBlock := finishBody[guardStart : guardStart+guardEnd]
|
|
|
|
if !strings.Contains(guardBlock, "question_table_exports") {
|
|
t.Error("question_table_exports INSERT must be inside the shouldCreateAnswerMessage guard block")
|
|
}
|
|
if !strings.Contains(guardBlock, "if shouldEnqueueQuestionTableExport(turn, s.questionTableExportEnabled) {") {
|
|
t.Error("question_table_exports INSERT must require both runtime enablement and a DingTalk turn")
|
|
}
|
|
|
|
// The guard block must be the one that contains the messages INSERT
|
|
// (answer message creation), and after that the outbox INSERT
|
|
msgInsert := strings.Index(guardBlock, "INSERT INTO messages")
|
|
outboxInsert := strings.Index(guardBlock, "question_table_exports")
|
|
if msgInsert < 0 || outboxInsert < 0 {
|
|
t.Fatal("cannot locate messages INSERT or outbox INSERT in guard block")
|
|
}
|
|
if outboxInsert < msgInsert {
|
|
t.Error("outbox INSERT should come after answer message INSERT")
|
|
}
|
|
}
|