|
6 | 6 | "path/filepath" |
7 | 7 | "strings" |
8 | 8 | "testing" |
| 9 | + "time" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | func TestListCommand(t *testing.T) { |
@@ -645,3 +646,166 @@ func TestListCommandNewlines(t *testing.T) { |
645 | 646 | t.Errorf("expected output to end with double newline (content newline + final blank), got: %q", output[len(output)-10:]) |
646 | 647 | } |
647 | 648 | } |
| 649 | + |
| 650 | +func TestListSortsReadyByCreatedAt(t *testing.T) { |
| 651 | + tmpDir := t.TempDir() |
| 652 | + filePath := filepath.Join(tmpDir, "mint-issues.yaml") |
| 653 | + t.Setenv("MINT_STORE_FILE", filePath) |
| 654 | + |
| 655 | + store := NewStore() |
| 656 | + now := time.Now() |
| 657 | + |
| 658 | + // Create 5 ready issues with different creation times, added in shuffled order |
| 659 | + store.Issues["issue3"] = &Issue{ID: "issue3", Title: "Third oldest", Status: "open", CreatedAt: now.Add(-3 * time.Hour)} |
| 660 | + store.Issues["issue1"] = &Issue{ID: "issue1", Title: "Oldest", Status: "open", CreatedAt: now.Add(-5 * time.Hour)} |
| 661 | + store.Issues["issue5"] = &Issue{ID: "issue5", Title: "Newest", Status: "open", CreatedAt: now} |
| 662 | + store.Issues["issue2"] = &Issue{ID: "issue2", Title: "Second oldest", Status: "open", CreatedAt: now.Add(-4 * time.Hour)} |
| 663 | + store.Issues["issue4"] = &Issue{ID: "issue4", Title: "Second newest", Status: "open", CreatedAt: now.Add(-1 * time.Hour)} |
| 664 | + |
| 665 | + _ = store.Save(filePath) |
| 666 | + |
| 667 | + cmd := newCommand() |
| 668 | + var buf bytes.Buffer |
| 669 | + cmd.Writer = &buf |
| 670 | + |
| 671 | + err := cmd.Run(context.Background(), []string{"mint", "list"}) |
| 672 | + if err != nil { |
| 673 | + t.Fatalf("list command failed: %v", err) |
| 674 | + } |
| 675 | + |
| 676 | + output := stripANSI(buf.String()) |
| 677 | + |
| 678 | + // Find positions of each issue ID in output |
| 679 | + idx1 := strings.Index(output, "issue1") |
| 680 | + idx2 := strings.Index(output, "issue2") |
| 681 | + idx3 := strings.Index(output, "issue3") |
| 682 | + idx4 := strings.Index(output, "issue4") |
| 683 | + idx5 := strings.Index(output, "issue5") |
| 684 | + |
| 685 | + // Verify all issues are present |
| 686 | + if idx1 == -1 || idx2 == -1 || idx3 == -1 || idx4 == -1 || idx5 == -1 { |
| 687 | + t.Fatalf("expected all issues in output, got: %s", output) |
| 688 | + } |
| 689 | + |
| 690 | + // Verify order: newest to oldest (issue5, issue4, issue3, issue2, issue1) |
| 691 | + if idx5 >= idx4 || idx4 >= idx3 || idx3 >= idx2 || idx2 >= idx1 { |
| 692 | + t.Errorf("expected ready issues sorted by CreatedAt (newest first): issue5(%d), issue4(%d), issue3(%d), issue2(%d), issue1(%d)\noutput:\n%s", |
| 693 | + idx5, idx4, idx3, idx2, idx1, output) |
| 694 | + } |
| 695 | +} |
| 696 | + |
| 697 | +func TestListSortsBlockedByCreatedAt(t *testing.T) { |
| 698 | + tmpDir := t.TempDir() |
| 699 | + filePath := filepath.Join(tmpDir, "mint-issues.yaml") |
| 700 | + t.Setenv("MINT_STORE_FILE", filePath) |
| 701 | + |
| 702 | + store := NewStore() |
| 703 | + now := time.Now() |
| 704 | + |
| 705 | + // Create a blocker issue |
| 706 | + store.Issues["blocker"] = &Issue{ID: "blocker", Title: "Blocker", Status: "open", CreatedAt: now} |
| 707 | + |
| 708 | + // Create 5 blocked issues with different creation times, added in shuffled order |
| 709 | + store.Issues["blocked3"] = &Issue{ID: "blocked3", Title: "Third oldest blocked", Status: "open", CreatedAt: now.Add(-3 * time.Hour), DependsOn: []string{"blocker"}} |
| 710 | + store.Issues["blocked1"] = &Issue{ID: "blocked1", Title: "Oldest blocked", Status: "open", CreatedAt: now.Add(-5 * time.Hour), DependsOn: []string{"blocker"}} |
| 711 | + store.Issues["blocked5"] = &Issue{ID: "blocked5", Title: "Newest blocked", Status: "open", CreatedAt: now.Add(-30 * time.Minute), DependsOn: []string{"blocker"}} |
| 712 | + store.Issues["blocked2"] = &Issue{ID: "blocked2", Title: "Second oldest blocked", Status: "open", CreatedAt: now.Add(-4 * time.Hour), DependsOn: []string{"blocker"}} |
| 713 | + store.Issues["blocked4"] = &Issue{ID: "blocked4", Title: "Second newest blocked", Status: "open", CreatedAt: now.Add(-1 * time.Hour), DependsOn: []string{"blocker"}} |
| 714 | + |
| 715 | + _ = store.Save(filePath) |
| 716 | + |
| 717 | + cmd := newCommand() |
| 718 | + var buf bytes.Buffer |
| 719 | + cmd.Writer = &buf |
| 720 | + |
| 721 | + err := cmd.Run(context.Background(), []string{"mint", "list"}) |
| 722 | + if err != nil { |
| 723 | + t.Fatalf("list command failed: %v", err) |
| 724 | + } |
| 725 | + |
| 726 | + output := stripANSI(buf.String()) |
| 727 | + |
| 728 | + // Find BLOCKED section |
| 729 | + blockedIdx := strings.Index(output, "BLOCKED") |
| 730 | + closedIdx := strings.Index(output, "CLOSED") |
| 731 | + if blockedIdx == -1 || closedIdx == -1 { |
| 732 | + t.Fatalf("expected BLOCKED and CLOSED sections in output") |
| 733 | + } |
| 734 | + |
| 735 | + // Extract just the BLOCKED section |
| 736 | + blockedSection := output[blockedIdx:closedIdx] |
| 737 | + |
| 738 | + // Find positions of each blocked issue in the BLOCKED section |
| 739 | + idx1 := strings.Index(blockedSection, "blocked1") |
| 740 | + idx2 := strings.Index(blockedSection, "blocked2") |
| 741 | + idx3 := strings.Index(blockedSection, "blocked3") |
| 742 | + idx4 := strings.Index(blockedSection, "blocked4") |
| 743 | + idx5 := strings.Index(blockedSection, "blocked5") |
| 744 | + |
| 745 | + // Verify all blocked issues are present |
| 746 | + if idx1 == -1 || idx2 == -1 || idx3 == -1 || idx4 == -1 || idx5 == -1 { |
| 747 | + t.Fatalf("expected all blocked issues in BLOCKED section, got: %s", blockedSection) |
| 748 | + } |
| 749 | + |
| 750 | + // Verify order: newest to oldest (blocked5, blocked4, blocked3, blocked2, blocked1) |
| 751 | + if idx5 >= idx4 || idx4 >= idx3 || idx3 >= idx2 || idx2 >= idx1 { |
| 752 | + t.Errorf("expected blocked issues sorted by CreatedAt (newest first): blocked5(%d), blocked4(%d), blocked3(%d), blocked2(%d), blocked1(%d)\nblocked section:\n%s", |
| 753 | + idx5, idx4, idx3, idx2, idx1, blockedSection) |
| 754 | + } |
| 755 | +} |
| 756 | + |
| 757 | +func TestListSortsClosedByUpdatedAt(t *testing.T) { |
| 758 | + tmpDir := t.TempDir() |
| 759 | + filePath := filepath.Join(tmpDir, "mint-issues.yaml") |
| 760 | + t.Setenv("MINT_STORE_FILE", filePath) |
| 761 | + |
| 762 | + store := NewStore() |
| 763 | + now := time.Now() |
| 764 | + |
| 765 | + // Create 5 closed issues with different update times, added in shuffled order |
| 766 | + store.Issues["closed3"] = &Issue{ID: "closed3", Title: "Third oldest update", Status: "closed", CreatedAt: now.Add(-10 * time.Hour), UpdatedAt: now.Add(-3 * time.Hour)} |
| 767 | + store.Issues["closed1"] = &Issue{ID: "closed1", Title: "Oldest update", Status: "closed", CreatedAt: now.Add(-10 * time.Hour), UpdatedAt: now.Add(-5 * time.Hour)} |
| 768 | + store.Issues["closed5"] = &Issue{ID: "closed5", Title: "Newest update", Status: "closed", CreatedAt: now.Add(-10 * time.Hour), UpdatedAt: now} |
| 769 | + store.Issues["closed2"] = &Issue{ID: "closed2", Title: "Second oldest update", Status: "closed", CreatedAt: now.Add(-10 * time.Hour), UpdatedAt: now.Add(-4 * time.Hour)} |
| 770 | + store.Issues["closed4"] = &Issue{ID: "closed4", Title: "Second newest update", Status: "closed", CreatedAt: now.Add(-10 * time.Hour), UpdatedAt: now.Add(-1 * time.Hour)} |
| 771 | + |
| 772 | + _ = store.Save(filePath) |
| 773 | + |
| 774 | + cmd := newCommand() |
| 775 | + var buf bytes.Buffer |
| 776 | + cmd.Writer = &buf |
| 777 | + |
| 778 | + err := cmd.Run(context.Background(), []string{"mint", "list"}) |
| 779 | + if err != nil { |
| 780 | + t.Fatalf("list command failed: %v", err) |
| 781 | + } |
| 782 | + |
| 783 | + output := stripANSI(buf.String()) |
| 784 | + |
| 785 | + // Find CLOSED section |
| 786 | + closedIdx := strings.Index(output, "CLOSED") |
| 787 | + if closedIdx == -1 { |
| 788 | + t.Fatalf("expected CLOSED section in output") |
| 789 | + } |
| 790 | + |
| 791 | + // Extract just the CLOSED section (from CLOSED to end) |
| 792 | + closedSection := output[closedIdx:] |
| 793 | + |
| 794 | + // Find positions of each closed issue in the CLOSED section |
| 795 | + idx1 := strings.Index(closedSection, "closed1") |
| 796 | + idx2 := strings.Index(closedSection, "closed2") |
| 797 | + idx3 := strings.Index(closedSection, "closed3") |
| 798 | + idx4 := strings.Index(closedSection, "closed4") |
| 799 | + idx5 := strings.Index(closedSection, "closed5") |
| 800 | + |
| 801 | + // Verify all closed issues are present |
| 802 | + if idx1 == -1 || idx2 == -1 || idx3 == -1 || idx4 == -1 || idx5 == -1 { |
| 803 | + t.Fatalf("expected all closed issues in CLOSED section, got: %s", closedSection) |
| 804 | + } |
| 805 | + |
| 806 | + // Verify order: newest to oldest by UpdatedAt (closed5, closed4, closed3, closed2, closed1) |
| 807 | + if idx5 >= idx4 || idx4 >= idx3 || idx3 >= idx2 || idx2 >= idx1 { |
| 808 | + t.Errorf("expected closed issues sorted by UpdatedAt (newest first): closed5(%d), closed4(%d), closed3(%d), closed2(%d), closed1(%d)\nclosed section:\n%s", |
| 809 | + idx5, idx4, idx3, idx2, idx1, closedSection) |
| 810 | + } |
| 811 | +} |
0 commit comments