Nostro/tests/ShouldHandleMoneyAndText/main.cpp

137 lines
8.6 KiB
C++
Raw Permalink Normal View History

2026-11-01 18:35:23 +01:00
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Currencies and amounts (Nostro:Currency) and the text helpers (Nostro:Utils): every parse with what it refuses,
feat: open payto links on the pay form, and take the checkout over D-Bus out An app could have nostrod pay an order: it called Pay of net.catcrafts.Nostro1.Checkout on the system bus, and nostrod paid from an account the user had chosen, after a dialog of its own. On 2026-11-22 the owner decided that a link takes its place. The app registers for payto links (RFC 8905), so that an app or a website can hand one to the desktop, and shows a link as its pay form filled in. The link: - The app's desktop entry takes the scheme (Exec=nostro-app %u, MimeType=x-scheme-handler/payto), and nostro-app takes an argument that is no option as a link: it overwrites it in argv and hands it to App::Open once the app has started. - ReadPaymentLink (Nostro.App:Forms) reads payto://iban/<IBAN> with the options amount, receiver-name and message, and refuses a link rather than guess at it, in a sentence of the app's own with nothing of the link in it. Among what it refuses: an IBAN whose check digits do not hold, one of the three options given twice, instruction, a comma in the amount, and a name or a message with a code point that is Unshown, or Blank and no plain space. - A link that is read waits until the accounts are read and DefaultCheckoutAccount has answered or failed, with a line on the accounts' page. Then App::TakeLink fills in the pay form, which says in a banner that a link filled it in, and with more than one account opens it under the choice of accounts that the form's From row has. A link pays nothing: the form's button sends, and polkit's dialog confirms as for any payment (design rule 12, and 14, which is new). - Unshown and Blank are the two tables the worker's Shown() works from. They move into Nostro:Utils, where the app's reader asks them too. The move is within the library. The reader holds a link's name to those two and to no more of Shown(): a name with an & is read, and the dialog shows it with a + as it does any name. The checkout: - nostrod serves one interface under one bus name. Gone are net.catcrafts.Nostro1.Checkout with its XML and its activation file, the polkit action Checkout, the module Nostro.Checkout with --product=checkout, and the worker's record of orders. That changes the published contract after a release, 0.0.0, and design rule 10 records it as the owner's decision. - DefaultCheckoutAccount and SetDefaultCheckoutAccount keep their names and signatures. The account they keep is where a link's pay form starts: nostrod's dialog for choosing it says "have pay links start from", the app's setting reads "Pay links start from", and `nostro checkout account` prints "pay links start from". - The D-Bus policy still lets nostrod own the old name: the nostrod of 0.0.0 asks for it when it starts, and the system files can be newer than the Flatpak it runs from. install.sh removes the old name's activation file. A user's checkouts.cdb is read by nothing any more, and is left where it is. - The Arch package stops installing the checkout's two files, and its pkgrel goes up to 2. Where makepkg -si finds a package of the version already built it installs that one again, which would bring the activation file back. The guide now says that pkgrel goes up with a change to what the package installs. - Bus::Interface goes too: the worker called it in two places, to tell a call to the checkout from one to the main interface. The review's own files follow. gate0 loses the build of --product=checkout, and the three lens sections that named the checkout no longer do, and ask about a link. The README has "Pay links" in place of "Checkout for apps", and AGENTS.consumer.md says what the app reads of a link. Its line on lib/json.hpp no longer gives a release: the file is of none, and the guide names its commit. The pay form and the accounts' page change only by a banner each, shown while a link is in play, which docs/screenshots/take.sh never has: the README's pictures stand. Tests: ShouldValidateForms reads and refuses links and fills the form from one. ShouldDriveTheApp opens links against a real nostrod on the fake bus: before the accounts are known, with no account, with one and with several, with the accounts known and the system bus gone, for a second user whose first connection pays their own accounts only, one that cannot be read, one whose name has an &, and through polkit's dialog to the payment. It now counts nostrod's answers to hold that the account pay links start from is read again once a connection is removed: the check it had there passed without that. ShouldHandleMoneyAndText holds the two tables at the ends of their ranges. ShouldServeEveryUser holds that nostrod serves no checkout, that the bus's policy lets an older nostrod own the old name, and the account pay links start from. ShouldRunCommands holds the CLI's wording. The review's gate0 passes at this commit: the version check, 17 tests, the builds of the CLI, the library, nostrod, the fake system, the app and its --debug-api build, and five lint runs that report nothing. tests/coverage.sh reports lines, functions and regions at 100.00%. Looked at on 2026-11-22 on the made-up system (run fake), on a desktop (KWin at a scale of 1.85). At 420x760, at --scale 3 --size 360x760 and at 1100x720 in the light look: a link's pay form under the choice of accounts, the form after a tap beside that choice, and the accounts' page while a link waits, its line above the rows that stand in for the accounts (--bank-delay 10000). At 420x760: that line above what the page says when the accounts cannot be read (--polkit deny) and above the accounts themselves (--polkit slow, the link opened in the running app), the settings' row and page for the account pay links start from, the sheet for a link that cannot be read, and with --empty the sheet for no account.
2026-11-22 13:35:51 +01:00
// every format, the local-time helpers with a fixed time zone so the expectations hold anywhere, and which characters
// draw nothing or are a space.
2026-11-01 18:35:23 +01:00
#include <stdlib.h>
#include <time.h>
import std;
import Nostro;
#include "../Support/Check.inc"
using namespace Nostro;
using Test::Check;
using Test::Equal;
using Test::Throws;
namespace {
void Currencies() {
Check(GetCurrency("EUR") == Currency::Euro, "EUR is the euro");
Check(GetCurrency("eur") == Currency::Euro, "eur is the euro");
Throws([] { GetCurrency("USD"); }, "unsupported currency 'USD'", "an unknown currency is refused");
Equal(GetCurrencyCode(Currency::Euro), std::string_view("EUR"), "code");
Equal(GetCurrencySymbol(Currency::Euro), std::string_view("€"), "symbol");
Equal(GetCurrencyDecimals(Currency::Euro), std::uint8_t{2}, "decimals");
Equal(std::format("{}", Currency::Euro), std::string("€"), "a currency formats as its symbol");
}
void Amounts() {
Equal(ParseAmount("12.50", Currency::Euro), std::int64_t{1250}, "a whole amount with cents");
Equal(ParseAmount("12", Currency::Euro), std::int64_t{1200}, "an amount without a fraction");
Equal(ParseAmount("12.5", Currency::Euro), std::int64_t{1250}, "a fraction shorter than the decimals");
Equal(ParseAmount("-0.05", Currency::Euro), std::int64_t{-5}, "a negative amount");
for(const std::string_view bad : {"", ".5", "-", "1.234", "1a.00", "1.0x", "+1"}) {
Throws([bad] { ParseAmount(bad, Currency::Euro); }, "malformed amount", std::format("'{}' is refused", bad));
}
Equal(FormatAmount(1250, Currency::Euro), std::string("12.50"), "cents format with a point");
Equal(FormatAmount(5, Currency::Euro), std::string("0.05"), "less than a unit gets its leading zeros");
Equal(FormatAmount(0, Currency::Euro), std::string("0.00"), "zero");
Equal(FormatAmount(-5, Currency::Euro), std::string("-0.05"), "a negative amount");
Equal(FormatAmount(std::numeric_limits<std::int64_t>::min(), Currency::Euro), std::string("-92233720368547758.08"), "the most negative amount");
Equal(FormatMoney(-102, Currency::Euro), std::string("-€1.02"), "money puts the sign before the symbol");
Equal(FormatMoney(102, Currency::Euro), std::string("€1.02"), "and no sign when positive");
}
void Escaping() {
Equal(EscB("a\"b\\c\nd\re\tf\x01g"), std::string("a\\\"b\\\\c\\nd\\re\\tf\\u0001g"), "JSON escaping of every kind of character");
}
void Timestamps() {
using namespace std::chrono;
const system_clock::time_point time = sys_days(year(2026) / 10 / 31) + hours(12) + minutes(34) + seconds(56) + microseconds(123456);
Check(ParseTimestamp("2026-10-31 12:34:56.123456") == time, "a timestamp with microseconds");
Check(ParseTimestamp("2026-10-31 12:34:56.1") == time - microseconds(23456), "a short fraction counts in tenths");
Check(ParseTimestamp("2026-10-31 12:34:56") == time - microseconds(123456), "a timestamp without a fraction");
for(const std::string_view bad : {"2026-10-31", "2026/10-31 12:34:56", "2026-10/31 12:34:56", "2026-10-31T12:34:56", "2026-10-31 12-34:56", "2026-10-31 12:34-56", "20x6-10-31 12:34:56", "2026-10-31 12:34:56.", "2026-10-31 12:34:56.1234567", "2026-10-31 12:34:56x1", "2026-02-30 12:34:56", "2026-10-31 12:34:5x"}) {
Throws([bad] { ParseTimestamp(bad); }, "malformed timestamp", std::format("'{}' is refused", bad));
}
Equal(FormatTimestamp(time), std::string("2026-10-31 12:34:56.123456"), "FormatTimestamp is ParseTimestamp's inverse");
}
void LocalTime() {
// Amsterdam: the clocks went back on 2026-10-25, so that day lasted 25 hours.
setenv("TZ", "Europe/Amsterdam", 1);
tzset();
using namespace std::chrono;
Check(ParseLocalDate("2026-10-31") == sys_days(year(2026) / 10 / 30) + hours(23), "a date is local midnight");
Check(ParseLocalDate("2026-10-31 09:30") == sys_days(year(2026) / 10 / 31) + hours(8) + minutes(30), "a date and time is local");
Check(ParseLocalDate("2026-10-25", 1) - ParseLocalDate("2026-10-25") == hours(25), "a day of the clock change counts by the calendar");
Check(ParseLocalDate("2026-10-31", 1) == ParseLocalDate("2026-11-01"), "a day on crosses the month");
for(const std::string_view bad : {"2026-10-3", "2026/10/31", "2026-10-31T09:30", "2026-10-31 09-30", "2026-1x-31", "2026-02-30", "2026-10-31 24:00", "2026-10-31 23:60"}) {
Throws([bad] { ParseLocalDate(bad); }, "is not a date", std::format("'{}' is refused", bad));
}
Equal(FormatLocalTime(sys_days(year(2026) / 10 / 31) + hours(8) + minutes(30)), std::string("2026-10-31 09:30"), "local time format");
}
void Urls() {
Equal(UrlEncode("a-b_c.d~e f/g?h=i&j"), std::string("a-b_c.d~e%20f%2Fg%3Fh%3Di%26j"), "everything but the unreserved characters is encoded");
Equal(UrlEncode("é"), std::string("%C3%A9"), "bytes above 0x7F are encoded one by one");
Equal(UrlDecode("a%20b+c%2Fd%2f"), std::string("a b c/d/"), "percent escapes and a plus decode");
Equal(UrlDecode("100%"), std::string("100%"), "a bare percent stays");
Equal(UrlDecode("%zz%2"), std::string("%zz%2"), "an escape without two hex digits stays");
Equal(UrlDecode("%Aa"), std::string("\xaa"), "hex digits of either case");
const std::string_view url = "https://example.com/cb?code=a%2Bb&state=x+y&empty=&flag#state=fragment";
Equal(QueryParameter(url, "code").value_or("none"), std::string("a+b"), "a parameter is decoded");
Equal(QueryParameter(url, "state").value_or("none"), std::string("x y"), "the fragment is not part of the query");
Equal(QueryParameter(url, "empty").value_or("none"), std::string(""), "an empty value");
Equal(QueryParameter(url, "flag").value_or("none"), std::string(""), "a name without a value");
Check(!QueryParameter(url, "missing"), "a parameter that is not there");
Check(!QueryParameter("https://example.com/cb", "code"), "a URL without a query");
}
feat: open payto links on the pay form, and take the checkout over D-Bus out An app could have nostrod pay an order: it called Pay of net.catcrafts.Nostro1.Checkout on the system bus, and nostrod paid from an account the user had chosen, after a dialog of its own. On 2026-11-22 the owner decided that a link takes its place. The app registers for payto links (RFC 8905), so that an app or a website can hand one to the desktop, and shows a link as its pay form filled in. The link: - The app's desktop entry takes the scheme (Exec=nostro-app %u, MimeType=x-scheme-handler/payto), and nostro-app takes an argument that is no option as a link: it overwrites it in argv and hands it to App::Open once the app has started. - ReadPaymentLink (Nostro.App:Forms) reads payto://iban/<IBAN> with the options amount, receiver-name and message, and refuses a link rather than guess at it, in a sentence of the app's own with nothing of the link in it. Among what it refuses: an IBAN whose check digits do not hold, one of the three options given twice, instruction, a comma in the amount, and a name or a message with a code point that is Unshown, or Blank and no plain space. - A link that is read waits until the accounts are read and DefaultCheckoutAccount has answered or failed, with a line on the accounts' page. Then App::TakeLink fills in the pay form, which says in a banner that a link filled it in, and with more than one account opens it under the choice of accounts that the form's From row has. A link pays nothing: the form's button sends, and polkit's dialog confirms as for any payment (design rule 12, and 14, which is new). - Unshown and Blank are the two tables the worker's Shown() works from. They move into Nostro:Utils, where the app's reader asks them too. The move is within the library. The reader holds a link's name to those two and to no more of Shown(): a name with an & is read, and the dialog shows it with a + as it does any name. The checkout: - nostrod serves one interface under one bus name. Gone are net.catcrafts.Nostro1.Checkout with its XML and its activation file, the polkit action Checkout, the module Nostro.Checkout with --product=checkout, and the worker's record of orders. That changes the published contract after a release, 0.0.0, and design rule 10 records it as the owner's decision. - DefaultCheckoutAccount and SetDefaultCheckoutAccount keep their names and signatures. The account they keep is where a link's pay form starts: nostrod's dialog for choosing it says "have pay links start from", the app's setting reads "Pay links start from", and `nostro checkout account` prints "pay links start from". - The D-Bus policy still lets nostrod own the old name: the nostrod of 0.0.0 asks for it when it starts, and the system files can be newer than the Flatpak it runs from. install.sh removes the old name's activation file. A user's checkouts.cdb is read by nothing any more, and is left where it is. - The Arch package stops installing the checkout's two files, and its pkgrel goes up to 2. Where makepkg -si finds a package of the version already built it installs that one again, which would bring the activation file back. The guide now says that pkgrel goes up with a change to what the package installs. - Bus::Interface goes too: the worker called it in two places, to tell a call to the checkout from one to the main interface. The review's own files follow. gate0 loses the build of --product=checkout, and the three lens sections that named the checkout no longer do, and ask about a link. The README has "Pay links" in place of "Checkout for apps", and AGENTS.consumer.md says what the app reads of a link. Its line on lib/json.hpp no longer gives a release: the file is of none, and the guide names its commit. The pay form and the accounts' page change only by a banner each, shown while a link is in play, which docs/screenshots/take.sh never has: the README's pictures stand. Tests: ShouldValidateForms reads and refuses links and fills the form from one. ShouldDriveTheApp opens links against a real nostrod on the fake bus: before the accounts are known, with no account, with one and with several, with the accounts known and the system bus gone, for a second user whose first connection pays their own accounts only, one that cannot be read, one whose name has an &, and through polkit's dialog to the payment. It now counts nostrod's answers to hold that the account pay links start from is read again once a connection is removed: the check it had there passed without that. ShouldHandleMoneyAndText holds the two tables at the ends of their ranges. ShouldServeEveryUser holds that nostrod serves no checkout, that the bus's policy lets an older nostrod own the old name, and the account pay links start from. ShouldRunCommands holds the CLI's wording. The review's gate0 passes at this commit: the version check, 17 tests, the builds of the CLI, the library, nostrod, the fake system, the app and its --debug-api build, and five lint runs that report nothing. tests/coverage.sh reports lines, functions and regions at 100.00%. Looked at on 2026-11-22 on the made-up system (run fake), on a desktop (KWin at a scale of 1.85). At 420x760, at --scale 3 --size 360x760 and at 1100x720 in the light look: a link's pay form under the choice of accounts, the form after a tap beside that choice, and the accounts' page while a link waits, its line above the rows that stand in for the accounts (--bank-delay 10000). At 420x760: that line above what the page says when the accounts cannot be read (--polkit deny) and above the accounts themselves (--polkit slow, the link opened in the running app), the settings' row and page for the account pay links start from, the sheet for a link that cannot be read, and with --empty the sheet for no account.
2026-11-22 13:35:51 +01:00
using Range = std::pair<std::uint32_t, std::uint32_t>;
// What a password dialog is kept free of, written out again: a change to the library's list shows here.
constexpr std::array<Range, 31> unshown = {{
{0x0000, 0x001F}, {0x007F, 0x009F}, {0x00AD, 0x00AD}, {0x034F, 0x034F}, {0x0600, 0x0605}, {0x061C, 0x061C}, {0x06DD, 0x06DD}, {0x070F, 0x070F},
{0x0890, 0x0891}, {0x08E2, 0x08E2}, {0x115F, 0x1160}, {0x17B4, 0x17B5}, {0x180B, 0x180F}, {0x200B, 0x200F}, {0x2028, 0x202E}, {0x2060, 0x206F},
{0x3164, 0x3164}, {0xD800, 0xF8FF}, {0xFDD0, 0xFDEF}, {0xFE00, 0xFE0F}, {0xFEFF, 0xFEFF}, {0xFFA0, 0xFFA0}, {0xFFF0, 0xFFFC}, {0xFFFE, 0xFFFF},
{0x110BD, 0x110BD}, {0x110CD, 0x110CD}, {0x13430, 0x1343F}, {0x1BCA0, 0x1BCA3}, {0x1D173, 0x1D17A}, {0xE0000, 0xE0FFF}, {0xF0000, 0xFFFFFFFF},
}};
constexpr std::array<Range, 8> blank = {{
{0x0020, 0x0020}, {0x00A0, 0x00A0}, {0x1680, 0x1680}, {0x2000, 0x200A}, {0x202F, 0x202F}, {0x205F, 0x205F}, {0x2800, 0x2800}, {0x3000, 0x3000},
}};
// Each range at both of its ends, and not what lies just outside it.
void Characters() {
for(const Range& range : unshown) {
Check(Unshown(range.first) && Unshown(range.second), std::format("U+{:04X} to U+{:04X} draw nothing", range.first, range.second));
Check((range.first == 0 || !Unshown(range.first - 1)) && (range.second == 0xFFFFFFFF || !Unshown(range.second + 1)), std::format("and what is beside U+{:04X} to U+{:04X} is drawn", range.first, range.second));
}
for(const std::uint32_t drawn : {0x41U, 0xE9U, 0x20ACU, 0xFFFDU, 0x1F600U}) {
Check(!Unshown(drawn), std::format("U+{:04X} is drawn", drawn));
}
for(const Range& range : blank) {
Check(Blank(range.first) && Blank(range.second), std::format("U+{:04X} to U+{:04X} are spaces", range.first, range.second));
Check(!Blank(range.first - 1) && !Blank(range.second + 1), std::format("and what is beside U+{:04X} to U+{:04X} is none", range.first, range.second));
}
Check(!Blank('a') && !Blank(0x200B), "a letter is no space, and neither is a space without a width, which draws nothing");
}
2026-11-01 18:35:23 +01:00
}
int main() {
Currencies();
Amounts();
Escaping();
Timestamps();
LocalTime();
Urls();
feat: open payto links on the pay form, and take the checkout over D-Bus out An app could have nostrod pay an order: it called Pay of net.catcrafts.Nostro1.Checkout on the system bus, and nostrod paid from an account the user had chosen, after a dialog of its own. On 2026-11-22 the owner decided that a link takes its place. The app registers for payto links (RFC 8905), so that an app or a website can hand one to the desktop, and shows a link as its pay form filled in. The link: - The app's desktop entry takes the scheme (Exec=nostro-app %u, MimeType=x-scheme-handler/payto), and nostro-app takes an argument that is no option as a link: it overwrites it in argv and hands it to App::Open once the app has started. - ReadPaymentLink (Nostro.App:Forms) reads payto://iban/<IBAN> with the options amount, receiver-name and message, and refuses a link rather than guess at it, in a sentence of the app's own with nothing of the link in it. Among what it refuses: an IBAN whose check digits do not hold, one of the three options given twice, instruction, a comma in the amount, and a name or a message with a code point that is Unshown, or Blank and no plain space. - A link that is read waits until the accounts are read and DefaultCheckoutAccount has answered or failed, with a line on the accounts' page. Then App::TakeLink fills in the pay form, which says in a banner that a link filled it in, and with more than one account opens it under the choice of accounts that the form's From row has. A link pays nothing: the form's button sends, and polkit's dialog confirms as for any payment (design rule 12, and 14, which is new). - Unshown and Blank are the two tables the worker's Shown() works from. They move into Nostro:Utils, where the app's reader asks them too. The move is within the library. The reader holds a link's name to those two and to no more of Shown(): a name with an & is read, and the dialog shows it with a + as it does any name. The checkout: - nostrod serves one interface under one bus name. Gone are net.catcrafts.Nostro1.Checkout with its XML and its activation file, the polkit action Checkout, the module Nostro.Checkout with --product=checkout, and the worker's record of orders. That changes the published contract after a release, 0.0.0, and design rule 10 records it as the owner's decision. - DefaultCheckoutAccount and SetDefaultCheckoutAccount keep their names and signatures. The account they keep is where a link's pay form starts: nostrod's dialog for choosing it says "have pay links start from", the app's setting reads "Pay links start from", and `nostro checkout account` prints "pay links start from". - The D-Bus policy still lets nostrod own the old name: the nostrod of 0.0.0 asks for it when it starts, and the system files can be newer than the Flatpak it runs from. install.sh removes the old name's activation file. A user's checkouts.cdb is read by nothing any more, and is left where it is. - The Arch package stops installing the checkout's two files, and its pkgrel goes up to 2. Where makepkg -si finds a package of the version already built it installs that one again, which would bring the activation file back. The guide now says that pkgrel goes up with a change to what the package installs. - Bus::Interface goes too: the worker called it in two places, to tell a call to the checkout from one to the main interface. The review's own files follow. gate0 loses the build of --product=checkout, and the three lens sections that named the checkout no longer do, and ask about a link. The README has "Pay links" in place of "Checkout for apps", and AGENTS.consumer.md says what the app reads of a link. Its line on lib/json.hpp no longer gives a release: the file is of none, and the guide names its commit. The pay form and the accounts' page change only by a banner each, shown while a link is in play, which docs/screenshots/take.sh never has: the README's pictures stand. Tests: ShouldValidateForms reads and refuses links and fills the form from one. ShouldDriveTheApp opens links against a real nostrod on the fake bus: before the accounts are known, with no account, with one and with several, with the accounts known and the system bus gone, for a second user whose first connection pays their own accounts only, one that cannot be read, one whose name has an &, and through polkit's dialog to the payment. It now counts nostrod's answers to hold that the account pay links start from is read again once a connection is removed: the check it had there passed without that. ShouldHandleMoneyAndText holds the two tables at the ends of their ranges. ShouldServeEveryUser holds that nostrod serves no checkout, that the bus's policy lets an older nostrod own the old name, and the account pay links start from. ShouldRunCommands holds the CLI's wording. The review's gate0 passes at this commit: the version check, 17 tests, the builds of the CLI, the library, nostrod, the fake system, the app and its --debug-api build, and five lint runs that report nothing. tests/coverage.sh reports lines, functions and regions at 100.00%. Looked at on 2026-11-22 on the made-up system (run fake), on a desktop (KWin at a scale of 1.85). At 420x760, at --scale 3 --size 360x760 and at 1100x720 in the light look: a link's pay form under the choice of accounts, the form after a tap beside that choice, and the accounts' page while a link waits, its line above the rows that stand in for the accounts (--bank-delay 10000). At 420x760: that line above what the page says when the accounts cannot be read (--polkit deny) and above the accounts themselves (--polkit slow, the link opened in the running app), the settings' row and page for the account pay links start from, the sheet for a link that cannot be read, and with --empty the sheet for no account.
2026-11-22 13:35:51 +01:00
Characters();
2026-11-01 18:35:23 +01:00
return Test::Result();
}