Bifrost/tests/Bifrost.Web.Tests/DeskReaderViewTests.cs
Heimdall a2c41b1f34 Desk: re-vendor Huginn desk fixtures at b299d6f7 (71 files, CultLib 3a51bbab subdirectory) and CultLib vectors at d3025d3c
Pins name the commits, the exact file list and each sha256; the verifier is proved to fail on a changed byte, an
extra file and a missing file, at both directory levels. The four golden request frames still reproduce. The
synthetic card-viewers and proposed non-decider fragments are gone: both reader views are now CultLib's own entry
bytes served through the door. Huginn's record-whole still has no answerer_count (its README says so), so the
fault test stays true and is re-pointed at it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 19:14:17 +00:00

151 lines
7.4 KiB
C#

using System.Text.Json;
using System.Text.Json.Nodes;
using Bifrost.Web.Features.Desk;
using Bifrost.Web.Tests.Support;
namespace Bifrost.Web.Tests;
// CultLib d3025d3c's reader views (append-only on 3a51bbab), as Bifrost meets them: the 17-key card (raised_at, decider_count) stays opaque,
// the record is typed with answerer_count, and answerer is nil in a non-decider's view.
public sealed class DeskReaderViewTests
{
private static readonly string[] CardKeys =
[
"schemaVersion", "fork_id", "title", "context", "options", "recommendation", "depends", "leans", "flag", "raised_in",
"raised_at", "deciders", "decider_count", "status", "ruling", "ruled_from", "order", "options_hash",
];
[Fact]
public void Valid_cards_are_the_17_key_card_with_both_views_and_pass_through_unchanged()
{
var rows = DeskVectors.Rows("card", "valid").ToList();
Assert.Equal(7, rows.Count);
foreach (var row in rows)
{
var message = row["message"]!.AsObject();
Assert.Equal(CardKeys, message.Select(p => p.Key));
Assert.StartsWith("DE0012", (string)row["messagePackHex"]!, StringComparison.Ordinal);
Assert.True(message["raised_at"] is null || message["raised_at"]!.GetValueKind() == JsonValueKind.String);
var count = (int)message["decider_count"]!;
Assert.InRange(count, 1, 64);
var deciders = message["deciders"]!.AsArray();
Assert.True(deciders.Count == 0 || deciders.Count == count, (string)row["name"]!);
var card = message.Deserialize<JsonElement>(DeskJson.Options);
Assert.True(DeskVectors.SameShape(message, JsonSerializer.SerializeToNode(card, DeskJson.Options)), (string)row["name"]!);
}
Assert.Contains(rows, r => r["message"]!["raised_at"] is null);
Assert.Contains(rows, r => r["message"]!["deciders"]!.AsArray().Count == 0 && (string)r["name"]! == "withheld_deciders");
}
[Fact]
public void The_options_hash_vectors_are_the_15_and_Bifrost_computes_no_hash()
{
Assert.Equal(15, DeskVectors.Array("optionsHash").Count);
Assert.Equal(9, DeskVectors.Array("bodyDigests").Count);
var directory = new DirectoryInfo(AppContext.BaseDirectory);
while (directory is not null && !Directory.Exists(Path.Combine(directory.FullName, "src", "Bifrost.Web", "Features", "Desk")))
{
directory = directory.Parent;
}
Assert.NotNull(directory);
foreach (var file in Directory.GetFiles(Path.Combine(directory.FullName, "src", "Bifrost.Web", "Features", "Desk"), "*.cs"))
{
var text = File.ReadAllText(file);
Assert.DoesNotContain("System.Security.Cryptography", text, StringComparison.Ordinal);
Assert.DoesNotContain("SHA256", text, StringComparison.Ordinal);
}
}
[Fact]
public void Stored_answers_round_trip_and_every_variant_carries_a_nil_answerer_with_the_key_written()
{
var rows = DeskVectors.Rows("deskAnswer", "valid").ToList();
Assert.Equal(8, rows.Count);
Assert.Contains(rows, r => (string)r["name"]! == "withheld_answerer");
Assert.Contains(rows, r => (string)r["name"]! == "withheld_answerer_revision" && (string)r["message"]!["outcome"]! == "Revision");
Assert.Contains(rows, r => (string)r["name"]! == "withheld_answerer_retraction" && (string)r["message"]!["outcome"]! == "Retraction");
var outcomes = new HashSet<string>();
foreach (var row in rows)
{
var message = row["message"]!.AsObject();
Assert.StartsWith("DE0010", (string)row["messagePackHex"]!, StringComparison.Ordinal);
outcomes.Add((string)message["outcome"]!);
Assert.True(DeskVectors.SameShape(message, JsonSerializer.SerializeToNode(message.Deserialize<DeskAnswerDocument>(DeskJson.Options), DeskJson.Options)), (string)row["name"]!);
var withheld = JsonNode.Parse(message.ToJsonString())!.AsObject();
withheld["answerer"] = null;
var back = JsonSerializer.SerializeToNode(withheld.Deserialize<DeskAnswerDocument>(DeskJson.Options), DeskJson.Options)!.AsObject();
Assert.True(back.ContainsKey("answerer"));
Assert.Null(back["answerer"]);
Assert.True(DeskVectors.SameShape(withheld, back), (string)row["name"]!);
}
Assert.Superset(new HashSet<string> { "Counted", "Revision", "Retraction", "Refused" }, outcomes);
}
[Fact]
public void Entries_in_both_views_round_trip_and_the_shape_faults_Bifrost_can_see_are_refused()
{
var valid = DeskVectors.Rows("transport", "entry", "valid").ToList();
Assert.Equal(8, valid.Count);
foreach (var row in valid)
{
var message = row["message"]!;
Assert.True(DeskVectors.SameShape(message, JsonSerializer.SerializeToNode(message.Deserialize<ForkEntry>(DeskJson.Options), DeskJson.Options)), (string)row["name"]!);
}
// Missing or null answerer_count is not a count Bifrost may invent; the other invalid entries are rules of the
// contract's validators (CultLib) and Huginn's door, which Bifrost does not judge.
foreach (var name in new[] { "answerer_count_missing", "answerer_count_null" })
{
var row = DeskVectors.Rows("transport", "entry", "invalid").First(r => (string)r["name"]! == name);
Assert.ThrowsAny<JsonException>(() => row["message"].Deserialize<ForkEntry>(DeskJson.Options));
}
}
[Fact]
public void Every_scenario_record_is_the_6_key_record_and_decodes_typed()
{
var count = 0;
foreach (var scenario in DeskVectors.Rows("scenarios"))
{
var record = scenario["record"]!;
Assert.StartsWith("86", (string)scenario["recordHex"]!, StringComparison.Ordinal);
Assert.True(DeskVectors.SameShape(record, JsonSerializer.SerializeToNode(record.Deserialize<ForkRecord>(DeskJson.Options), DeskJson.Options)), (string)scenario["name"]!);
count++;
}
Assert.Equal(63, count);
}
[Fact]
public async Task The_non_decider_page_leaves_the_edge_with_notes_nil_and_ids_withheld_untouched()
{
var scenario = DeskVectors.Rows("transport", "listScenarios").First(s => (string)s["name"]! == "non_decider_page");
using var factory = new DeskWebApplicationFactory();
await factory.EnsureAccountAsync(DeskWebApplicationFactory.Member, true);
var step = scenario["steps"]![0]!;
var response = step["response"]!.AsObject();
factory.Door.ListScript = (_, _) => new DeskListOutcome.Listed(response.Deserialize<ForkList>(DeskJson.Options)!);
var request = step["request"]!;
var reply = await factory.SendAsync(
"GET", $"/api/desk/{(string)request["mind"]!}/forks?status={(string?)request["status"]}&cursor={(string?)request["cursor"]}&limit={(int)request["limit"]!}",
null, factory.Token(DeskWebApplicationFactory.Member, "bifrost-desk", "desk.read"));
Assert.Equal(200, reply.Status);
var expected = JsonNode.Parse(response.ToJsonString())!;
foreach (var fork in expected["forks"]!.AsArray())
{
foreach (var answer in fork!["record"]!["answers"]!.AsArray())
{
answer!["note"] = null;
}
}
Assert.True(DeskVectors.SameShape(JsonNode.Parse(reply.Body), expected));
}
}