package PBS references and expose paginated MCP reads
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
package p.studio.app.compiler;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.api.automation.*;
|
||||
import p.studio.compiler.FrontendRegistryService;
|
||||
import p.studio.compiler.models.FrontendSpec;
|
||||
import p.studio.compiler.services.*;
|
||||
import p.studio.core.project.*;
|
||||
import p.studio.utilities.structures.ReadOnlySet;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ProjectReferencesTest {
|
||||
@TempDir Path root;
|
||||
final ObjectMapper mapper = new ObjectMapper();
|
||||
final ProjectAccessRegistry access = new ProjectAccessRegistry();
|
||||
|
||||
CompilerProjectKnowledge knowledge(String language, String sdk) throws Exception {
|
||||
writeManifest(language, sdk);
|
||||
access.register(root, () -> { throw new AssertionError("Read needs no grant"); }, () -> true);
|
||||
return new CompilerProjectKnowledge(access, new ProjectCatalogService(root, mapper, new ProjectLocalStudioSetupStore(mapper)), mapper);
|
||||
}
|
||||
|
||||
void writeManifest(String language, String sdk) throws Exception {
|
||||
mapper.writeValue(root.resolve("prometeu.json").toFile(), Map.of("name", "References", "version", "1.0.0",
|
||||
"language", language, "stdlib", sdk, "target", "Game", "dependencies", List.of()));
|
||||
}
|
||||
|
||||
@Test void pbsReferenceKindsAndVersionsMatchTheInstalledProviderAndPaginateExactly() throws Exception {
|
||||
var knowledge = knowledge("pbs", "1");
|
||||
var context = knowledge.context(root);
|
||||
var kinds = context.references().stream().map(ProjectKnowledge.Reference::kind).collect(java.util.stream.Collectors.toSet());
|
||||
assertTrue(kinds.containsAll(List.of(ProjectKnowledge.ReferenceKind.QUICKSTART, ProjectKnowledge.ReferenceKind.SYNTAX,
|
||||
ProjectKnowledge.ReferenceKind.SEMANTICS, ProjectKnowledge.ReferenceKind.EXAMPLE, ProjectKnowledge.ReferenceKind.SDK)));
|
||||
assertEquals(context.references().size(), context.languages().getFirst().referenceIds().size());
|
||||
for (var reference : context.references()) {
|
||||
assertEquals("pbs", reference.owner());
|
||||
if (reference.kind() == ProjectKnowledge.ReferenceKind.SDK || reference.kind() == ProjectKnowledge.ReferenceKind.EXAMPLE) {
|
||||
assertEquals("1", reference.version().value());
|
||||
assertTrue(reference.version().source().contains("SDK"));
|
||||
} else assertFalse(reference.version().known());
|
||||
}
|
||||
var sdk = "pbs:sdk:game:1:sdk/gfx/main.pbs";
|
||||
var expected = FrontendRegistryService.require("pbs").documentation().orElseThrow().references(1, p.studio.AppMode.Game)
|
||||
.stream().filter(ref -> ref.id().equals(sdk)).findFirst().orElseThrow().text();
|
||||
var actual = new StringBuilder();
|
||||
int offset = 0;
|
||||
do {
|
||||
var page = knowledge.readReference(root, sdk, offset, 97);
|
||||
assertEquals(expected.codePointCount(0, expected.length()), page.total());
|
||||
actual.append(page.text());
|
||||
if (page.nextOffset() == null) break;
|
||||
offset = page.nextOffset();
|
||||
} while (true);
|
||||
assertEquals(expected, actual.toString());
|
||||
assertTrue(actual.toString().contains("fn fill_rect"));
|
||||
}
|
||||
|
||||
@Test void unknownAndTraversalLikeIdsNeverReadFilesOrUrls() throws Exception {
|
||||
var knowledge = knowledge("pbs", "1");
|
||||
Path secret = root.resolve("secret.txt");
|
||||
Files.writeString(secret, "not a reference");
|
||||
for (String id : List.of(secret.toString(), secret.toUri().toString(), "../secret.txt", "pbs:../secret.txt",
|
||||
"pbs-knowledge/index.json", "pbs:%2e%2e/secret.txt", "https://example.invalid", "pbs:unknown")) {
|
||||
assertEquals(AutomationFailure.Code.REFERENCE_NOT_FOUND,
|
||||
assertThrows(AutomationException.class, () -> knowledge.readReference(root, id, 0, 100)).failure().code());
|
||||
}
|
||||
}
|
||||
|
||||
@Test void changedSdkCannotReadPreviouslyAdvertisedSdkOrExampleIds() throws Exception {
|
||||
var knowledge = knowledge("pbs", "1");
|
||||
assertTrue(knowledge.context(root).languages().getFirst().referenceIds().contains("pbs:sdk:game:1:sdk/gfx/main.pbs"));
|
||||
writeManifest("pbs", "999");
|
||||
var context = knowledge.context(root);
|
||||
assertTrue(context.references().stream().noneMatch(ref -> ref.kind() == ProjectKnowledge.ReferenceKind.SDK || ref.kind() == ProjectKnowledge.ReferenceKind.EXAMPLE));
|
||||
assertTrue(context.limitations().stream().anyMatch(value -> value.contains("no installed SDK reference")));
|
||||
assertEquals(AutomationFailure.Code.REFERENCE_NOT_FOUND, assertThrows(AutomationException.class,
|
||||
() -> knowledge.readReference(root, "pbs:sdk:game:1:sdk/gfx/main.pbs", 0, 100)).failure().code());
|
||||
assertFalse(knowledge.readReference(root, "pbs:quickstart", 0, 100).text().isEmpty());
|
||||
}
|
||||
|
||||
@Test void paginationCountsUnicodeCodePointsAndAcceptsTheExactEnd() throws Exception {
|
||||
String id = "reference-test";
|
||||
register(id, Optional.of((sdk, target) -> List.of(new FrontendReferenceProvider.Reference(
|
||||
id + ":text", "Unicode text", FrontendReferenceProvider.Kind.QUICKSTART, null, "unknown", "A😀é\nB"))));
|
||||
try {
|
||||
var knowledge = knowledge(id, "1");
|
||||
var emoji = knowledge.readReference(root, id + ":text", 1, 1);
|
||||
assertEquals("😀", emoji.text());
|
||||
assertEquals(5, emoji.total());
|
||||
assertEquals(2, emoji.nextOffset());
|
||||
var end = knowledge.readReference(root, id + ":text", 5, 100);
|
||||
assertEquals("", end.text()); assertNull(end.nextOffset());
|
||||
for (int offset : List.of(-1, 6, Integer.MAX_VALUE))
|
||||
assertThrows(IllegalArgumentException.class, () -> knowledge.readReference(root, id + ":text", offset, 1));
|
||||
for (int limit : List.of(0, -1, 16001))
|
||||
assertThrows(IllegalArgumentException.class, () -> knowledge.readReference(root, id + ":text", 0, limit));
|
||||
} finally { FrontendRegistryService.unregister(id); }
|
||||
}
|
||||
|
||||
@Test void missingDocumentationAndUnsupportedLanguageHaveExplicitFailures() throws Exception {
|
||||
String id = "reference-test";
|
||||
register(id, Optional.empty());
|
||||
try {
|
||||
var knowledge = knowledge(id, "1");
|
||||
assertTrue(knowledge.context(root).references().isEmpty());
|
||||
assertEquals(AutomationFailure.Code.DOCUMENTATION_UNAVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> knowledge.readReference(root, id + ":text", 0, 100)).failure().code());
|
||||
writeManifest("uninstalled", "1");
|
||||
assertEquals(AutomationFailure.Code.UNSUPPORTED_LANGUAGE, assertThrows(AutomationException.class,
|
||||
() -> knowledge.readReference(root, id + ":text", 0, 100)).failure().code());
|
||||
} finally { FrontendRegistryService.unregister(id); }
|
||||
}
|
||||
|
||||
@Test void failingProviderDoesNotAdvertiseUnreadableReferences() throws Exception {
|
||||
String id = "reference-test";
|
||||
register(id, Optional.of((sdk, target) -> { throw new IllegalStateException("Missing installed index"); }));
|
||||
try {
|
||||
var knowledge = knowledge(id, "1");
|
||||
var context = knowledge.context(root);
|
||||
assertTrue(context.references().isEmpty());
|
||||
assertTrue(context.limitations().stream().anyMatch(value -> value.contains("cannot load installed references")));
|
||||
assertEquals(AutomationFailure.Code.DOCUMENTATION_UNAVAILABLE, assertThrows(AutomationException.class,
|
||||
() -> knowledge.readReference(root, id + ":text", 0, 100)).failure().code());
|
||||
} finally { FrontendRegistryService.unregister(id); }
|
||||
}
|
||||
|
||||
private void register(String id, Optional<FrontendReferenceProvider> documentation) {
|
||||
FrontendRegistryService.register(new FrontendProvider() {
|
||||
@Override public FrontendSpec specification() {
|
||||
return FrontendSpec.builder().languageId(id).allowedExtensions(ReadOnlySet.from("alt"))
|
||||
.sourceRoots(ReadOnlySet.from("src")).stdlibVersions(List.of(FrontendSpec.Stdlib.of(1))).build();
|
||||
}
|
||||
@Override public FrontendPhaseService compiler() { throw new AssertionError("No compilation for reference reads"); }
|
||||
@Override public Optional<FrontendReferenceProvider> documentation() { return documentation; }
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user