package mysql

import (
	"context"
	"regexp"
	"testing"

	"github.com/DATA-DOG/go-sqlmock"
)

func TestListUserOutboxAppCodesForcesPrimaryPrefixIndex(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatalf("create sqlmock: %v", err)
	}
	defer db.Close()
	mock.ExpectQuery(regexp.QuoteMeta(`SELECT DISTINCT app_code FROM user_outbox FORCE INDEX (PRIMARY) ORDER BY app_code`)).
		WillReturnRows(sqlmock.NewRows([]string{"app_code"}).AddRow("fami").AddRow("huwaa").AddRow("lalu"))

	apps, err := (&Repository{db: db}).ListUserOutboxAppCodes(context.Background())
	if err != nil {
		t.Fatalf("list user outbox apps: %v", err)
	}
	if len(apps) != 3 || apps[0] != "fami" || apps[1] != "huwaa" || apps[2] != "lalu" {
		t.Fatalf("user outbox apps mismatch: %v", apps)
	}
	if err := mock.ExpectationsWereMet(); err != nil {
		t.Fatalf("user app discovery query mismatch: %v", err)
	}
}
