Files
frameledger/tests/FrameLedger.App.Tests/FirstRunViewModelTests.cs
poli0981andClaude Opus 5.5 49b9084c1c feat: an alpha says what it is and asks first - Alpha notice + tick box, the alpha Agent's wait, ALPHA badge/banner, ledger copy; EULA 1.6, Disclaimer 2.12 (D73-D75) (beta.32 PR-2) (#346)
* feat: an alpha says what it is and asks first - the Alpha notice with its own tick box, the alpha Agent's wait, ALPHA badge and banner, the ledger copy before an alpha opens it; EULA 1.6, Disclaimer 2.12 (D73-D75) (beta.32 PR-2)

The owner's decisions of 2026-10-31, asked while planning: the alpha replaces the beta and shares
its data folder, copying the ledger before it first opens it (D73); a versioned Alpha notice with
its own tick box in the Legal Gate, the alpha's Agent waiting for it, a badge and a banner (D74);
the EULA and the Disclaimer made true about alphas (D75). Nothing of it shows in a beta.

- legal/ALPHA_NOTICE.md 1.0 (the approved text), embedded; Shared.AlphaNotice holds its key and
  version for both processes. LegalDocuments.Load(BuildLine) adds it after the four on the alpha
  line; About lists it on every line, for information.
- First run: on an alpha's gate a second warning box with the tick box; Accept is disabled until
  it is ticked. While the notice is outstanding the App holds the Agent's start - a hold of its
  own beside the updater's, set before the host starts (the connection's first round runs under
  the first-run window) - and Accept releases it and starts the Agent at once.
- Agent: an alpha's --serve reads legal_acceptance after opening the ledger and exits 8 until the
  notice is accepted at this build's version (the logon task can start it first); maintenance
  verbs are not held. Its log names the release line and the runtime.
- The shell: ALPHA badge beside the Agent pill (tooltip: the runtime), window title "FrameLedger
  Alpha", a Warning InfoBar at every start, closable for the run, opening the notice. About and
  --diag name the runtime; Settings locks the update channel on an alpha and says why.
- The ledger: LedgerDatabase.OpenAsync(backupFirst:) - an alpha copies the ledger with SQLite's
  online backup into backups\ledger-before-<version>.db before anything opens it for writing, once
  per version, under a lock of its own (the migration lock's 30 s would time a slow copy out); the
  newest three are kept and the copy just made is never pruned; a copy that cannot be made refuses
  the open (App: start-failure dialog; Agent: exit 7). Never for a viewer.
- EULA 1.6 (§2B) and Disclaimer 2.12 (§0): "pre-release builds - betas, and alphas". 12 strings in
  en/vi/ja (ja marked review).
- Docs: 08_UI, 06_DATA_MODEL §Migrations, 01_ARCHITECTURE §Lifecycle and §Data directory,
  11_UPDATER, 12_BUILD, LIMITATIONS §Alpha builds, guide/11-alpha-builds.md (+ contents, sidebar,
  the App's page list), guide/01-install, terms-in-plain-words, wiki/Releasing, CHANGELOG.
- Tests: FirstRunViewModel (tick, hold release), LegalDocuments (both lines; the constant equals
  the document's version), AgentConnection (the notice hold survives the updater's release),
  SettingsViewModel (locked picker), LedgerBackup (once per version, rows copied, keep three, the
  copy just made survives a skewed clock, a copy that cannot be made refuses the open, no request
  copies nothing), AlphaNoticeGate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix: the ALPHA badge in the accent appearance, the picker's alpha lock on a container, About's legal list in its test (beta.32 PR-2)

The local gate's three reds on the previous commit, each a design test doing its job:
- ContrastTests: a badge's literal Appearance may not be a palette one (Danger paints outside the
  theme dictionaries, 08_UI §Contrast) - the ALPHA badge is Primary, the accent with its own text.
- ViewerModeTests: the update-channel picker keeps the viewer switch on the control itself; the
  alpha line's lock is a Grid around it bound to CanChooseUpdateChannel.
- DocumentWindowsTests: About's legal list now names legal/ALPHA_NOTICE.md (on every line, D74).

App.Tests 725/725.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: the alpha's start logic out of App.xaml.cs into AlphaStart, tested; the notice window, the exception, the no-op holds (beta.32 PR-2)

SonarCloud's quality gate on #346: new-code coverage 69.3 % against 80 %. The uncovered new lines
were the App's start helpers, DocumentWindows.ShowAlphaNotice, MainWindowViewModel's alpha
properties, LedgerBackupException's constructors and two default interface members.

- Services/AlphaStart: BackupFor (the copy only for an alpha over its own folder) and
  HoldTheAgentForTheNoticeAsync (the hold only while the notice is outstanding), which App.xaml.cs
  now calls; AlphaStartTests covers both and the viewer's and a window set's no-op members.
- DocumentWindowsTests: the alpha banner's button opens the notice in one window and asking again
  reveals it. LedgerBackupTests: the exception's three constructors.

App.Tests 730/730; dotnet format clean.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs: one Changed heading under [Unreleased] after main came in (beta.32 PR-2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 15:50:05 +00:00

230 lines
11 KiB
C#

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 - additional terms under GPLv3 section 7: see NOTICE
using FluentAssertions;
using FrameLedger.App.Services;
using FrameLedger.App.ViewModels;
using FrameLedger.Application.Persistence;
using FrameLedger.Infrastructure.Persistence;
namespace FrameLedger.App.Tests;
/// <summary>
/// FR-11: required on an empty ledger and after a version moves, one Accept writes a row per document that needs
/// acceptance at the documents' versions — three since beta.12 (D45): the GPL is shown for information — a decline writes
/// nothing and ends the flow, the steps advance, and read-only mode records nothing.
/// </summary>
public sealed class FirstRunViewModelTests
{
private static CancellationToken Ct => TestContext.Current.CancellationToken;
private static IReadOnlyList<LegalDocument> Docs { get; } =
[
new("eula", "EULA", "1.0", "text", new Uri("https://github.com/x/eula")),
new("gpl", "GPL", "GPL-3.0-only", "text", new Uri("https://github.com/x/gpl"), RequiresAcceptance: false),
new("disclaimer", "Disclaimer", "2.0", "text", new Uri("https://github.com/x/d")),
new("privacy", "Privacy", "2.0", "text", new Uri("https://github.com/x/p")),
];
/// <summary>The main line's four and an alpha's notice after them (D74), as <c>LegalDocuments.Load(BuildLine.Alpha)</c> orders them.</summary>
private static IReadOnlyList<LegalDocument> AlphaDocs { get; } =
[
.. Docs,
new(LegalDocuments.Alpha, "Alpha notice", Shared.AlphaNotice.Version, "text", new Uri("https://github.com/x/a")),
];
[Fact]
public async Task AnAlphasGateWaitsForTheTickAndItsAcceptStartsTheAgent()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var store = new SqliteLegalAcceptanceStore(s.Db);
var link = new FakeAgentLink();
using var vm = new FirstRunViewModel(new LegalGate(store, AlphaDocs), link, readOnly: false);
vm.AlphaTickRequired.Should().BeTrue("the Alpha notice is among the documents to accept");
vm.AcceptCommand.CanExecute(null).Should().BeFalse("D74: Accept waits for the alpha's own tick box");
vm.AlphaAcknowledged = true;
vm.AcceptCommand.CanExecute(null).Should().BeTrue();
await vm.AcceptCommand.ExecuteAsync(null);
(await store.ListAsync(Ct)).Select(static r => (r.Document, r.Version)).Should().Contain((LegalDocuments.Alpha, Shared.AlphaNotice.Version));
link.NoticeHolds.Should().Equal([false], "the Agent the App held for the gate is let go");
link.Retries.Should().Be(1, "and started now rather than at the next round");
vm.IsAgentStep.Should().BeTrue();
}
[Fact]
public async Task TheMainLinesGateHasNoTickAndTouchesNoHold()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var link = new FakeAgentLink();
using var vm = new FirstRunViewModel(new LegalGate(new SqliteLegalAcceptanceStore(s.Db), Docs), link, readOnly: false);
vm.AlphaTickRequired.Should().BeFalse();
vm.AcceptCommand.CanExecute(null).Should().BeTrue();
await vm.AcceptCommand.ExecuteAsync(null);
link.NoticeHolds.Should().BeEmpty();
link.Retries.Should().Be(0, "a beta's Accept is what it always was");
}
[Fact]
public async Task ReadOnlyModeAsksForNoTickEvenOnAnAlpha()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
using var vm = new FirstRunViewModel(new LegalGate(new SqliteLegalAcceptanceStore(s.Db), AlphaDocs), new FakeAgentLink(), readOnly: true);
vm.AlphaTickRequired.Should().BeFalse("Settings ▸ Reopen shows the documents and records nothing");
vm.AcceptCommand.CanExecute(null).Should().BeTrue();
}
[Fact]
public async Task RequiredOnAnEmptyLedgerAndSatisfiedByOneAccept()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var store = new SqliteLegalAcceptanceStore(s.Db);
var gate = new LegalGate(store, Docs);
(await gate.OutstandingAsync(Ct)).Select(static d => d.Key).Should().Equal("eula", "disclaimer", "privacy");
using var vm = new FirstRunViewModel(gate, new FakeAgentLink(), readOnly: false);
vm.IsLegalStep.Should().BeTrue();
vm.SelectedDocument!.Key.Should().Be("eula");
await vm.AcceptCommand.ExecuteAsync(null);
vm.IsAgentStep.Should().BeTrue("Accept moves to the Agent step");
(await gate.IsRequiredAsync(Ct)).Should().BeFalse();
IReadOnlyList<LegalAcceptance> rows = await store.ListAsync(Ct);
rows.Select(static r => (r.Document, r.Version)).Should().BeEquivalentTo([("eula", "1.0"), ("disclaimer", "2.0"), ("privacy", "2.0")], "the GPL needs no acceptance, so it gets no row");
vm.NextCommand.Execute(null);
vm.IsExplainerStep.Should().BeTrue();
vm.NextCommand.Execute(null);
vm.IsImportStep.Should().BeTrue();
vm.NextCommand.Execute(null);
vm.IsImportStep.Should().BeTrue("the last step stays");
vm.BackCommand.Execute(null);
vm.IsExplainerStep.Should().BeTrue();
vm.FinishCommand.Execute(null);
Task<bool> outcome = vm.Outcome;
(await outcome).Should().BeTrue();
}
/// <summary>
/// beta.12 (D45): a ledger that accepted four documents under an earlier version keeps its <c>gpl</c> row, unread; one
/// that never had it is not held open for it.
/// </summary>
[Fact]
public async Task TheGplNeitherHoldsTheGateOpenNorIsRecordedAndAnOldRowIsLeftAlone()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var store = new SqliteLegalAcceptanceStore(s.Db);
await store.RecordAsync(new LegalAcceptance("eula", "1.0", DateTimeOffset.UnixEpoch), Ct);
await store.RecordAsync(new LegalAcceptance("disclaimer", "2.0", DateTimeOffset.UnixEpoch), Ct);
await store.RecordAsync(new LegalAcceptance("privacy", "2.0", DateTimeOffset.UnixEpoch), Ct);
var gate = new LegalGate(store, Docs);
(await gate.IsRequiredAsync(Ct)).Should().BeFalse("no gpl row is needed");
await store.RecordAsync(new LegalAcceptance("gpl", "GPL-3.0-only", DateTimeOffset.UnixEpoch), Ct);
await gate.AcceptAllAsync(Ct);
(await store.ListAsync(Ct)).Single(static r => string.Equals(r.Document, "gpl", StringComparison.Ordinal)).AcceptedAt
.Should().Be(DateTimeOffset.UnixEpoch, "an earlier version's gpl row stays as it was written");
}
[Fact]
public async Task AMovedVersionMakesOnlyThatDocumentOutstanding()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var store = new SqliteLegalAcceptanceStore(s.Db);
await new LegalGate(store, Docs).AcceptAllAsync(Ct);
IReadOnlyList<LegalDocument> bumped = [.. Docs.Select(static d => string.Equals(d.Key, "disclaimer", StringComparison.Ordinal) ? d with { Version = "2.1" } : d)];
var gate = new LegalGate(store, bumped);
(await gate.OutstandingAsync(Ct)).Should().ContainSingle().Which.Key.Should().Be("disclaimer");
(await gate.IsRequiredAsync(Ct)).Should().BeTrue("FR-11: re-shown when a document version increments");
await gate.AcceptAllAsync(Ct);
(await store.FindAsync("disclaimer", Ct))!.Version.Should().Be("2.1");
(await gate.IsRequiredAsync(Ct)).Should().BeFalse();
}
[Fact]
public async Task DeclineWritesNothingAndEndsTheFlow()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var store = new SqliteLegalAcceptanceStore(s.Db);
using var vm = new FirstRunViewModel(new LegalGate(store, Docs), new FakeAgentLink(), readOnly: false);
vm.DeclineCommand.Execute(null);
Task<bool> outcome = vm.Outcome;
(await outcome).Should().BeFalse();
(await store.ListAsync(Ct)).Should().BeEmpty();
}
[Fact]
public async Task ClosingTheWindowWithoutADecisionIsADecline()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
using var vm = new FirstRunViewModel(new LegalGate(new SqliteLegalAcceptanceStore(s.Db), Docs), new FakeAgentLink(), readOnly: false);
vm.Abandon();
Task<bool> outcome = vm.Outcome;
(await outcome).Should().BeFalse();
}
[Fact]
public async Task ReadOnlyModeRecordsNothing()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var store = new SqliteLegalAcceptanceStore(s.Db);
using var vm = new FirstRunViewModel(new LegalGate(store, Docs), new FakeAgentLink(), readOnly: true);
await vm.AcceptCommand.ExecuteAsync(null);
vm.CloseCommand.Execute(null);
Task<bool> outcome = vm.Outcome;
(await outcome).Should().BeTrue();
(await store.ListAsync(Ct)).Should().BeEmpty("Settings ▸ Reopen shows, it does not re-record");
vm.IsLegalStep.Should().BeTrue();
}
/// <summary>beta.12: View on GitHub goes through the App's opener (it started a process itself), at the document's own URL.</summary>
[Fact]
public async Task ViewOnGitHubOpensTheSelectedDocumentThroughTheAppsOpener()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var urls = new NoUrlOpener();
using var vm = new FirstRunViewModel(new LegalGate(new SqliteLegalAcceptanceStore(s.Db), Docs), new FakeAgentLink(), readOnly: true, urls);
vm.SelectedDocument = vm.Documents[2];
vm.OpenOnlineCommand.Execute(null);
urls.Opened.Should().Equal(new Uri("https://github.com/x/d"));
vm.Urls.Should().BeSameAs(urls, "the rendered documents' links leave through the same opener");
}
[Fact]
public async Task TheAgentStepShowsTheHelloAcksFactsAndNAWithoutOne()
{
await using ScratchLedger s = await ScratchLedger.OpenAsync();
var link = new FakeAgentLink { Hello = null, IsConnected = false, State = AgentConnectionState.Starting };
using var vm = new FirstRunViewModel(new LegalGate(new SqliteLegalAcceptanceStore(s.Db), Docs), link, readOnly: false);
vm.AgentVersion.Should().Be(Strings.Common_NotAvailable);
vm.CpuTemperature.Should().Be(Strings.Common_NotAvailable);
link.Hello = new Shared.Ipc.HelloAck("1.2.3", Shared.Ipc.IpcProtocol.Version, 4, false, "b", false, "l2", true, "consent-dialog/1");
link.State = AgentConnectionState.Connected;
link.RaiseChanged();
vm.AgentVersion.Should().Be("1.2.3");
vm.TelemetrySource.Should().Be("l2");
vm.CpuTemperature.Should().Be(Strings.Common_Yes);
vm.Elevated.Should().Be(Strings.Common_No);
vm.AgentState.Should().Be(AgentStatusPresentation.Pill(AgentConnectionState.Connected).Text);
}
}