package PBS references and expose paginated MCP reads

This commit is contained in:
2026-09-29 08:48:33 +01:00
parent 58f11a8123
commit feb57c5298
23 changed files with 738 additions and 21 deletions
@@ -0,0 +1,60 @@
package p.studio.compiler.pbs.documentation;
import org.junit.jupiter.api.Test;
import p.studio.AppMode;
import p.studio.compiler.services.FrontendReferenceProvider;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static org.junit.jupiter.api.Assertions.*;
class PbsDocumentationTest {
@Test void installedIndexHasAllKindsUniqueIdsAndExactSdkSources() throws Exception {
var references = new PbsDocumentation().references(1, AppMode.Game);
assertEquals(references.size(), references.stream().map(FrontendReferenceProvider.Reference::id).distinct().count());
for (var kind : List.of(FrontendReferenceProvider.Kind.QUICKSTART, FrontendReferenceProvider.Kind.SYNTAX,
FrontendReferenceProvider.Kind.SEMANTICS, FrontendReferenceProvider.Kind.EXAMPLE, FrontendReferenceProvider.Kind.SDK))
assertTrue(references.stream().anyMatch(ref -> ref.kind() == kind));
for (var reference : references) {
assertFalse(reference.text().isBlank());
if (reference.kind() != FrontendReferenceProvider.Kind.SDK) continue;
try (var stream = getClass().getClassLoader().getResourceAsStream("game/stdlib/1/" + reference.id().substring("pbs:sdk:game:1:".length()))) {
assertNotNull(stream);
assertEquals(new String(stream.readAllBytes(), StandardCharsets.UTF_8), reference.text());
assertEquals("1", reference.version());
}
}
}
@Test void unsupportedSdkOrTargetDoesNotBorrowAnotherSdkOrExample() {
var docs = new PbsDocumentation();
for (var selection : List.of(docs.references(999, AppMode.Game), docs.references(1, AppMode.Shell))) {
assertFalse(selection.isEmpty());
assertTrue(selection.stream().noneMatch(ref -> ref.kind() == FrontendReferenceProvider.Kind.SDK || ref.kind() == FrontendReferenceProvider.Kind.EXAMPLE));
assertTrue(selection.stream().allMatch(ref -> ref.version() == null && ref.versionSource().equals("unknown")));
}
}
@Test void missingIndexAndCorruptedContentFailExplicitly() {
assertThrows(IllegalStateException.class, () -> new PbsDocumentation(path -> null).references(1, AppMode.Game));
var corrupt = new PbsDocumentation(path -> path.equals("pbs-knowledge/index.json")
? getClass().getClassLoader().getResourceAsStream(path)
: new ByteArrayInputStream("corrupted resource".getBytes(StandardCharsets.UTF_8)));
assertThrows(IllegalStateException.class, () -> corrupt.references(1, AppMode.Game));
}
@Test void invalidIndexResourceCannotReachTheResourceLoader() {
var opened = new ArrayList<String>();
String invalidIndex = """
{"schemaVersion":1,"owner":"pbs","references":[
{"id":"pbs:bad","title":"Bad","kind":"SYNTAX","resource":"../private.txt","sha256":"invalid"}
]}
""";
var docs = new PbsDocumentation(path -> {
opened.add(path);
return new ByteArrayInputStream(invalidIndex.getBytes(StandardCharsets.UTF_8));
});
assertThrows(IllegalStateException.class, () -> docs.references(1, AppMode.Game));
assertEquals(List.of("pbs-knowledge/index.json"), opened);
}
}