Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
239 lines
6.1 KiB
Go
239 lines
6.1 KiB
Go
package automation
|
|
|
|
import "testing"
|
|
|
|
func TestValidateConditions_Empty(t *testing.T) {
|
|
err := ValidateConditions(Conditions{})
|
|
if err != nil {
|
|
t.Fatalf("expected no error for empty conditions, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_ValidSingleCondition(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for valid condition, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_InvalidAttribute(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "invalid_attribute",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid attribute, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_InvalidFilterOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "invalid_op",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid filter operator, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_InvalidQueryOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "invalid_op",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error for invalid query operator, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_MultipleValidConditions(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
{
|
|
Attribute: "priority",
|
|
FilterOperator: "not_equal",
|
|
Values: []string{"low"},
|
|
QueryOperator: "and",
|
|
},
|
|
{
|
|
Attribute: "assignee_id",
|
|
FilterOperator: "is_present",
|
|
Values: []string{},
|
|
QueryOperator: "or",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for multiple valid conditions, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_MixedValidAndInvalid(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"open"},
|
|
QueryOperator: "and",
|
|
},
|
|
{
|
|
Attribute: "bogus_attr",
|
|
FilterOperator: "equal",
|
|
Values: []string{"test"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err == nil {
|
|
t.Fatal("expected error when one condition has invalid attribute, got nil")
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AllWhitelistedAttributes(t *testing.T) {
|
|
for attr := range WhitelistedConditionAttributes {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: attr,
|
|
FilterOperator: "equal",
|
|
Values: []string{"test"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Errorf("expected valid for attribute %q, got error: %v", attr, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AllWhitelistedFilterOperators(t *testing.T) {
|
|
for op := range WhitelistedFilterOperators {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: op,
|
|
Values: []string{"test"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Errorf("expected valid for filter_operator %q, got error: %v", op, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AllWhitelistedQueryOperators(t *testing.T) {
|
|
for op := range WhitelistedQueryOperators {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "equal",
|
|
Values: []string{"test"},
|
|
QueryOperator: op,
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Errorf("expected valid for query_operator %q, got error: %v", op, err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_ContainsOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "content",
|
|
FilterOperator: "contains",
|
|
Values: []string{"help"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for contains operator, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_ChatwootMessageFilterOperators(t *testing.T) {
|
|
valid := Conditions{
|
|
{Attribute: "message_type", FilterOperator: "equal_to", Values: []string{"0"}},
|
|
{Attribute: "private_note", FilterOperator: "not_equal_to", Values: []string{"true"}},
|
|
{Attribute: "content", FilterOperator: "does_not_contain", Values: []string{"spam"}},
|
|
}
|
|
if err := ValidateConditions(valid); err != nil {
|
|
t.Fatalf("expected Chatwoot message filter operators to validate, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_RejectsUnsupportedMessageFilterOperators(t *testing.T) {
|
|
tests := []Condition{
|
|
{Attribute: "message_type", FilterOperator: "contains", Values: []string{"incoming"}},
|
|
{Attribute: "private_note", FilterOperator: "contains", Values: []string{"true"}},
|
|
{Attribute: "content", FilterOperator: "is_present", Values: []string{}},
|
|
}
|
|
for _, condition := range tests {
|
|
if err := ValidateConditions(Conditions{condition}); err == nil {
|
|
t.Fatalf("expected invalid operator for %s/%s", condition.Attribute, condition.FilterOperator)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_AttributeChangedOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "status",
|
|
FilterOperator: "attribute_changed",
|
|
Values: []string{"open", "resolved"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for attribute_changed operator, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestValidateConditions_DoesNotContainOperator(t *testing.T) {
|
|
conditions := Conditions{
|
|
{
|
|
Attribute: "labels",
|
|
FilterOperator: "does_not_contain",
|
|
Values: []string{"spam"},
|
|
QueryOperator: "and",
|
|
},
|
|
}
|
|
err := ValidateConditions(conditions)
|
|
if err != nil {
|
|
t.Fatalf("expected no error for does_not_contain operator, got: %v", err)
|
|
}
|
|
}
|