|
| 1 | +package com.metaobjects.generator.spring; |
| 2 | + |
| 3 | +import com.metaobjects.field.MetaField; |
| 4 | +import com.metaobjects.library.LibrarySources; |
| 5 | +import com.metaobjects.loader.InMemoryStringSource; |
| 6 | +import com.metaobjects.loader.LoaderOptions; |
| 7 | +import com.metaobjects.loader.MetaDataLoader; |
| 8 | +import com.metaobjects.object.MetaObject; |
| 9 | +import org.junit.Rule; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.rules.TemporaryFolder; |
| 12 | + |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.TreeSet; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertNotNull; |
| 24 | +import static org.junit.Assert.assertTrue; |
| 25 | + |
| 26 | +/** |
| 27 | + * #332 — the trace-helper generator runs against the SHIPPED |
| 28 | + * {@code metaobjects::ai::LlmCallBase}, not a hand-copied one. |
| 29 | + * |
| 30 | + * <p>Every other test of this generator declares its own {@code LlmCallBase} inline under a |
| 31 | + * different package. That is the bypass ADR-0024 already named — "the green tests pass only |
| 32 | + * because they bypass the shipped base with bespoke entities" — and it is precisely what let |
| 33 | + * the Java port ship this generator while having no way at all to LOAD the metadata it |
| 34 | + * exists to consume. A hand-copied base can also drift from the real one silently: the |
| 35 | + * copies stay green while the shipped file moves.</p> |
| 36 | + * |
| 37 | + * <p>This test loads the real library through the {@code libraries} opt-in and asserts both |
| 38 | + * directions of ADR-0024 FIX #1 — the fields the generator emits are exactly the shipped |
| 39 | + * base's effective fields, no more and no fewer.</p> |
| 40 | + */ |
| 41 | +public class TraceHelperOnShippedLibraryTest { |
| 42 | + |
| 43 | + /** |
| 44 | + * A concrete call entity extending the SHIPPED base — no local copy of it anywhere. |
| 45 | + * The request/response value objects and the nested {@code template.prompt} carrying |
| 46 | + * {@code @responseRef} are what the generator's applies-to predicate requires; only the |
| 47 | + * BASE is different from the other tests of this generator, which is the whole point. |
| 48 | + */ |
| 49 | + private static final String META = "{\"metadata.root\": {" |
| 50 | + + " \"package\": \"acme::app\"," |
| 51 | + + " \"children\": [" |
| 52 | + + " { \"object.value\": { \"name\": \"GreetRequest\", \"children\": [" |
| 53 | + + " { \"field.string\": { \"name\": \"name\", \"@required\": true } }" |
| 54 | + + " ]}}," |
| 55 | + + " { \"object.value\": { \"name\": \"GreetResponse\", \"children\": [" |
| 56 | + + " { \"field.string\": { \"name\": \"greeting\", \"@required\": true } }" |
| 57 | + + " ]}}," |
| 58 | + + " { \"object.entity\": {" |
| 59 | + + " \"name\": \"GreetingCall\"," |
| 60 | + + " \"extends\": \"metaobjects::ai::LlmCallBase\"," |
| 61 | + + " \"children\": [" |
| 62 | + + " { \"source.rdb\": { \"@table\": \"greeting_call\", \"@role\": \"primary\" } }," |
| 63 | + + " { \"identity.primary\": { \"name\": \"pk\", \"@fields\": [\"spanId\"] } }," |
| 64 | + + " { \"template.prompt\": { \"name\": \"greetingPrompt\"," |
| 65 | + + " \"@payloadRef\": \"acme::app::GreetRequest\"," |
| 66 | + + " \"@responseRef\": \"acme::app::GreetResponse\" } }" |
| 67 | + + " ]" |
| 68 | + + " } }" |
| 69 | + + " ]" |
| 70 | + + "}}"; |
| 71 | + |
| 72 | + @Rule |
| 73 | + public TemporaryFolder tmp = new TemporaryFolder(); |
| 74 | + |
| 75 | + private MetaDataLoader loadWithShippedLibrary(String name) { |
| 76 | + MetaDataLoader loader = new MetaDataLoader( |
| 77 | + LoaderOptions.create(false, false, true), |
| 78 | + MetaDataLoader.SUBTYPE_MANUAL, name); |
| 79 | + loader.setLibraries(Collections.singletonList("ai")); |
| 80 | + loader.init(); |
| 81 | + loader.load(List.of(new InMemoryStringSource(META, name + "/meta.json"))); |
| 82 | + return loader; |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void generatesAHelperForAnEntityExtendingTheShippedBase() throws Exception { |
| 87 | + MetaDataLoader loader = loadWithShippedLibrary("trace-shipped"); |
| 88 | + |
| 89 | + MetaObject base = loader.getMetaObjectByName("metaobjects::ai::LlmCallBase"); |
| 90 | + assertNotNull("the SHIPPED base must be in the model — that is the whole point", base); |
| 91 | + |
| 92 | + MetaObject call = loader.getMetaObjectByName("acme::app::GreetingCall"); |
| 93 | + assertNotNull("GreetingCall must load", call); |
| 94 | + |
| 95 | + Path gen = tmp.newFolder("gen").toPath(); |
| 96 | + LlmTraceHelperGenerator generator = new LlmTraceHelperGenerator(); |
| 97 | + Map<String, String> args = new HashMap<>(); |
| 98 | + args.put("outputDir", gen.toString()); |
| 99 | + generator.setArgs(args); |
| 100 | + generator.execute(loader); |
| 101 | + |
| 102 | + Path helper = gen.resolve("acme/app/GreetingCallTraceHelper.java"); |
| 103 | + assertTrue("a helper must be emitted for an entity extending the SHIPPED base, at " + helper, |
| 104 | + Files.exists(helper)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * ADR-0024 FIX #1, both directions: the recorder's row keys are exactly the shipped |
| 109 | + * base's effective fields. |
| 110 | + * |
| 111 | + * <p>One direction alone is worthless here. "Every row key is a field" passes on a |
| 112 | + * recorder that writes nothing; "every field is a row key" passes on one that writes the |
| 113 | + * whole world. Only the equality says the two agree, which is the claim — and it is the |
| 114 | + * claim a hand-copied base can never make, because it is comparing a copy against |
| 115 | + * itself.</p> |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void theShippedBaseFieldsAreExactlyWhatTheRecorderWrites() { |
| 119 | + MetaDataLoader loader = loadWithShippedLibrary("trace-shipped-fields"); |
| 120 | + MetaObject base = loader.getMetaObjectByName("metaobjects::ai::LlmCallBase"); |
| 121 | + assertNotNull(base); |
| 122 | + |
| 123 | + // ADR-0039: the RESOLVING accessor. An own-only read would drop anything the base |
| 124 | + // itself inherits, and the row keys are about the EFFECTIVE field set. |
| 125 | + List<String> baseFields = new ArrayList<>(); |
| 126 | + for (MetaField f : base.getMetaFields()) baseFields.add(f.getName()); |
| 127 | + |
| 128 | + MetaObject call = loader.getMetaObjectByName("acme::app::GreetingCall"); |
| 129 | + List<String> callFields = new ArrayList<>(); |
| 130 | + for (MetaField f : call.getMetaFields()) callFields.add(f.getName()); |
| 131 | + |
| 132 | + assertTrue("the base must declare fields at all — an empty set would make the " |
| 133 | + + "equality below vacuously true", baseFields.size() >= 10); |
| 134 | + assertTrue("every shipped base field reaches the concrete entity through extends", |
| 135 | + callFields.containsAll(baseFields)); |
| 136 | + assertEquals("the concrete entity adds no fields of its own in this fixture, so the " |
| 137 | + + "two sets must be equal — a difference means extends dropped or invented one", |
| 138 | + new TreeSet<>(baseFields), new TreeSet<>(callFields)); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + public void theLibraryIsNotLoadedUnlessAskedFor() { |
| 143 | + // The negative arm for the generator path specifically: without the opt-in there is |
| 144 | + // no base, so there is nothing for the generator to key on. A test suite that only |
| 145 | + // ever declares its own base cannot tell these two worlds apart, which is exactly |
| 146 | + // how a generator came to ship without its input. |
| 147 | + assertTrue("the ai package must be one this build ships", |
| 148 | + LibrarySources.knownPackages().contains("ai")); |
| 149 | + assertTrue("and it must contribute nothing when not requested", |
| 150 | + LibrarySources.librarySources(Collections.emptyList()).isEmpty()); |
| 151 | + } |
| 152 | +} |
0 commit comments