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
|
use anyhow::Result;
use futures::join;
use serial_test::serial;
use test_utils::{git::GitTestRepo, relay::Relay, *};
static FEATURE_BRANCH_NAME_1: &str = "feature-example-t";
static FEATURE_BRANCH_NAME_2: &str = "feature-example-f";
static FEATURE_BRANCH_NAME_3: &str = "feature-example-c";
static PROPOSAL_TITLE_1: &str = "proposal a";
static PROPOSAL_TITLE_2: &str = "proposal b";
static PROPOSAL_TITLE_3: &str = "proposal c";
fn cli_tester_create_proposals() -> Result<GitTestRepo> {
let git_repo = GitTestRepo::default();
git_repo.populate()?;
cli_tester_create_proposal(
&git_repo,
FEATURE_BRANCH_NAME_1,
"a",
Some((PROPOSAL_TITLE_1, "proposal a description")),
None,
)?;
cli_tester_create_proposal(
&git_repo,
FEATURE_BRANCH_NAME_2,
"b",
Some((PROPOSAL_TITLE_2, "proposal b description")),
None,
)?;
cli_tester_create_proposal(
&git_repo,
FEATURE_BRANCH_NAME_3,
"c",
Some((PROPOSAL_TITLE_3, "proposal c description")),
None,
)?;
Ok(git_repo)
}
fn create_and_populate_branch(
test_repo: &GitTestRepo,
branch_name: &str,
prefix: &str,
only_one_commit: bool,
) -> Result<()> {
test_repo.checkout("main")?;
test_repo.create_branch(branch_name)?;
test_repo.checkout(branch_name)?;
std::fs::write(
test_repo.dir.join(format!("{}3.md", prefix)),
"some content",
)?;
test_repo.stage_and_commit(format!("add {}3.md", prefix).as_str())?;
if !only_one_commit {
std::fs::write(
test_repo.dir.join(format!("{}4.md", prefix)),
"some content",
)?;
test_repo.stage_and_commit(format!("add {}4.md", prefix).as_str())?;
}
Ok(())
}
fn cli_tester_create_proposal(
test_repo: &GitTestRepo,
branch_name: &str,
prefix: &str,
cover_letter_title_and_description: Option<(&str, &str)>,
in_reply_to: Option<String>,
) -> Result<()> {
create_and_populate_branch(test_repo, branch_name, prefix, false)?;
if let Some(in_reply_to) = in_reply_to {
let mut p = CliTester::new_from_dir(
&test_repo.dir,
[
"--nsec",
TEST_KEY_1_NSEC,
"--password",
TEST_PASSWORD,
"--disable-cli-spinners",
"send",
"--no-cover-letter",
"--in-reply-to",
in_reply_to.as_str(),
],
);
p.expect_end_eventually()?;
} else if let Some((title, description)) = cover_letter_title_and_description {
let mut p = CliTester::new_from_dir(
&test_repo.dir,
[
"--nsec",
TEST_KEY_1_NSEC,
"--password",
TEST_PASSWORD,
"--disable-cli-spinners",
"send",
"--title",
format!("\"{title}\"").as_str(),
"--description",
format!("\"{description}\"").as_str(),
],
);
p.expect_end_eventually()?;
} else {
let mut p = CliTester::new_from_dir(
&test_repo.dir,
[
"--nsec",
TEST_KEY_1_NSEC,
"--password",
TEST_PASSWORD,
"--disable-cli-spinners",
"send",
"--no-cover-letter",
],
);
p.expect_end_eventually()?;
}
Ok(())
}
mod when_main_is_checked_out {
use super::*;
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn cli_show_error() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?;
test_repo.checkout("main")?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("Error: checkout a branch associated with a proposal first\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
Ok(())
}
}
}
mod when_branch_doesnt_exist {
use super::*;
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn cli_show_error() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
test_repo.create_branch("random-name")?;
test_repo.checkout("random-name")?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect(
"Error: cannot find a proposal root event associated with the checked out branch name\r\n",
)?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
Ok(())
}
}
}
mod when_branch_is_checked_out {
use super::*;
mod when_branch_is_up_to_date {
use super::*;
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn cli_show_up_to_date() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect("branch already up-to-date\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
Ok(())
}
}
}
mod when_branch_is_behind {
use super::*;
async fn prep_and_run() -> Result<(GitTestRepo, GitTestRepo)> {
// fallback (51,52) user write (53, 55) repo (55, 56)
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle =
std::thread::spawn(move || -> Result<(GitTestRepo, GitTestRepo)> {
let originating_repo = cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", true)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect_end_eventually()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok((originating_repo, test_repo))
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
let res = cli_tester_handle.join().unwrap()?;
Ok(res)
}
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn cli_applied_1_commit() -> Result<()> {
// fallback (51,52) user write (53, 55) repo (55, 56)
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle =
std::thread::spawn(move || -> Result<(GitTestRepo, GitTestRepo)> {
let originating_repo = cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", true)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect("applied 1 new commits\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok((originating_repo, test_repo))
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
Ok(())
}
}
#[tokio::test]
#[serial]
async fn proposal_branch_tip_is_most_recent_patch() -> Result<()> {
let (originating_repo, test_repo) = prep_and_run().await?;
assert_eq!(
originating_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?,
test_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?,
);
Ok(())
}
}
mod when_old_proposal_revision_amended_locally {
use super::*;
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn cli_output_correct() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
// simulating amending an older version of the proposal commits on the current
// branch
create_and_populate_branch(
&test_repo,
FEATURE_BRANCH_NAME_1,
"a-changed",
false,
)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect(
"you have an amended/rebase version the proposal that is unpublished\r\n",
)?;
p.expect("your local proposal branch (2 ahead 0 behind 'main') has conflicting changes with the latest published proposal (2 ahead 0 behind 'main')\r\n")?;
p.expect("its likely that you have rebased / amended an old proposal version because git has no record of the latest proposal commit.\r\n")?;
p.expect("it is possible that you have been working off the latest version and git has delete this commit as part of a clean up\r\n")?;
p.expect("to view the latest proposal but retain your changes:\r\n")?;
p.expect(" 1) create a new branch off the tip commit of this one to store your changes\r\n")?;
p.expect(" 2) run `ngit list` and checkout the latest published version of this proposal\r\n")?;
p.expect("if you are confident in your changes consider running `ngit push --force`\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
println!("{:?}", r55.events);
Ok(())
}
}
}
mod when_latest_proposal_amended_locally {
use super::*;
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn cli_output_correct() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
// simulating checking out the proposal (the commits_ids will match)
create_and_populate_branch(&test_repo, "different-branch-name", "a", false)?;
test_repo.checkout("main")?;
// simulating amending the proposal
create_and_populate_branch(
&test_repo,
FEATURE_BRANCH_NAME_1,
"a-changed",
false,
)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect(
"you have an amended/rebase version the proposal that is unpublished\r\n",
)?;
p.expect("you have previously applied the latest version of the proposal (2 ahead 0 behind 'main') but your local proposal branch has amended or rebased it (2 ahead 0 behind 'main')\r\n")?;
p.expect("to view the latest proposal but retain your changes:\r\n")?;
p.expect(" 1) create a new branch off the tip commit of this one to store your changes\r\n")?;
p.expect(" 2) run `ngit list` and checkout the latest published version of this proposal\r\n")?;
p.expect("if you are confident in your changes consider running `ngit push --force`\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
println!("{:?}", r55.events);
Ok(())
}
}
}
mod when_local_commits_on_uptodate_proposal {
use super::*;
async fn prep_and_run() -> Result<(GitTestRepo, GitTestRepo)> {
// fallback (51,52) user write (53, 55) repo (55, 56)
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(
move || -> Result<(GitTestRepo, GitTestRepo)> {
let originating_repo = cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?;
// add appended commit to local branch
std::fs::write(test_repo.dir.join("appended.md"), "some content")?;
test_repo.stage_and_commit("appended commit")?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect("local proposal branch exists with 1 unpublished commits on top of the most up-to-date version of the proposal\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok((originating_repo, test_repo))
},
);
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
let res = cli_tester_handle.join().unwrap()?;
Ok(res)
}
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn prompts_to_choose_from_proposal_titles() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
cli_tester_create_proposals()?;
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?;
// add appended commit to local branch
std::fs::write(test_repo.dir.join("appended.md"), "some content")?;
test_repo.stage_and_commit("appended commit")?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect("local proposal branch exists with 1 unpublished commits on top of the most up-to-date version of the proposal\r\n")?;
p.expect_end()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
println!("{:?}", r55.events);
Ok(())
}
}
#[tokio::test]
#[serial]
async fn didnt_overwrite_local_appendments() -> Result<()> {
let (originating_repo, test_repo) = prep_and_run().await?;
assert_ne!(
test_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?,
originating_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?,
);
Ok(())
}
}
mod when_latest_event_rebases_branch {
use std::time::Duration;
use nostr_sdk::blocking::Client;
use super::*;
async fn prep_and_run() -> Result<(GitTestRepo, GitTestRepo)> {
// fallback (51,52) user write (53, 55) repo (55, 56)
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle =
std::thread::spawn(move || -> Result<(GitTestRepo, GitTestRepo)> {
// create 3 proposals
let _ = cli_tester_create_proposals()?;
// get proposal id of first
let client = Client::new(&nostr::Keys::generate());
client.add_relay("ws://localhost:8055")?;
client.connect_relay("ws://localhost:8055")?;
let proposals = client.get_events_of(
vec![
nostr::Filter::default()
.kind(nostr::Kind::Custom(PATCH_KIND))
.custom_tag(nostr::Alphabet::T, vec!["root"]),
],
Some(Duration::from_millis(500)),
)?;
client.disconnect()?;
let proposal_1_id = proposals
.iter()
.find(|e| {
e.tags
.iter()
.any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1))
})
.unwrap()
.id;
// recreate proposal 1 on top of a another commit (like a rebase on top
// of one extra commit)
let second_originating_repo = GitTestRepo::default();
second_originating_repo.populate()?;
std::fs::write(
second_originating_repo.dir.join("amazing.md"),
"some content",
)?;
second_originating_repo.stage_and_commit("commit for rebasing on top of")?;
cli_tester_create_proposal(
&second_originating_repo,
FEATURE_BRANCH_NAME_1,
"a",
Some((PROPOSAL_TITLE_1, "proposal a description")),
Some(proposal_1_id.to_string()),
)?;
// pretend we have downloaded the origianl version of the first proposal
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?;
// pretend we have pulled the updated main branch
test_repo.checkout("main")?;
std::fs::write(test_repo.dir.join("amazing.md"), "some content")?;
test_repo.stage_and_commit("commit for rebasing on top of")?;
test_repo.checkout(FEATURE_BRANCH_NAME_1)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect_end_eventually_and_print()?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok((second_originating_repo, test_repo))
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
let res = cli_tester_handle.join().unwrap()?;
Ok(res)
}
mod cli_prompts {
use super::*;
#[tokio::test]
#[serial]
async fn prompts_to_choose_from_proposal_titles() -> Result<()> {
let (mut r51, mut r52, mut r53, mut r55, mut r56) = (
Relay::new(8051, None, None),
Relay::new(8052, None, None),
Relay::new(8053, None, None),
Relay::new(8055, None, None),
Relay::new(8056, None, None),
);
r51.events.push(generate_test_key_1_relay_list_event());
r51.events.push(generate_test_key_1_metadata_event("fred"));
r51.events.push(generate_repo_ref_event());
r55.events.push(generate_repo_ref_event());
r55.events.push(generate_test_key_1_metadata_event("fred"));
r55.events.push(generate_test_key_1_relay_list_event());
let cli_tester_handle = std::thread::spawn(move || -> Result<()> {
// create 3 proposals
let _ = cli_tester_create_proposals()?;
// get proposal id of first
let client = Client::new(&nostr::Keys::generate());
client.add_relay("ws://localhost:8055")?;
client.connect_relay("ws://localhost:8055")?;
let proposals = client.get_events_of(
vec![
nostr::Filter::default()
.kind(nostr::Kind::Custom(PATCH_KIND))
.custom_tag(nostr::Alphabet::T, vec!["root"]),
],
Some(Duration::from_millis(500)),
)?;
client.disconnect()?;
let proposal_1_id = proposals
.iter()
.find(|e| {
e.tags
.iter()
.any(|t| t.as_vec()[1].eq(&FEATURE_BRANCH_NAME_1))
})
.unwrap()
.id;
// recreate proposal 1 on top of a another commit (like a rebase on top
// of one extra commit)
let second_originating_repo = GitTestRepo::default();
second_originating_repo.populate()?;
std::fs::write(
second_originating_repo.dir.join("amazing.md"),
"some content",
)?;
second_originating_repo.stage_and_commit("commit for rebasing on top of")?;
cli_tester_create_proposal(
&second_originating_repo,
FEATURE_BRANCH_NAME_1,
"a",
Some((PROPOSAL_TITLE_1, "proposal a description")),
Some(proposal_1_id.to_string()),
)?;
// pretend we have downloaded the origianl version of the first proposal
let test_repo = GitTestRepo::default();
test_repo.populate()?;
create_and_populate_branch(&test_repo, FEATURE_BRANCH_NAME_1, "a", false)?;
// pretend we have pulled the updated main branch
test_repo.checkout("main")?;
std::fs::write(test_repo.dir.join("amazing.md"), "some content")?;
test_repo.stage_and_commit("commit for rebasing on top of")?;
test_repo.checkout(FEATURE_BRANCH_NAME_1)?;
let mut p = CliTester::new_from_dir(&test_repo.dir, ["pull"]);
p.expect("finding proposal root event...\r\n")?;
p.expect("found proposal root event. finding commits...\r\n")?;
p.expect_end_with("pulled new version of proposal (2 ahead 0 behind 'main'), replacing old version (2 ahead 1 behind 'main')\r\n")?;
for p in [51, 52, 53, 55, 56] {
relay::shutdown_relay(8000 + p)?;
}
Ok(())
});
// launch relay
let _ = join!(
r51.listen_until_close(),
r52.listen_until_close(),
r53.listen_until_close(),
r55.listen_until_close(),
r56.listen_until_close(),
);
cli_tester_handle.join().unwrap()?;
println!("{:?}", r55.events);
Ok(())
}
}
#[tokio::test]
#[serial]
async fn proposal_branch_tip_is_most_recent_proposal_revision_tip() -> Result<()> {
let (originating_repo, test_repo) = prep_and_run().await?;
assert_eq!(
originating_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?,
test_repo.get_tip_of_local_branch(FEATURE_BRANCH_NAME_1)?,
);
Ok(())
}
}
}
|