Repository navigation
Expand file tree
/
Copy paththread_test.go
More file actions
2565 lines (2189 loc) Β· 67.9 KB
/
Copy paththread_test.go
File metadata and controls
2565 lines (2189 loc) Β· 67.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package slagent
import (
"context"
"strings"
"testing"
"time"
slackapi "github.com/slack-go/slack"
"github.com/sttts/slagent/access"
)
// isStop returns true if msg is a StopMessage.
func isStop(msg Message) bool {
_, ok := msg.(StopMessage)
return ok
}
// isQuit returns true if msg is a QuitMessage.
func isQuit(msg Message) bool {
_, ok := msg.(QuitMessage)
return ok
}
func TestClassifyBlock(t *testing.T) {
tests := []struct {
blockID string
wantKind BlockKind
wantID string
}{
{"slagent-abc123", BlockFinal, "abc123"},
{"slagent-abc123~", BlockStreaming, "abc123"},
{"slagent-abc123~act", BlockActivity, "abc123"},
{"other-block", BlockNone, ""},
{"", BlockNone, ""},
{"slagent-", BlockFinal, ""},
}
for _, tt := range tests {
kind, id := classifyBlock(tt.blockID)
if kind != tt.wantKind || id != tt.wantID {
t.Errorf("classifyBlock(%q) = (%d, %q), want (%d, %q)", tt.blockID, kind, id, tt.wantKind, tt.wantID)
}
}
}
func TestIsNativeToken(t *testing.T) {
tests := []struct {
token string
want bool
}{
{"xoxb-123-456", true},
{"xoxc-123-456", false},
{"xoxp-123-456", false},
{"", false},
}
for _, tt := range tests {
if got := isNativeToken(tt.token); got != tt.want {
t.Errorf("isNativeToken(%q) = %v, want %v", tt.token, got, tt.want)
}
}
}
func TestThreadPermissions(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// Owner is authorized
if !thread.IsAuthorized("U_OWNER") {
t.Error("owner should be authorized")
}
// Other user is not
if thread.IsAuthorized("U_OTHER") {
t.Error("other user should not be authorized")
}
// /open command from owner
if handled, _ := thread.handleCommand("U_OWNER", "/open"); !handled {
t.Error("/open from owner should be handled")
}
if !thread.IsAuthorized("U_OTHER") {
t.Error("other user should be authorized after /open")
}
// /close from owner
if handled, _ := thread.handleCommand("U_OWNER", "/close"); !handled {
t.Error("/close from owner should be handled")
}
if thread.IsAuthorized("U_OTHER") {
t.Error("other user should not be authorized after /close")
}
// /open from non-owner returns handled=true but with denial feedback
if handled, feedback := thread.handleCommand("U_OTHER", "/open"); !handled {
t.Error("/open from non-owner should be handled (with denial)")
} else if feedback == "" {
t.Error("/open from non-owner should return feedback")
}
}
func TestOpenForSpecificUsers(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// By default, only owner is authorized
if thread.IsAuthorized("U_ALICE") {
t.Error("alice should not be authorized initially")
}
// /open <@U_ALICE> β allow alice
thread.handleCommand("U_OWNER", "/open <@U_ALICE>")
if !thread.IsAuthorized("U_ALICE") {
t.Error("alice should be authorized after /open")
}
if thread.IsAuthorized("U_BOB") {
t.Error("bob should not be authorized")
}
// /open <@U_BOB> β also allow bob (additive)
thread.handleCommand("U_OWNER", "/open <@U_BOB>")
if !thread.IsAuthorized("U_ALICE") {
t.Error("alice should still be authorized")
}
if !thread.IsAuthorized("U_BOB") {
t.Error("bob should now be authorized")
}
// /lock β reset everything
thread.handleCommand("U_OWNER", "/lock")
if thread.IsAuthorized("U_ALICE") {
t.Error("alice should not be authorized after /lock")
}
if thread.IsAuthorized("U_BOB") {
t.Error("bob should not be authorized after /lock")
}
}
func TestOpenMultipleUsersAtOnce(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// /open <@U_ALICE> <@U_BOB> β allow both at once
thread.handleCommand("U_OWNER", "/open <@U_ALICE> <@U_BOB>")
if !thread.IsAuthorized("U_ALICE") {
t.Error("alice should be authorized")
}
if !thread.IsAuthorized("U_BOB") {
t.Error("bob should be authorized")
}
if thread.IsAuthorized("U_CAROL") {
t.Error("carol should not be authorized")
}
}
func TestLockSpecificUser(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// Open for everyone first
thread.handleCommand("U_OWNER", "/open")
if !thread.IsAuthorized("U_ALICE") {
t.Error("alice should be authorized when open")
}
// /lock <@U_ALICE> β ban alice specifically
thread.handleCommand("U_OWNER", "/lock <@U_ALICE>")
if thread.IsAuthorized("U_ALICE") {
t.Error("alice should be banned")
}
if !thread.IsAuthorized("U_BOB") {
t.Error("bob should still be authorized (thread is open)")
}
// Owner is never banned
thread.handleCommand("U_OWNER", "/lock <@U_OWNER>")
if !thread.IsAuthorized("U_OWNER") {
t.Error("owner should never be banned")
}
}
func TestLockMultipleUsers(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
thread.handleCommand("U_OWNER", "/open")
// /lock <@U_ALICE> <@U_BOB> β ban both
thread.handleCommand("U_OWNER", "/lock <@U_ALICE> <@U_BOB>")
if thread.IsAuthorized("U_ALICE") {
t.Error("alice should be banned")
}
if thread.IsAuthorized("U_BOB") {
t.Error("bob should be banned")
}
if !thread.IsAuthorized("U_CAROL") {
t.Error("carol should still be authorized")
}
}
func TestOpenUnbansBannedUser(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
thread.handleCommand("U_OWNER", "/open")
thread.handleCommand("U_OWNER", "/lock <@U_ALICE>")
if thread.IsAuthorized("U_ALICE") {
t.Error("alice should be banned")
}
// /open <@U_ALICE> β unban alice
thread.handleCommand("U_OWNER", "/open <@U_ALICE>")
if !thread.IsAuthorized("U_ALICE") {
t.Error("alice should be unbanned after /open")
}
}
func TestLockRemovesFromAllowed(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// Allow alice specifically
thread.handleCommand("U_OWNER", "/open <@U_ALICE>")
if !thread.IsAuthorized("U_ALICE") {
t.Error("alice should be allowed")
}
// Ban alice β should remove from allowed too
thread.handleCommand("U_OWNER", "/lock <@U_ALICE>")
if thread.IsAuthorized("U_ALICE") {
t.Error("alice should be banned and removed from allowed")
}
}
func TestFormatTitle(t *testing.T) {
mock := newMockSlack()
defer mock.close()
tests := []struct {
name string
setup func(*Thread)
want string
}{
{
name: "locked (default)",
setup: func(th *Thread) {},
want: ":fox_face:ππ§΅ Test Topic",
},
{
name: "open for all",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open")
},
want: ":fox_face:π§΅ Test Topic",
},
{
name: "open for specific user",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open <@U_ALICE>")
},
want: ":fox_face:π§΅ <@U_ALICE> Test Topic",
},
{
name: "open for multiple users",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open <@U_ALICE> <@U_BOB>")
},
want: ":fox_face:π§΅ <@U_ALICE> <@U_BOB> Test Topic",
},
{
name: "banned user",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open")
th.handleCommand("U_OWNER", "/lock <@U_EVIL>")
},
want: ":fox_face:π§΅ Test Topic (π <@U_EVIL>)",
},
{
name: "allowed and banned",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open <@U_ALICE>")
th.handleCommand("U_OWNER", "/lock <@U_EVIL>")
},
want: ":fox_face:π§΅ <@U_ALICE> Test Topic (π <@U_EVIL>)",
},
{
name: "with mode suffix",
setup: func(th *Thread) {
th.modeSuffix = " β π planning"
},
want: ":fox_face:ππ§΅ Test Topic β π planning",
},
{
name: "mode suffix with bans",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open")
th.handleCommand("U_OWNER", "/lock <@U_EVIL>")
th.modeSuffix = " β π planning"
},
want: ":fox_face:π§΅ Test Topic β π planning (π <@U_EVIL>)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
th.topic = "Test Topic"
tt.setup(th)
got := th.formatTitle()
if got != tt.want {
t.Errorf("formatTitle() = %q, want %q", got, tt.want)
}
})
}
}
func TestParseTitle(t *testing.T) {
mock := newMockSlack()
defer mock.close()
tests := []struct {
name string
text string
wantTitle string
wantOpen bool
wantAllowed []string
wantBanned []string
}{
{
name: "locked",
text: ":fox_face:ππ§΅ My Topic",
wantTitle: "My Topic",
wantOpen: false,
},
{
name: "open",
text: ":fox_face:π§΅ My Topic",
wantTitle: "My Topic",
wantOpen: true,
},
{
name: "selective access",
text: ":fox_face:π§΅ <@U_ALICE> <@U_BOB> My Topic",
wantTitle: "My Topic",
wantOpen: false,
wantAllowed: []string{"U_ALICE", "U_BOB"},
},
{
name: "banned users",
text: ":fox_face:π§΅ My Topic (π <@U_EVIL>)",
wantTitle: "My Topic",
wantOpen: true,
wantBanned: []string{"U_EVIL"},
},
{
name: "allowed and banned",
text: ":fox_face:π§΅ <@U_ALICE> My Topic (π <@U_EVIL>)",
wantTitle: "My Topic",
wantOpen: false,
wantAllowed: []string{"U_ALICE"},
wantBanned: []string{"U_EVIL"},
},
{
name: "locked shortcodes",
text: ":fox_face::lock::thread: My Topic",
wantTitle: "My Topic",
wantOpen: false,
},
{
name: "open shortcodes",
text: ":fox_face::thread: My Topic",
wantTitle: "My Topic",
wantOpen: true,
},
{
name: "banned shortcodes",
text: ":fox_face::thread: My Topic (:lock: <@U_EVIL>)",
wantTitle: "My Topic",
wantOpen: true,
wantBanned: []string{"U_EVIL"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := NewThread(mock.client(), "C_TEST",
WithInstanceID("fox_face"),
)
th.parseTitle(tt.text)
if th.topic != tt.wantTitle {
t.Errorf("title = %q, want %q", th.topic, tt.wantTitle)
}
if th.OpenAccess() != tt.wantOpen {
t.Errorf("openAccess = %v, want %v", th.OpenAccess(), tt.wantOpen)
}
st := th.Controller.State()
allowedSet := make(map[string]bool)
for _, u := range st.AllowedUsers {
allowedSet[u] = true
}
bannedSet := make(map[string]bool)
for _, u := range st.BannedUsers {
bannedSet[u] = true
}
for _, uid := range tt.wantAllowed {
if !allowedSet[uid] {
t.Errorf("allowedUsers missing %s", uid)
}
}
for _, uid := range tt.wantBanned {
if !bannedSet[uid] {
t.Errorf("bannedUsers missing %s", uid)
}
}
})
}
}
func TestParseTitleRoundtrip(t *testing.T) {
mock := newMockSlack()
defer mock.close()
// Build a thread with allowed + banned users, format title, parse it back
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
th.topic = "Design API"
th.handleCommand("U_OWNER", "/open <@U_ALICE> <@U_BOB>")
th.handleCommand("U_OWNER", "/lock <@U_EVIL>")
label := th.formatTitle()
// Parse into a fresh thread
th2 := NewThread(mock.client(), "C_TEST",
WithInstanceID("fox_face"),
)
th2.parseTitle(label)
if th2.topic != "Design API" {
t.Errorf("roundtrip title = %q, want %q", th2.topic, "Design API")
}
if th2.OpenAccess() != false {
t.Error("roundtrip openAccess should be false")
}
st2 := th2.Controller.State()
allowedSet := make(map[string]bool)
for _, u := range st2.AllowedUsers {
allowedSet[u] = true
}
bannedSet := make(map[string]bool)
for _, u := range st2.BannedUsers {
bannedSet[u] = true
}
if !allowedSet["U_ALICE"] || !allowedSet["U_BOB"] {
t.Errorf("roundtrip allowedUsers = %v, want alice+bob", st2.AllowedUsers)
}
if !bannedSet["U_EVIL"] {
t.Errorf("roundtrip bannedUsers = %v, want evil", st2.BannedUsers)
}
}
func TestParseTitleStripsModeSuffix(t *testing.T) {
mock := newMockSlack()
defer mock.close()
tests := []struct {
name string
title string
wantTopic string
}{
{
name: "no mode suffix",
title: ":fox_face:ππ§΅ Build CLI",
wantTopic: "Build CLI",
},
{
name: "with planning suffix",
title: ":fox_face:ππ§΅ Build CLI β π planning",
wantTopic: "Build CLI",
},
{
name: "open with planning suffix",
title: ":fox_face:π§΅ Build CLI β π planning",
wantTopic: "Build CLI",
},
{
name: "with bans and planning suffix",
title: ":fox_face:π§΅ Build CLI β π planning (π <@U_EVIL>)",
wantTopic: "Build CLI",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := NewThread(mock.client(), "C_TEST", WithInstanceID("fox_face"))
th.parseTitle(tt.title)
if th.topic != tt.wantTopic {
t.Errorf("topic = %q, want %q", th.topic, tt.wantTopic)
}
})
}
}
func TestModeSuffixRoundtrip(t *testing.T) {
mock := newMockSlack()
defer mock.close()
// Format with mode suffix, parse back β topic should be clean
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
th.topic = "Refactor session"
th.modeSuffix = " β π planning"
label := th.formatTitle()
if !strings.Contains(label, " β π planning") {
t.Errorf("formatted title should contain mode suffix, got %q", label)
}
// Parse into fresh thread
th2 := NewThread(mock.client(), "C_TEST", WithInstanceID("fox_face"))
th2.parseTitle(label)
if th2.topic != "Refactor session" {
t.Errorf("roundtrip topic = %q, want %q", th2.topic, "Refactor session")
}
}
func TestModeSuffixWithBansRoundtrip(t *testing.T) {
mock := newMockSlack()
defer mock.close()
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
th.topic = "Design API"
th.modeSuffix = " β π planning"
th.handleCommand("U_OWNER", "/open")
th.handleCommand("U_OWNER", "/lock <@U_EVIL>")
label := th.formatTitle()
th2 := NewThread(mock.client(), "C_TEST", WithInstanceID("fox_face"))
th2.parseTitle(label)
if th2.topic != "Design API" {
t.Errorf("topic = %q, want %q", th2.topic, "Design API")
}
st2 := th2.Controller.State()
bannedSet := make(map[string]bool)
for _, u := range st2.BannedUsers {
bannedSet[u] = true
}
if !bannedSet["U_EVIL"] {
t.Errorf("bannedUsers = %v, want U_EVIL", st2.BannedUsers)
}
}
func TestObserveFormatTitle(t *testing.T) {
mock := newMockSlack()
defer mock.close()
tests := []struct {
name string
setup func(*Thread)
want string
}{
{
name: "observe closed (no allowed users)",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/observe")
},
want: ":fox_face:ππ§΅ Test Topic",
},
{
name: "observe with allowed users",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open <@U_ALICE>")
th.SetObserve(true)
},
want: ":fox_face:ππ§΅ <@U_ALICE> Test Topic",
},
{
name: "observe off after toggle",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/observe") // on
th.handleCommand("U_OWNER", "/observe") // off
},
want: ":fox_face:ππ§΅ Test Topic",
},
{
name: "open mode ignores observe",
setup: func(th *Thread) {
th.handleCommand("U_OWNER", "/open")
},
want: ":fox_face:π§΅ Test Topic",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
th.topic = "Test Topic"
tt.setup(th)
got := th.formatTitle()
if got != tt.want {
t.Errorf("formatTitle() = %q, want %q", got, tt.want)
}
})
}
}
func TestObserveParseTitle(t *testing.T) {
mock := newMockSlack()
defer mock.close()
tests := []struct {
name string
text string
wantObserve bool
wantOpen bool
wantTopic string
wantAllowed []string
}{
{
name: "observe closed",
text: ":fox_face:ππ§΅ My Topic",
wantObserve: true,
wantOpen: false,
wantTopic: "My Topic",
},
{
name: "observe with allowed users",
text: ":fox_face:ππ§΅ <@U_ALICE> My Topic",
wantObserve: true,
wantOpen: false,
wantTopic: "My Topic",
wantAllowed: []string{"U_ALICE"},
},
{
name: "observe shortcodes",
text: ":fox_face::eyes::thread: My Topic",
wantObserve: true,
wantOpen: false,
wantTopic: "My Topic",
},
{
name: "locked (no observe)",
text: ":fox_face:ππ§΅ My Topic",
wantObserve: false,
wantOpen: false,
wantTopic: "My Topic",
},
{
name: "open (no observe)",
text: ":fox_face:π§΅ My Topic",
wantObserve: false,
wantOpen: true,
wantTopic: "My Topic",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
th := NewThread(mock.client(), "C_TEST", WithInstanceID("fox_face"))
th.parseTitle(tt.text)
if th.Observe() != tt.wantObserve {
t.Errorf("observe = %v, want %v", th.Observe(), tt.wantObserve)
}
if th.OpenAccess() != tt.wantOpen {
t.Errorf("openAccess = %v, want %v", th.OpenAccess(), tt.wantOpen)
}
if th.topic != tt.wantTopic {
t.Errorf("topic = %q, want %q", th.topic, tt.wantTopic)
}
st := th.Controller.State()
allowedSet := make(map[string]bool)
for _, u := range st.AllowedUsers {
allowedSet[u] = true
}
for _, uid := range tt.wantAllowed {
if !allowedSet[uid] {
t.Errorf("allowedUsers missing %s", uid)
}
}
})
}
}
func TestObserveRoundtrip(t *testing.T) {
mock := newMockSlack()
defer mock.close()
// Observe + closed
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
th.topic = "Watch this"
th.handleCommand("U_OWNER", "/observe")
label := th.formatTitle()
th2 := NewThread(mock.client(), "C_TEST", WithInstanceID("fox_face"))
th2.parseTitle(label)
if th2.topic != "Watch this" {
t.Errorf("roundtrip topic = %q, want %q", th2.topic, "Watch this")
}
if !th2.Observe() {
t.Error("roundtrip observe should be true")
}
if th2.OpenAccess() {
t.Error("roundtrip openAccess should be false")
}
}
func TestObserveIsVisible(t *testing.T) {
mock := newMockSlack()
defer mock.close()
th := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithObserve(),
)
// Owner is both authorized and visible
if !th.IsAuthorized("U_OWNER") {
t.Error("owner should be authorized")
}
if !th.IsVisible("U_OWNER") {
t.Error("owner should be visible")
}
// Other user is not authorized but IS visible in observe mode
if th.IsAuthorized("U_OTHER") {
t.Error("other user should not be authorized in observe mode")
}
if !th.IsVisible("U_OTHER") {
t.Error("other user should be visible in observe mode")
}
// Turn off observe
th.SetObserve(false)
if th.IsVisible("U_OTHER") {
t.Error("other user should not be visible when observe is off")
}
}
func TestObserveAccessMode(t *testing.T) {
mock := newMockSlack()
defer mock.close()
th := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
if th.AccessMode() != "locked" {
t.Errorf("default should be locked, got %q", th.AccessMode())
}
th.SetObserve(true)
if th.AccessMode() != "observe+locked" {
t.Errorf("observe+locked should be observe+locked, got %q", th.AccessMode())
}
th.SetOpen()
if th.AccessMode() != "open" {
t.Errorf("open should be open, got %q", th.AccessMode())
}
}
func TestObserveCommand(t *testing.T) {
mock := newMockSlack()
defer mock.close()
th := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// /observe toggles on
handled, feedback := th.handleCommand("U_OWNER", "/observe")
if !handled {
t.Error("/observe should be handled")
}
if !strings.Contains(feedback, "on") {
t.Errorf("expected 'on' in feedback, got %q", feedback)
}
if !th.Observe() {
t.Error("observe should be true")
}
if th.OpenAccess() {
t.Error("openAccess should be false after /observe")
}
// /observe toggles off
handled, feedback = th.handleCommand("U_OWNER", "/observe")
if !handled {
t.Error("/observe should be handled")
}
if !strings.Contains(feedback, "off") {
t.Errorf("expected 'off' in feedback, got %q", feedback)
}
if th.Observe() {
t.Error("observe should be false after second /observe")
}
// Non-owner can't use /observe
th.handleCommand("U_OWNER", "/observe") // turn on
_, feedback = th.handleCommand("U_OTHER", "/observe")
if !strings.Contains(feedback, "π«") {
t.Errorf("non-owner should get denied, got %q", feedback)
}
}
func TestLockDisablesObserve(t *testing.T) {
mock := newMockSlack()
defer mock.close()
th := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
th.handleCommand("U_OWNER", "/observe")
if !th.Observe() {
t.Error("observe should be on")
}
// /lock disables observe
th.handleCommand("U_OWNER", "/lock")
if th.Observe() {
t.Error("observe should be off after /lock")
}
}
func TestOpenDisablesObserve(t *testing.T) {
mock := newMockSlack()
defer mock.close()
th := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
th.handleCommand("U_OWNER", "/observe")
if !th.Observe() {
t.Error("observe should be on")
}
// /open disables observe
th.handleCommand("U_OWNER", "/open")
if th.Observe() {
t.Error("observe should be off after /open")
}
}
func TestThreadTitleUpdatedOnAccessChange(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
thread.Start("My Task")
// Default is locked β title should contain π
parentMsg := mock.postedMessages()[0]
if !strings.Contains(parentMsg.Text, "π") {
t.Errorf("initial title should contain π, got %q", parentMsg.Text)
}
// /open β title should no longer contain π
thread.handleCommand("U_OWNER", "/open")
msgs := mock.postedMessages()
var updated string
for _, m := range msgs {
if m.TS == parentMsg.TS && m.IsUpdate {
updated = m.Text
}
}
if strings.Contains(updated, "π") {
t.Errorf("title after /open should not contain π, got %q", updated)
}
}
func TestHandleCommandUnknownCommand(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
if handled, _ := thread.handleCommand("U_OWNER", "/unknown"); handled {
t.Error("/unknown should not be handled")
}
if handled, _ := thread.handleCommand("U_OWNER", "/status"); handled {
t.Error("/status should not be handled")
}
}
func TestHandleCommandNonOwnerBlocked(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST", WithOwner("U_OWNER"))
// Non-owner commands are handled (recognized) but denied with feedback
for _, cmd := range []string{"/open", "/lock", "/open <@U_ALICE>", "/lock <@U_ALICE>"} {
handled, feedback := thread.handleCommand("U_OTHER", cmd)
if !handled {
t.Errorf("non-owner %q should be handled (recognized)", cmd)
}
if !strings.Contains(feedback, "owner") {
t.Errorf("non-owner %q feedback should mention owner, got %q", cmd, feedback)
}
}
}
func TestCommandFeedback(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
thread.Start("Test")
tests := []struct {
cmd string
wantContains string
}{
{"/open", "π"},
{"/lock", "π"},
{"/open <@U_ALICE>", "π"},
{"/lock <@U_ALICE>", "π"},
{"/close", "π"},
}
for _, tt := range tests {
_, feedback := thread.handleCommand("U_OWNER", tt.cmd)
if !strings.Contains(feedback, tt.wantContains) {
t.Errorf("handleCommand(%q) feedback = %q, want containing %q", tt.cmd, feedback, tt.wantContains)
}
}
}
func TestCommandFeedbackPostedToSlack(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
thread.Start("Test")
// Inject a /open command from the owner
mock.injectReply("C_TEST", thread.ThreadTS(), "U_OWNER", ":fox_face:: /open")
replies, err := thread.PollReplies()
if err != nil {
t.Fatal(err)
}
if len(replies) != 0 {
t.Errorf("handled command should not produce replies, got %d", len(replies))
}
// Check that feedback was posted
msgs := mock.activeMessages()
var found bool
for _, m := range msgs {
if strings.Contains(m.Text, "π") && m.ThreadTS == thread.ThreadTS() {
found = true
break
}
}
if !found {
t.Error("feedback message should be posted to thread")
}
}
func TestUnauthorizedBroadcastSilentlySkipped(t *testing.T) {
mock := newMockSlack()
defer mock.close()
thread := NewThread(mock.client(), "C_TEST",
WithOwner("U_OWNER"),
WithInstanceID("fox_face"),
)
thread.Start("Test")
// Broadcast from unauthorized user β silently skipped
mock.injectReply("C_TEST", thread.ThreadTS(), "U_OTHER", "hello everyone")
replies, _ := thread.PollReplies()
if len(replies) != 0 {
t.Error("unauthorized broadcast should not produce replies")