using System.Net.Http.Json; using ItWorx.KnowledgeHub.Contracts.Knowledge; using ItWorx.KnowledgeHub.IntegrationTests.Infrastructure; namespace ItWorx.KnowledgeHub.IntegrationTests.Knowledge; /// Templates create valid study note, runbook, ADR and troubleshooting /// documents (docs/27-acceptance-matrix.md, M2-A06). [Collection(PostgresTestGroup.Name)] public sealed class TemplateTests(PostgresContainerFixture postgres) : OwnerSetupTestBase(postgres) { [Fact] public async Task All_four_built_in_templates_are_listed() { using var client = Factory.CreateClient(); await SetupHelper.LoginAsync(client, OwnerEmail, OwnerPassword); var templates = await client.GetFromJsonAsync>("/api/v1/templates"); Assert.NotNull(templates); Assert.Equal(4, templates.Count); Assert.Contains(templates, t => t.Key == "study-note"); Assert.Contains(templates, t => t.Key == "runbook"); Assert.Contains(templates, t => t.Key == "architecture-decision"); Assert.Contains(templates, t => t.Key == "troubleshooting"); } [Theory] [InlineData("study-note", "StudyNote", "## Kernidee")] [InlineData("runbook", "Runbook", "## Rollback")] [InlineData("architecture-decision", "ArchitectureDecision", "## Beslissing")] [InlineData("troubleshooting", "Troubleshooting", "## Escalatie")] public async Task Creating_a_page_from_a_template_produces_the_expected_structure( string templateKey, string expectedDocumentType, string expectedSectionHeading) { using var client = Factory.CreateClient(); await SetupHelper.LoginAsync(client, OwnerEmail, OwnerPassword); var response = await SetupHelper.PostWithCsrfAsync( client, "/api/v1/pages", new CreatePageRequest(null, $"Sjabloontest {templateKey}", null, null, templateKey)); response.EnsureSuccessStatusCode(); var page = await response.Content.ReadFromJsonAsync(); Assert.NotNull(page); Assert.Equal(expectedDocumentType, page.DocumentType); Assert.Contains($"# Sjabloontest {templateKey}", page.Markdown, StringComparison.Ordinal); Assert.Contains(expectedSectionHeading, page.Markdown, StringComparison.Ordinal); } [Fact] public async Task An_unknown_template_key_is_rejected() { using var client = Factory.CreateClient(); await SetupHelper.LoginAsync(client, OwnerEmail, OwnerPassword); var response = await SetupHelper.PostWithCsrfAsync( client, "/api/v1/pages", new CreatePageRequest(null, "Ongeldig sjabloon", null, null, "does-not-exist")); Assert.Equal(System.Net.HttpStatusCode.UnprocessableEntity, response.StatusCode); } }