|
4 | 4 | "context" |
5 | 5 | "database/sql" |
6 | 6 | "testing" |
| 7 | + |
| 8 | + "github.com/dvflw/mantle/internal/auth" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | // countTriggers returns the number of workflow_triggers rows for a workflow, |
@@ -190,6 +192,63 @@ func TestSave_BackfillsTriggersOnUnchangedApply(t *testing.T) { |
190 | 192 | } |
191 | 193 | } |
192 | 194 |
|
| 195 | +// TestSave_CronTriggersUniquePerTeam verifies that two teams can each register |
| 196 | +// a workflow with the same name and cron schedule. Before the uniqueness index |
| 197 | +// was team-scoped, the second team's apply failed with a unique-constraint |
| 198 | +// violation on (workflow_name, schedule). |
| 199 | +func TestSave_CronTriggersUniquePerTeam(t *testing.T) { |
| 200 | + database := setupTestDB(t) |
| 201 | + ctx := context.Background() |
| 202 | + |
| 203 | + // A second team — both workflow_definitions.team_id and |
| 204 | + // workflow_triggers.team_id are foreign keys to teams(id). |
| 205 | + teamB := "00000000-0000-0000-0000-0000000000b2" |
| 206 | + if _, err := database.ExecContext(ctx, |
| 207 | + `INSERT INTO teams (id, name) VALUES ($1, 'team-b')`, teamB); err != nil { |
| 208 | + t.Fatalf("creating team-b: %v", err) |
| 209 | + } |
| 210 | + |
| 211 | + yaml := []byte(`name: shared-name |
| 212 | +triggers: |
| 213 | + - type: cron |
| 214 | + schedule: "*/5 * * * *" |
| 215 | +steps: |
| 216 | + - name: s |
| 217 | + action: http/request |
| 218 | + params: |
| 219 | + method: GET |
| 220 | + url: "https://example.com" |
| 221 | +`) |
| 222 | + result, err := ParseBytes(yaml) |
| 223 | + if err != nil { |
| 224 | + t.Fatalf("ParseBytes: %v", err) |
| 225 | + } |
| 226 | + |
| 227 | + // Default team. |
| 228 | + if _, err := Save(ctx, database, result, yaml); err != nil { |
| 229 | + t.Fatalf("Save (default team): %v", err) |
| 230 | + } |
| 231 | + // team-b — same workflow name and schedule. |
| 232 | + ctxB := auth.WithUser(ctx, &auth.User{TeamID: teamB}) |
| 233 | + if _, err := Save(ctxB, database, result, yaml); err != nil { |
| 234 | + t.Fatalf("Save (team-b): %v", err) |
| 235 | + } |
| 236 | + |
| 237 | + // Each team owns exactly one cron trigger for the workflow. |
| 238 | + for _, tid := range []string{auth.DefaultTeamID, teamB} { |
| 239 | + var n int |
| 240 | + if err := database.QueryRowContext(ctx, |
| 241 | + `SELECT COUNT(*) FROM workflow_triggers |
| 242 | + WHERE workflow_name = 'shared-name' AND type = 'cron' AND team_id = $1`, tid, |
| 243 | + ).Scan(&n); err != nil { |
| 244 | + t.Fatalf("counting triggers for %s: %v", tid, err) |
| 245 | + } |
| 246 | + if n != 1 { |
| 247 | + t.Errorf("team %s cron triggers = %d, want 1", tid, n) |
| 248 | + } |
| 249 | + } |
| 250 | +} |
| 251 | + |
193 | 252 | // TestSave_BackfillPreservesDisabledState verifies that backfilling triggers |
194 | 253 | // for a currently-disabled workflow inserts them disabled, so a pruned |
195 | 254 | // workflow does not start firing on a re-apply. |
|
0 commit comments