Value positions from a daily price feed

A position was a share count. An instrument now carries a market symbol and
the last close fetched for it, so Wealth and the dashboard report cash plus
market value instead of cash alone.

The symbol is chosen by hand and never derived: one ISIN lists on several
exchanges in different currencies, and a price from the wrong listing misstates
wealth without failing any check. The refresh refuses a quote whose currency
differs from the instrument's, keeps the previous quote when a symbol cannot be
priced, and counts an instrument with no symbol as unpriced - naming it in a
check and leaving it out of every total, because cost is not value. The quote
belongs to the job: saving an instrument can neither set nor erase it, and
changing the symbol discards it.

Two things the provider forced. It answers HTTP 429 to every request whose
User-Agent names a programming language, so the client identifies as a browser;
without that header the first call of the day fails. Its closes are 32-bit
floats widened to 64 - 165.26 arrives as 165.25999450683594 - so a figure is
rounded to seven significant digits, which is what 24 mantissa bits carry;
eight would have stored 165.25999 as a price.

Accepted quotes are written in one commit against a revision re-read after the
fetches, and nothing is committed when no quote changed. The automatic run
starts shortly after launch and repeats daily on its own timer, so a sync
backoff cannot delay it and prices arrive with no bank connected.

Verified against live quotes end to end: 80 shares at 125.45 and 40 at 165.26
on 6000.00 cash report 22646.40 with one holding named as unpriced; giving that
holding a symbol through the UI moves the figure to 23530.50, and a second
refresh leaves the revision untouched.
This commit is contained in:
Lars Nolden
2026-09-12 18:42:07 +02:00
parent 2373790be3
commit 588c16ad19
19 changed files with 1580 additions and 55 deletions
+196
View File
@@ -0,0 +1,196 @@
package quotes
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// secret stands in for anything a provider might put in a response body: no
// part of it may reach a message shown to the user.
const secret = "SUPER-SECRET-PROVIDER-TEXT"
func stub(t *testing.T, handler http.HandlerFunc) Client {
t.Helper()
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
return Client{BaseURL: server.URL, HTTPClient: server.Client()}
}
func body(payload string) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(payload))
}
}
const chartVWCE = `{"chart":{"result":[{"meta":{"currency":"EUR","symbol":"VWCE.DE","exchangeName":"GER"},
"timestamp":[1757376000,1757462400],
"indicators":{"quote":[{"close":[127.11,128.42],"volume":[1,2]}]}}],"error":null}}`
func TestLatestReadsLastClose(t *testing.T) {
var path, query string
client := stub(t, func(w http.ResponseWriter, r *http.Request) {
path, query = r.URL.Path, r.URL.RawQuery
body(chartVWCE)(w, r)
})
quote, err := client.Latest(context.Background(), "VWCE.DE")
if err != nil {
t.Fatal(err)
}
if quote.Symbol != "VWCE.DE" || quote.Price != "128.42" || quote.Currency != "EUR" || quote.Day != "2025-09-10" {
t.Fatalf("quote: %+v", quote)
}
if path != "/v8/finance/chart/VWCE.DE" || query != "range=5d&interval=1d" {
t.Fatalf("request: %q %q", path, query)
}
}
func TestLatestSkipsTrailingNullCloses(t *testing.T) {
client := stub(t, body(`{"chart":{"result":[{"meta":{"currency":"EUR"},
"timestamp":[1757376000,1757462400,1757548800],
"indicators":{"quote":[{"close":[127.11,128.42,null]}]}}],"error":null}}`))
quote, err := client.Latest(context.Background(), "VWCE.DE")
if err != nil {
t.Fatal(err)
}
// The day must come from the candle that priced, not from the newest one.
if quote.Price != "128.42" || quote.Day != "2025-09-10" {
t.Fatalf("quote: %+v", quote)
}
}
func TestLatestReportsForeignCurrency(t *testing.T) {
client := stub(t, body(`{"chart":{"result":[{"meta":{"currency":"USD"},
"timestamp":[1757376000],"indicators":{"quote":[{"close":[9.4079999923706]}]}}],"error":null}}`))
quote, err := client.Latest(context.Background(), "VUSA")
if err != nil {
t.Fatal(err)
}
// A foreign currency is the caller's decision to reject, not a fetch failure.
if quote.Currency != "USD" || quote.Price != "9.408" {
t.Fatalf("quote: %+v", quote)
}
}
func TestLatestRejectsUnusableResponses(t *testing.T) {
cases := []struct {
name string
handler http.HandlerFunc
}{
{"every close null", body(`{"chart":{"result":[{"meta":{"currency":"EUR"},
"timestamp":[1757376000,1757462400],"indicators":{"quote":[{"close":[null,null]}]}}],"error":null}}`)},
{"server failure", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"chart":{"result":null,"error":{"description":"` + secret + `"}}}`))
}},
{"chart error", body(`{"chart":{"result":null,"error":{"code":"Not Found","description":"` + secret + `"}}}`)},
{"empty result", body(`{"chart":{"result":[],"error":null}}`)},
{"no currency", body(`{"chart":{"result":[{"meta":{"currency":"eur"},
"timestamp":[1757376000],"indicators":{"quote":[{"close":[128.42]}]}}],"error":null}}`)},
{"close not positive", body(`{"chart":{"result":[{"meta":{"currency":"EUR"},
"timestamp":[1757376000],"indicators":{"quote":[{"close":[0]}]}}],"error":null}}`)},
{"close without timestamp", body(`{"chart":{"result":[{"meta":{"currency":"EUR"},
"timestamp":[],"indicators":{"quote":[{"close":[128.42]}]}}],"error":null}}`)},
{"not json", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("<html>" + secret + "</html>")) }},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
quote, err := stub(t, c.handler).Latest(context.Background(), "VWCE.DE")
if err == nil {
t.Fatalf("expected failure, got %+v", quote)
}
var provider Error
if !errors.As(err, &provider) || provider.Symbol != "VWCE.DE" || provider.Reason == "" {
t.Fatalf("want typed provider error, got %#v", err)
}
if strings.Contains(err.Error(), secret) {
t.Fatalf("response text leaked into %q", err)
}
if !strings.Contains(err.Error(), "VWCE.DE") {
t.Fatalf("error must name the symbol: %q", err)
}
})
}
}
func TestLatestRejectsUnusableSymbolAndAddress(t *testing.T) {
client := stub(t, func(http.ResponseWriter, *http.Request) {
t.Fatal("no request may be made for a rejected symbol or address")
})
if _, err := client.Latest(context.Background(), "../secrets"); err == nil {
t.Fatal("expected a path-shaping symbol to be rejected")
}
plain := Client{BaseURL: "http://prices.example.com"}
if _, err := plain.Latest(context.Background(), "VWCE.DE"); err == nil {
t.Fatal("expected non-loopback plain HTTP to be rejected")
}
}
func TestLatestKeepsCancellationIdentity(t *testing.T) {
client := stub(t, body(chartVWCE))
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := client.Latest(ctx, "VWCE.DE"); !errors.Is(err, context.Canceled) {
t.Fatalf("want context.Canceled, got %#v", err)
}
}
func TestDecimalQuantityRoundsHalfAwayFromZero(t *testing.T) {
cases := []struct {
text string
want string
}{
// Real closes, copied from a live response: every one is a 32-bit float
// widened to 64, and the decimal the exchange published has to come
// back out of it.
{"165.25999450683594", "165.26"},
{"125.44999694824219", "125.45"},
{"127.1449966430664", "127.145"},
{"167.77999877929688", "167.78"},
{"0.41578700000001", "0.415787"},
{"9.4079999923706", "9.408"},
{"-9.4079999923706", "-9.408"},
{"128.42", "128.42"},
{"0.000000005", "0.00000001"},
{"0.000000004", "0"},
{"0.999999995", "1"},
{"42", "42"},
{"0007.5", "7.5"},
// Past the seventh digit the provider is describing its own encoding,
// so the eighth place moves rather than being preserved.
{"12345.678912345", "12345.68"},
{"12345678.94999999", "12345680"},
}
for _, c := range cases {
got, err := decimalQuantity(c.text)
if err != nil || string(got) != c.want {
t.Fatalf("decimalQuantity(%q) = %q, %v; want %q", c.text, got, err, c.want)
}
}
for _, text := range []string{"", "-", ".5", "5.", "1.2.3", "1e5", "12e-3", "abc", "1 2", "999999999", "99999999.999999995"} {
if got, err := decimalQuantity(text); err == nil {
t.Fatalf("decimalQuantity(%q) = %q, want an error", text, got)
}
}
}
// The provider answers 429 to every request whose agent names a programming
// language, so a missing or Go-default User-Agent breaks every quote on the
// first call rather than under load. The header is load-bearing, not decor.
func TestLatestIdentifiesAsABrowser(t *testing.T) {
agent := "unset"
client := stub(t, func(w http.ResponseWriter, r *http.Request) {
agent = r.Header.Get("User-Agent")
body(chartVWCE)(w, r)
})
if _, err := client.Latest(context.Background(), "VWCE.DE"); err != nil {
t.Fatal(err)
}
if !strings.HasPrefix(agent, "Mozilla/") || strings.Contains(agent, "Go-http-client") {
t.Fatalf("User-Agent %q is refused by the provider", agent)
}
}