## Summary
A new page **Stammdaten → Texte bereinigen** (`/text-cleanup?acc=<number>`) to view and clean the booking texts of one account, e.g. "Ferien", in the year selected in the header.
Scope, as agreed: one account, the selected year, **only the `Text` field**, only existing bookings. Booking rules and the booking-text list are not touched.
### Page
- **Account dropdown**, filter, and a table of all bookings of the account (both sides, so refunds are negative): date, amount, counter account, the **editable text**, and the bank text as a read-only hint.
- **Group panel:** the texts grouped by first word, exactly as the Analyse page groups them, with count and total. It updates live while editing, and clicking a group filters the table.
- **Tools, acting on the rows currently shown** (the filter limits their reach):
- **Search and replace:** plain text, or regex with `$1` groups. For example `^(.+) Luzern$` → `Luzern $1` turns "Migros Luzern" into "Luzern Migros".
- **Leerzeichen bereinigen:** trims and collapses repeated spaces.
- **Several rows at once:** checkboxes plus "Text setzen" give selected rows one text.
- **Changes:** changed rows are highlighted, with the old text crossed out underneath. Nothing is written until **Speichern (n)**; **Verwerfen** discards.
- **Unsaved changes:** leaving the page asks first (same `unsaved-guard.js` as the JSON editor).
- **Entry points:** the menu, a new button per account on **Konten**, and a "Texte <Jahr> bereinigen" link on the **Analyse** page at account level.
### Saving
`RawDataRepository.SaveTransactionTextsAsync` changes only the `Text` of the given bookings (by ID):
- refused if the file changed since loading (SHA-256), if a text is empty, or if a booking no longer exists;
- the previous version is backed up to `backup/{mandator}/{year}/json-{timestamp}/`, then saved atomically through `BaseRepository`, so cached data is refreshed;
- the file is read once, so the backup is exactly the version that was checked.
`TextReplacement` (Services) holds the search and replace logic:
- plain search is literal, including a `$` in the replacement;
- regex has a 500 ms timeout against runaway patterns;
- invalid patterns give a German error message.
### Layout fix (app-wide)
The content area (`.body-content`) scrolls its own content and was only as tall as the page, so on short pages open dropdowns were cut off after a line or two. It now fills at least the window between header and footer (`app.css`). On short pages the footer therefore sits at the bottom of the window. The account list is also limited to 360 px and scrolls.
## Test plan
- [x] `dotnet build`: no warnings
- [x] `dotnet test`: 111/111 pass, including 13 new tests:
- 5 `RawDataRepositoryTestShould`: load with hash, only Text changes plus backup, empty text refused, unknown booking refused, changed file refused
- 8 `TextReplacementTestShould`: case handling, literal special characters, regex groups, no match, invalid regex, empty search, tidy
- [x] Headless Chrome on a copy of the real data, Ferien account (21 bookings in 2026):
- "Mailand" → "Milano" via replace, 1 direct edit, and 3 rows set via "Text setzen"; the group panel showed "Milano" before saving
- after saving, exactly those 7 texts changed on disk, no other field of any booking, and a backup was written
- [x] Opening the page without an account: hint shown, no render error. An early `return` inside an element first caused "A frame of type 'Element' was left unclosed"; fixed and checked.
- [x] Account dropdown on the empty page opens fully (360 px, scrollable) within the content area
- [x] Links from Konten and Analyse open the page with the account preselected
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Reviewed-on: #8
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
238 lines
8.8 KiB
C#
238 lines
8.8 KiB
C#
using Microsoft.Extensions.Logging.Abstractions;
|
|
using NSubstitute;
|
|
using Schaad.Accounting.Datasets;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Repositories;
|
|
using Shouldly;
|
|
|
|
namespace Schaad.Accounting.Tests;
|
|
|
|
public class RawDataRepositoryTestShould : IDisposable
|
|
{
|
|
private const string Accounts = """
|
|
[
|
|
{ "Id": "a1", "Number": 1020, "Name": "Bank", "Currency": "CHF" },
|
|
{ "Id": "a2", "Number": 4000, "Name": "Lebensmittel", "Currency": "CHF" }
|
|
]
|
|
""";
|
|
|
|
private const string Transactions = """
|
|
[
|
|
{ "Id": "t1", "ValueDate": "2026-01-10T00:00:00", "Text": "Migros", "Value": 12.5, "OriginAccountId": "a1", "TargetAccountId": "a2" }
|
|
]
|
|
""";
|
|
|
|
private readonly string dataPath;
|
|
private readonly string dbDir;
|
|
private readonly RepositoryCache cache = new();
|
|
private readonly ISettingsService settingsService;
|
|
private readonly RawDataRepository sut;
|
|
|
|
public RawDataRepositoryTestShould()
|
|
{
|
|
dataPath = Path.Combine(Path.GetTempPath(), "AccountingNext-tests-" + Guid.NewGuid().ToString("N"));
|
|
dbDir = Path.Combine(dataPath, "Mandator", "2026");
|
|
Directory.CreateDirectory(dbDir);
|
|
File.WriteAllText(Path.Combine(dbDir, "Accounts.json"), Accounts);
|
|
File.WriteAllText(Path.Combine(dbDir, "Transactions.json"), Transactions);
|
|
|
|
settingsService = Substitute.For<ISettingsService>();
|
|
settingsService.GetDbPath().Returns(dbDir);
|
|
settingsService.GetLastYearDbPath().Returns(Path.Combine(dataPath, "no-such-dir"));
|
|
settingsService.GetSettings().Returns(new SettingsDataset { DataPath = dataPath });
|
|
settingsService.GetMandator().Returns("Mandator");
|
|
settingsService.GetYear().Returns(2026);
|
|
|
|
sut = new RawDataRepository(settingsService, cache, NullLogger<RawDataRepository>.Instance);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (Directory.Exists(dataPath))
|
|
{
|
|
Directory.Delete(dataPath, recursive: true);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReturnFileTextWhenLoading()
|
|
{
|
|
var file = await sut.LoadAsync("Transactions.json");
|
|
|
|
file.Text.ShouldBe(Transactions);
|
|
file.Hash.ShouldNotBeNullOrEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SaveAndBackUpPreviousVersionWhenTextIsValid()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
var edited = loaded.Text.Replace("\"Migros\"", "\"Migros Bern\"");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", edited, loaded.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
result.Saved.ShouldBeTrue();
|
|
result.File!.Text.ShouldContain("\"Text\": \"Migros Bern\"");
|
|
result.File.Hash.ShouldNotBe(loaded.Hash);
|
|
File.ReadAllText(result.BackupPath!).ShouldBe(Transactions);
|
|
result.BackupPath!.ShouldStartWith(Path.Combine(dataPath, "backup", "Mandator", "2026", "json-"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefreshCachedDataWhenSaving()
|
|
{
|
|
var transactionRepository = new TransactionRepository(settingsService, cache, Substitute.For<IAccountRepository>(), NullLogger<TransactionRepository>.Instance);
|
|
(await transactionRepository.GetTransactionListAsync()).Single().Text.ShouldBe("Migros");
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
await sut.SaveAsync("Transactions.json", loaded.Text.Replace("\"Migros\"", "\"Coop\""), loaded.Hash);
|
|
|
|
(await transactionRepository.GetTransactionListAsync()).Single().Text.ShouldBe("Coop");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectInvalidJsonWithLineAndColumnWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", "[\n { \"Id\": \"t1\",, }\n]", loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldStartWith("Zeile 2, Spalte");
|
|
File.ReadAllText(Path.Combine(dbDir, "Transactions.json")).ShouldBe(Transactions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectUnknownPropertyWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", loaded.Text.Replace("\"Value\"", "\"Vlaue\""), loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("Vlaue");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectUnknownAccountWhenSavingTransactions()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", loaded.Text.Replace("\"a2\"", "\"a9\""), loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("TargetAccountId \"a9\"");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectMissingAndDuplicateIdsWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Accounts.json");
|
|
var text = """
|
|
[
|
|
{ "Id": "a1", "Number": 1020, "Name": "Bank" },
|
|
{ "Id": "a1", "Number": 4000, "Name": "Lebensmittel" },
|
|
{ "Number": 4100, "Name": "Miete" }
|
|
]
|
|
""";
|
|
|
|
var result = await sut.SaveAsync("Accounts.json", text, loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.ShouldContain(e => e.Contains("Eintrag 3"));
|
|
result.Errors.ShouldContain(e => e.Contains("\"a1\" kommt 2-mal vor"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectRemovingAccountStillUsedByTransactionsWhenSaving()
|
|
{
|
|
var loaded = await sut.LoadAsync("Accounts.json");
|
|
var text = """[ { "Id": "a1", "Number": 1020, "Name": "Bank" } ]""";
|
|
|
|
var result = await sut.SaveAsync("Accounts.json", text, loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("\"a2\" fehlt, wird aber von 1 Buchungen");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseToOverwriteWhenFileChangedSinceLoading()
|
|
{
|
|
var loaded = await sut.LoadAsync("Transactions.json");
|
|
File.WriteAllText(Path.Combine(dbDir, "Transactions.json"), Transactions.Replace("12.5", "13"));
|
|
|
|
var result = await sut.SaveAsync("Transactions.json", loaded.Text, loaded.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("seit dem Laden geändert");
|
|
File.ReadAllText(Path.Combine(dbDir, "Transactions.json")).ShouldContain("13");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RejectUnknownFileNameWhenLoading()
|
|
{
|
|
await Should.ThrowAsync<ArgumentException>(() => sut.LoadAsync("../appsettings.json"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ReturnParsedTransactionsWithHashWhenLoadingTransactions()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
snapshot.Transactions.Single().Text.ShouldBe("Migros");
|
|
snapshot.Hash.ShouldBe((await sut.LoadAsync("Transactions.json")).Hash);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task ChangeOnlyTheTextWhenSavingTransactionTexts()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["t1"] = "Luzern Migros" }, snapshot.Hash);
|
|
|
|
result.Errors.ShouldBeEmpty();
|
|
var saved = (await sut.LoadTransactionsAsync()).Transactions.Single();
|
|
saved.Text.ShouldBe("Luzern Migros");
|
|
saved.Value.ShouldBe(12.5m);
|
|
saved.OriginAccountId.ShouldBe("a1");
|
|
saved.TargetAccountId.ShouldBe("a2");
|
|
saved.ValueDate.ShouldBe(new DateTime(2026, 1, 10));
|
|
File.ReadAllText(result.BackupPath!).ShouldBe(Transactions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseEmptyTextWhenSavingTransactionTexts()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["t1"] = " " }, snapshot.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("darf nicht leer sein");
|
|
File.ReadAllText(Path.Combine(dbDir, "Transactions.json")).ShouldBe(Transactions);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseUnknownTransactionWhenSavingTransactionTexts()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["gone"] = "Text" }, snapshot.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("\"gone\" gibt es nicht mehr");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RefuseWhenFileChangedSinceLoadingTransactions()
|
|
{
|
|
var snapshot = await sut.LoadTransactionsAsync();
|
|
File.WriteAllText(Path.Combine(dbDir, "Transactions.json"), Transactions.Replace("12.5", "13"));
|
|
|
|
var result = await sut.SaveTransactionTextsAsync(new Dictionary<string, string> { ["t1"] = "Coop" }, snapshot.Hash);
|
|
|
|
result.Saved.ShouldBeFalse();
|
|
result.Errors.Single().ShouldContain("seit dem Laden geändert");
|
|
}
|
|
}
|