|
| 1 | +using MetaObjects.Library; |
| 2 | +using MetaObjects.Loader; |
| 3 | +using MetaObjects.Meta; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace MetaObjects.Conformance.Tests; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// The C# port can load the MetaObjects-shipped library packages, so |
| 10 | +/// <c>extends: "metaobjects::ai::LlmCallBase"</c> resolves — port parity with TypeScript, |
| 11 | +/// Python and Java (#332 / #333). |
| 12 | +/// |
| 13 | +/// <para>The NEGATIVE arm is the half that proves the opt-in is doing the work: without it the |
| 14 | +/// same model must still fail. A positive-only test keeps passing if the library is quietly |
| 15 | +/// made unconditional, which would put its top-level nodes into the model — and the generated |
| 16 | +/// output, and the docs — of every project that never asked for one.</para> |
| 17 | +/// </summary> |
| 18 | +public class LibraryLoadTests |
| 19 | +{ |
| 20 | + private const string Model = """ |
| 21 | + { |
| 22 | + "metadata.root": { |
| 23 | + "package": "acme::trace", |
| 24 | + "children": [ |
| 25 | + { "object.entity": { |
| 26 | + "name": "AgentCall", |
| 27 | + "extends": "metaobjects::ai::LlmCallBase", |
| 28 | + "children": [ |
| 29 | + { "source.rdb": { "@table": "agent_call" } }, |
| 30 | + { "identity.primary": { "name": "pk", "@fields": ["spanId"] } } |
| 31 | + ] |
| 32 | + } } |
| 33 | + ] |
| 34 | + } |
| 35 | + } |
| 36 | + """; |
| 37 | + |
| 38 | + private static string WriteModel() |
| 39 | + { |
| 40 | + var dir = Path.Combine(Path.GetTempPath(), "mo-library-load-" + Guid.NewGuid().ToString("N")); |
| 41 | + Directory.CreateDirectory(dir); |
| 42 | + File.WriteAllText(Path.Combine(dir, "trace.json"), Model); |
| 43 | + return dir; |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void LibrariesOptInMakesTheShippedBaseResolvable() |
| 48 | + { |
| 49 | + var dir = WriteModel(); |
| 50 | + try |
| 51 | + { |
| 52 | + var result = MetaDataLoader.FromDirectory(dir, new[] { "ai" }); |
| 53 | + |
| 54 | + Assert.Empty(result.Errors); |
| 55 | + // The library's own nodes are in the model... |
| 56 | + Assert.NotNull(result.Root.Children() |
| 57 | + .FirstOrDefault(c => c.ResolutionKey() == "metaobjects::ai::LlmCallBase")); |
| 58 | + // ...and the project's entity resolves its super against them, inheriting the fields. |
| 59 | + var agentCall = result.Root.Children() |
| 60 | + .FirstOrDefault(c => c.ResolutionKey() == "acme::trace::AgentCall"); |
| 61 | + Assert.NotNull(agentCall); |
| 62 | + // ADR-0039: the RESOLVING accessor — traceId is inherited, never declared here. |
| 63 | + Assert.Contains(agentCall!.Children(), c => c.Name == "traceId"); |
| 64 | + } |
| 65 | + finally |
| 66 | + { |
| 67 | + Directory.Delete(dir, recursive: true); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void WithoutTheOptInTheSameModelDoesNotResolve() |
| 73 | + { |
| 74 | + var dir = WriteModel(); |
| 75 | + try |
| 76 | + { |
| 77 | + var result = MetaDataLoader.FromDirectory(dir); |
| 78 | + |
| 79 | + Assert.Null(result.Root.Children() |
| 80 | + .FirstOrDefault(c => c.ResolutionKey() == "metaobjects::ai::LlmCallBase")); |
| 81 | + // Named, not merely non-empty: a bare "it failed" assertion would pass if the model |
| 82 | + // failed for any unrelated reason, which would prove nothing about whether the |
| 83 | + // opt-in is what supplies the base. |
| 84 | + Assert.Contains(result.Errors, e => e.Message.Contains("LlmCallBase")); |
| 85 | + } |
| 86 | + finally |
| 87 | + { |
| 88 | + Directory.Delete(dir, recursive: true); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + public void AnUnknownPackageContributesNoSourcesAndIsNotAnError() |
| 94 | + { |
| 95 | + // The cross-port contract for the PROGRAMMATIC door. A caller asking for a package this |
| 96 | + // version does not ship must still be able to load its own metadata; the callers that |
| 97 | + // read the name from a human validate first. |
| 98 | + Assert.Empty(LibrarySources.Resolve(new[] { "nosuchpackage" })); |
| 99 | + Assert.Empty(LibrarySources.Resolve(null)); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void KnownPackagesNamesWhatThisBuildShips() |
| 104 | + { |
| 105 | + var known = LibrarySources.KnownPackages(); |
| 106 | + Assert.Contains("ai", known); |
| 107 | + Assert.Equal(known.OrderBy(k => k, StringComparer.Ordinal).ToList(), known); |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// The freshness gate: the embed must equal the canonical tree byte for byte. |
| 112 | + /// |
| 113 | + /// <para>Skipped per-ref when the repo-root <c>library/</c> tree is unreachable — the |
| 114 | + /// published-package case, where there is nothing to compare against. In a checkout, which |
| 115 | + /// is where this test actually runs, the comparison is live.</para> |
| 116 | + /// </summary> |
| 117 | + [Fact] |
| 118 | + public void TheEmbedIsByteIdenticalToTheCanonicalTree() |
| 119 | + { |
| 120 | + foreach (var (reference, embedded) in EmbeddedLibrary.Content) |
| 121 | + { |
| 122 | + var onDisk = LibrarySources.OnDiskContent(reference); |
| 123 | + if (onDisk is null) continue; // not a checkout — nothing to compare |
| 124 | + Assert.True(onDisk == embedded, |
| 125 | + $"EmbeddedLibrary is stale for ref \"{reference}\" — " |
| 126 | + + "run: bun run scripts/generate-embedded-library.ts"); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments