Files
sure/test/models/series_test.rb
T
f0a0013da9 fix(charts): round series values to the currency's display precision (#3091)
* fix(charts): round chart values to the currency's display precision

Charts are drawn from the serialized amounts, not from the formatted strings,
so a sub-unit residue plotted as a visible move between two points that both
read $0.00, and the trend between them reported a change. Round in
`Series#as_json` so the series values stay exact for insights, goals and the
assistant.

Also hide the percentage when the previous value is zero, since that makes it
infinite.

* fix(charts): round the two payloads that bypass Series#as_json

`NetWorthBreakdownSeriesBuilder` builds its payload by hand, so the reports
chart never went through the rounding added in `Series#as_json` and still
plotted raw amounts: adjacent points printing the same value rendered a
visible move, and the tooltip it inherits reported a change between them.

`Series#trend` had the same gap on the server side. It is rendered right above
the chart by `UI::Account::Chart` and by the reports summary, so an account
going from 0 to a sub-cent residue showed a coloured $0.00 change next to a
flat line.

Rounding the trend can make a previously finite percentage infinite, so guard
the three views that render `percent_formatted` without checking, as
`shared/_trend_change` already does.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(charts): address net worth review comments

* fix(charts): tighten rounded trend handling

* fix(reports): restore positive sign in print trend

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-authored-by: sure-admin <sure-admin@splashblot.com>
2026-08-22 04:30:23 +02:00

80 lines
2.7 KiB
Ruby

require "test_helper"
class SeriesTest < ActiveSupport::TestCase
test "a change between two values that print identically reads as no change" do
series = build_series(previous: 100.001, current: 100.004, currency: "USD")
trend = series.as_json[:values].last[:trend]
# Both amounts format as $100.00
assert_equal 0, trend.value.amount
assert trend.direction.flat?
end
test "rounds the trend at the currency's display precision" do
series = build_series(previous: 1000.4, current: 1000.6, currency: "JPY")
trend = series.as_json[:values].last[:trend]
# ¥1,000 -> ¥1,001
assert_equal 1, trend.value.amount
assert trend.direction.up?
end
test "does not serialize a sub-unit residue the chart would plot as a move" do
# A leftover cent fraction (400 - 199.9999 - 200) prints as $0.00, but the
# chart is drawn from the amount, so an unrounded 0.0001 fills the y axis
series = build_series(previous: 0, current: 0.0001, currency: "USD")
assert_equal 0, series.as_json[:values].last[:value].amount
end
test "leaves the series values themselves exact" do
series = build_series(previous: 100.001, current: 100.004, currency: "USD")
# Insights, goals and the assistant read the values directly
assert_equal 100.004, series.values.last.value.amount
assert_equal 0.003, series.values.last.trend.value.amount
end
test "the series trend ignores a change that is not visible" do
# The account chart renders this trend right above the chart itself
series = build_series(previous: 0, current: 0.0001, currency: "USD")
assert series.trend.direction.flat?
assert_equal 0, series.trend.value.amount
assert series.trend.percent.finite?
end
private
def build_series(previous:, current:, currency:)
Series.new(
start_date: Date.new(2026, 6, 1),
end_date: Date.new(2026, 6, 2),
interval: "1 day",
values: [
Series::Value.new(
date: Date.new(2026, 6, 1),
date_formatted: "June 1, 2026",
value: Money.new(previous, currency),
trend: Trend.new(
current: Money.new(previous, currency),
previous: Money.new(previous, currency),
favorable_direction: "up"
)
),
Series::Value.new(
date: Date.new(2026, 6, 2),
date_formatted: "June 2, 2026",
value: Money.new(current, currency),
trend: Trend.new(
current: Money.new(current, currency),
previous: Money.new(previous, currency),
favorable_direction: "up"
)
)
]
)
end
end