The report answered for the whole index, which is the one window a spending report is least often asked for: last January's groceries sat in the same total as last week's, and nothing on screen said which was which. It now opens on the month that has just ended -- the last one a statement can be complete for -- and left/right step along an axis beside the totals, from all time down through the named windows to the oldest month the index holds. The period narrows the report and nothing else. reloadReport runs its own query rather than reusing the rows the transaction list is showing: the two share the account, the search and the untagged toggle, and differ only in the date bounds, so opening on last month must not hide the rest of the index from the list beside it. Nothing may put the period into m.filter, which is exactly what would make it leak. The windows are relative to today, never to the newest statement. "Last month" with nothing in it reports nothing and says so, because silently answering for a month nobody asked for is worse than an empty screen; an empty period and an empty index therefore give different messages, one asking for another period and the other for an import. The rolling windows run to the end of this month rather than to the last complete one -- "last 3 months" is asked in order to see what is happening now, and leaving out the days since the 1st answers a question nobody put. The axis is built over the whole index rather than the rows in view, or it would grow and shrink as the account or search filter changed and move under the cursor; a reload rebuilds it, since an import can reach further back, but keeps the window the user was on. s cycles how those rows are arranged: largest out first as before, then in, net lowest first so the biggest losses lead, count, and the tag A to Z. A letter rather than a chord because the report is not a form. The marked heading says which column the rows are read from and the help names what the key does next, as the rule builder's preview already does. The sort rearranges rows and never changes which rows there are, so the order stays out of the filter for the same reason the period does. Currency remains the outer key under every order -- there are no rates here, so two currencies interleaved by amount would invite a comparison that cannot be made -- and every order falls back to the tag, so ties keep a fixed position instead of reshuffling between reloads. money report takes the same choice as --sort out|in|net|count|tag, and a misspelt one is refused rather than silently reporting in the default order. It keeps --month and has no equivalent of the wider windows. store.Filter gains From and To, compared as strings because dates are stored ISO-8601 and a string comparison is therefore a date comparison. store.Months replaces report.Months, which nothing had ever called: the axis needs the months of the whole index, not of a slice already in hand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
103 lines
3.1 KiB
Go
103 lines
3.1 KiB
Go
package report
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func on(s string) time.Time {
|
|
t, err := time.Parse("2006-01-02", s)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return t
|
|
}
|
|
|
|
// The axis opens on the month that has just ended, whatever the index holds.
|
|
func TestPeriodsDefaultsToLastMonth(t *testing.T) {
|
|
ps := Periods(on("2026-08-23"), []string{"2026-08", "2026-07", "2026-06"})
|
|
p := ps[DefaultIndex(ps)]
|
|
if p.Label != LastMonth {
|
|
t.Fatalf("default = %q, want %q", p.Label, LastMonth)
|
|
}
|
|
if p.From != "2026-07-01" || p.To != "2026-07-31" {
|
|
t.Errorf("last month = %s…%s, want the whole of July", p.From, p.To)
|
|
}
|
|
if p.Span != "2026-07" {
|
|
t.Errorf("span = %q, want the month it stands for", p.Span)
|
|
}
|
|
}
|
|
|
|
// An index that stops short of last month still opens there: an empty report
|
|
// for the window asked for beats a full one for a window nobody chose.
|
|
func TestPeriodsDefaultsToLastMonthWithNothingInIt(t *testing.T) {
|
|
ps := Periods(on("2026-08-23"), []string{"2026-03", "2026-02"})
|
|
if p := ps[DefaultIndex(ps)]; p.Label != LastMonth {
|
|
t.Fatalf("default = %q, want %q", p.Label, LastMonth)
|
|
}
|
|
}
|
|
|
|
// The named windows come first, widest to narrowest, and the months follow in
|
|
// reverse order, skipping the ones the named windows already name.
|
|
func TestPeriodsOrder(t *testing.T) {
|
|
ps := Periods(on("2026-08-23"), []string{"2026-09", "2026-08", "2026-07", "2026-06", "2026-01"})
|
|
var got []string
|
|
for _, p := range ps {
|
|
got = append(got, p.Label)
|
|
}
|
|
want := []string{
|
|
"all time", "this year", "last 12 months", "last 3 months",
|
|
"this month", LastMonth, "2026-06", "2026-01",
|
|
}
|
|
if len(got) != len(want) {
|
|
t.Fatalf("periods = %v, want %v", got, want)
|
|
}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("periods = %v, want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A month is bounded by its own length, not by a fixed 30 or 31 days.
|
|
func TestPeriodsMonthEnds(t *testing.T) {
|
|
ps := Periods(on("2026-03-15"), []string{"2026-02"})
|
|
p := ps[DefaultIndex(ps)]
|
|
if p.From != "2026-02-01" || p.To != "2026-02-28" {
|
|
t.Errorf("February 2026 = %s…%s, want the 1st to the 28th", p.From, p.To)
|
|
}
|
|
}
|
|
|
|
// A rolling window runs to the end of this month rather than to the last
|
|
// complete one: it is asked in order to see what is happening now.
|
|
func TestPeriodsRollingWindows(t *testing.T) {
|
|
ps := Periods(on("2026-08-23"), nil)
|
|
for _, tc := range []struct{ label, from, to string }{
|
|
{"last 3 months", "2026-06-01", "2026-08-31"},
|
|
{"last 12 months", "2025-09-01", "2026-08-31"},
|
|
{"this year", "2026-01-01", "2026-12-31"},
|
|
} {
|
|
var found bool
|
|
for _, p := range ps {
|
|
if p.Label != tc.label {
|
|
continue
|
|
}
|
|
found = true
|
|
if p.From != tc.from || p.To != tc.to {
|
|
t.Errorf("%s = %s…%s, want %s…%s", tc.label, p.From, p.To, tc.from, tc.to)
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("no %q window", tc.label)
|
|
}
|
|
}
|
|
}
|
|
|
|
// All time is unbounded on both sides, so nothing has to special-case it.
|
|
func TestPeriodsAllTimeIsUnbounded(t *testing.T) {
|
|
ps := Periods(on("2026-08-23"), []string{"2026-01"})
|
|
if ps[0].Label != "all time" || ps[0].From != "" || ps[0].To != "" {
|
|
t.Errorf("first window = %+v, want an unbounded all time", ps[0])
|
|
}
|
|
}
|