|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | +# (PMPL-1.0-or-later preferred; MPL-2.0 required for Hex.pm) |
| 3 | +# |
| 4 | +# a2ml_aspect_test.exs — Aspect tests for A2ML parser/renderer. |
| 5 | +# |
| 6 | +# Tests cross-cutting concerns: security (input safety), correctness, |
| 7 | +# performance, and resilience. These complement the unit and contract tests |
| 8 | +# by validating behavioural aspects that cut across the whole API surface. |
| 9 | + |
| 10 | +defmodule A2MLAspectTest do |
| 11 | + use ExUnit.Case, async: true |
| 12 | + |
| 13 | + # --------------------------------------------------------------------------- |
| 14 | + # Aspect: Security — empty and nil-like inputs are handled gracefully |
| 15 | + # --------------------------------------------------------------------------- |
| 16 | + |
| 17 | + test "ASPECT security: empty string is handled gracefully without raise" do |
| 18 | + result = A2ML.parse("") |
| 19 | + assert match?({:error, _}, result) |
| 20 | + end |
| 21 | + |
| 22 | + test "ASPECT security: whitespace-only input handled gracefully" do |
| 23 | + result = A2ML.parse(" \n \t ") |
| 24 | + assert match?({:error, _}, result) |
| 25 | + end |
| 26 | + |
| 27 | + test "ASPECT security: nil input returns error tuple, does not raise" do |
| 28 | + result = |
| 29 | + try do |
| 30 | + A2ML.parse(nil) |
| 31 | + rescue |
| 32 | + _ -> {:error, :bad_argument} |
| 33 | + end |
| 34 | + |
| 35 | + assert match?({:error, _}, result) |
| 36 | + end |
| 37 | + |
| 38 | + # --------------------------------------------------------------------------- |
| 39 | + # Aspect: Security — very long strings do not crash the parser |
| 40 | + # --------------------------------------------------------------------------- |
| 41 | + |
| 42 | + test "ASPECT security: 1000-character string does not crash parser" do |
| 43 | + long_string = String.duplicate("x", 1000) |
| 44 | + result = A2ML.parse(long_string) |
| 45 | + # A2ML may parse long text as a paragraph — either ok or error, not raise. |
| 46 | + assert match?({:ok, _}, result) or match?({:error, _}, result) |
| 47 | + end |
| 48 | + |
| 49 | + test "ASPECT security: heading with 200-character title is safe" do |
| 50 | + long_title = String.duplicate("a", 200) |
| 51 | + input = "# #{long_title}\n\n@version 1.0" |
| 52 | + result = A2ML.parse(input) |
| 53 | + assert match?({:ok, _}, result) or match?({:error, _}, result) |
| 54 | + end |
| 55 | + |
| 56 | + # --------------------------------------------------------------------------- |
| 57 | + # Aspect: Correctness — attestation fields survive parse/render roundtrip |
| 58 | + # --------------------------------------------------------------------------- |
| 59 | + |
| 60 | + test "ASPECT correctness: attestation fields survive roundtrip" do |
| 61 | + input = |
| 62 | + "# Attestation Roundtrip\n\n!attest\n identity: Jonathan D.A. Jewell\n role: author\n trust-level: verified\n timestamp: 2026-04-04T00:00:00Z" |
| 63 | + |
| 64 | + assert {:ok, doc1} = A2ML.parse(input) |
| 65 | + assert [attest] = doc1.attestations |
| 66 | + assert attest.identity == "Jonathan D.A. Jewell" |
| 67 | + assert attest.trust_level == :verified |
| 68 | + |
| 69 | + rendered = A2ML.render(doc1) |
| 70 | + assert {:ok, doc2} = A2ML.parse(rendered) |
| 71 | + assert [attest2] = doc2.attestations |
| 72 | + assert attest2.identity == attest.identity |
| 73 | + assert attest2.trust_level == attest.trust_level |
| 74 | + end |
| 75 | + |
| 76 | + test "ASPECT correctness: multiple directives survive roundtrip" do |
| 77 | + input = "# Multi-Directive\n\n@version 1.5\n\n@author Alice\n\n@license MPL-2.0" |
| 78 | + assert {:ok, doc1} = A2ML.parse(input) |
| 79 | + assert length(doc1.directives) == 3 |
| 80 | + |
| 81 | + rendered = A2ML.render(doc1) |
| 82 | + assert {:ok, doc2} = A2ML.parse(rendered) |
| 83 | + assert length(doc2.directives) == 3 |
| 84 | + end |
| 85 | + |
| 86 | + test "ASPECT correctness: document title survives parse/render/parse" do |
| 87 | + input = "# My Document\n\nParagraph content." |
| 88 | + assert {:ok, doc1} = A2ML.parse(input) |
| 89 | + assert doc1.title == "My Document" |
| 90 | + |
| 91 | + rendered = A2ML.render(doc1) |
| 92 | + assert {:ok, doc2} = A2ML.parse(rendered) |
| 93 | + assert doc2.title == "My Document" |
| 94 | + end |
| 95 | + |
| 96 | + # --------------------------------------------------------------------------- |
| 97 | + # Aspect: Performance — parsing 100 identical inputs completes without error |
| 98 | + # --------------------------------------------------------------------------- |
| 99 | + |
| 100 | + test "ASPECT performance: parse 100 identical inputs without error" do |
| 101 | + input = "# Performance Test\n\n@version 1.0\n\nA performance paragraph." |
| 102 | + |
| 103 | + results = Enum.map(1..100, fn _ -> A2ML.parse(input) end) |
| 104 | + |
| 105 | + Enum.each(results, fn result -> |
| 106 | + assert match?({:ok, _}, result), |
| 107 | + "Expected {:ok, _} but got #{inspect(result)}" |
| 108 | + end) |
| 109 | + end |
| 110 | + |
| 111 | + test "ASPECT performance: render 100 identical documents without error" do |
| 112 | + input = "# Render Performance\n\n@version 2.0\n\nRender test." |
| 113 | + assert {:ok, doc} = A2ML.parse(input) |
| 114 | + |
| 115 | + outputs = Enum.map(1..100, fn _ -> A2ML.render(doc) end) |
| 116 | + |
| 117 | + Enum.each(outputs, fn out -> |
| 118 | + assert is_binary(out) |
| 119 | + end) |
| 120 | + end |
| 121 | + |
| 122 | + # --------------------------------------------------------------------------- |
| 123 | + # Aspect: Resilience — unusual but syntactically possible inputs |
| 124 | + # --------------------------------------------------------------------------- |
| 125 | + |
| 126 | + test "ASPECT resilience: document with only a directive is handled" do |
| 127 | + input = "@version 1.0" |
| 128 | + result = A2ML.parse(input) |
| 129 | + assert match?({:ok, _}, result) or match?({:error, _}, result) |
| 130 | + end |
| 131 | + |
| 132 | + test "ASPECT resilience: document with only an attestation block is handled" do |
| 133 | + input = "!attest\n identity: Bot\n role: scanner\n trust-level: automated" |
| 134 | + result = A2ML.parse(input) |
| 135 | + assert match?({:ok, _}, result) or match?({:error, _}, result) |
| 136 | + end |
| 137 | +end |
0 commit comments