organize and clean up

This commit is contained in:
2026-03-24 13:42:14 +00:00
parent e8afc8ea0d
commit f7101fec4d
45 changed files with 410 additions and 747 deletions
@@ -1,76 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.ResolvedWorkspace;
import p.studio.compiler.workspaces.phases.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class DependencyServiceTest {
@Test
void canonical_pipeline_e2e(@TempDir Path tempDir) throws IOException {
final Path root = tempDir;
final Path depA = root.resolve("depA");
Files.createDirectories(depA);
Files.createDirectories(root.resolve("src"));
Files.createDirectories(depA.resolve("src"));
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
final var cfg = new DependencyConfig(false, root, List.of());
final var phases = List.of(
new SeedPhase(),
new DiscoverPhase(),
new MaterializePhase(),
new LocalizePhase(),
new ValidatePhase(),
new StackPhase(),
new PolicyPhase()
);
final var service = new DependencyService(phases);
final ResolvedWorkspace ws = service.run(cfg);
assertNotNull(ws);
assertNotNull(ws.graph());
assertEquals(2, ws.graph().projects().size());
// Obter ids por nome
var rootDesc = ws.graph().projects().stream().filter(p -> p.getName().equals("root-proj")).findFirst().orElseThrow();
var depDesc = ws.graph().projects().stream().filter(p -> p.getName().equals("dep-a")).findFirst().orElseThrow();
final var rootId = rootDesc.getProjectId();
final var depId = depDesc.getProjectId();
// Edges: root -> dep
assertTrue(ws.graph().edges().get(rootId.getIndex()).contains(depId));
assertTrue(ws.graph().edges().get(depId.getIndex()).isEmpty());
// Stack: de acordo com a implementação atual (arestas root->dep), root vem antes
assertEquals(rootId, ws.stack().projects().get(0));
assertEquals(depId, ws.stack().projects().get(1));
// workspace.root é o projeto root
assertEquals(rootId, ws.projectId());
}
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
final StringBuilder deps = new StringBuilder();
deps.append("[");
for (int i = 0; i < localDeps.length; i++) {
if (i > 0) deps.append(",");
deps.append("{\"canonPath\":\"").append(localDeps[i]).append("\"}");
}
deps.append("]");
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
Files.writeString(dir.resolve("prometeu.json"), json);
}
}
@@ -1,55 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.DependencyContext;
import p.studio.compiler.workspaces.phases.DiscoverPhase;
import p.studio.compiler.workspaces.phases.SeedPhase;
import p.studio.utilities.structures.ReadOnlyCollection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class DiscoverPhaseTest {
@Test
void discover_finds_root_and_local_dependency(@TempDir Path tempDir) throws IOException {
// Estrutura: root/ (prometeu.json) -> deps: ["depA/"] e cria depA/(prometeu.json)
final Path root = tempDir;
final Path depA = root.resolve("depA");
Files.createDirectories(depA);
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"}, null);
writeManifest(depA, "dep-a", "0.1.0", new String[]{}, null);
final var cfg = new DependencyConfig(false, root, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
final var issues = new DiscoverPhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "Descoberta deve ocorrer sem issues");
assertEquals(2, ctx.projectInfos.size(), "Dois projetos devem ser descobertos");
assertTrue(ctx.projectIndexByDirectory.containsKey(root.toRealPath()), "Projeto raiz indexado");
assertTrue(ctx.projectIndexByDirectory.containsKey(depA.toRealPath()), "Projeto depA indexado");
assertEquals(0L, ctx.projectIndexByDirectory.get(root.toRealPath()));
assertEquals(1L, ctx.projectIndexByDirectory.get(depA.toRealPath()));
}
private static void writeManifest(Path dir, String name, String version, String[] localDeps, String language) throws IOException {
final StringBuilder deps = new StringBuilder();
deps.append("[");
for (int i = 0; i < localDeps.length; i++) {
if (i > 0) deps.append(",");
deps.append("{\"canonPath\":\"").append(localDeps[i]).append("\"}");
}
deps.append("]");
final String langField = language == null ? "" : ",\n \"language\": \"" + language + "\"";
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + langField + "\n}";
Files.writeString(dir.resolve("prometeu.json"), json);
}
}
@@ -1,55 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.DependencyContext;
import p.studio.compiler.workspaces.phases.*;
import p.studio.utilities.structures.ReadOnlyCollection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class LocalizePhaseTest {
@Test
void localize_resolves_dependency_references(@TempDir Path tempDir) throws IOException {
final Path root = tempDir;
final Path depA = root.resolve("depA");
Files.createDirectories(depA);
Files.createDirectories(root.resolve("src"));
Files.createDirectories(depA.resolve("src"));
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
final var cfg = new DependencyConfig(false, root, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new MaterializePhase().run(ctx)));
final var issues = new LocalizePhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "Localização deve ocorrer sem issues");
final var depId = ctx.projectIdByDirectoryRoot.get(depA.toRealPath());
assertNotNull(depId);
final var rootId = ctx.root;
assertTrue(ctx.dependenciesByProject.get(rootId.getIndex()).contains(depId), "Root deve depender de depA");
}
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
final StringBuilder deps = new StringBuilder();
deps.append("[");
for (int i = 0; i < localDeps.length; i++) {
if (i > 0) deps.append(",");
deps.append("{\"canonPath\":\"").append(localDeps[i]).append("\"}");
}
deps.append("]");
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
Files.writeString(dir.resolve("prometeu.json"), json);
}
}
@@ -1,63 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.DependencyContext;
import p.studio.compiler.workspaces.phases.DiscoverPhase;
import p.studio.compiler.workspaces.phases.MaterializePhase;
import p.studio.compiler.workspaces.phases.SeedPhase;
import p.studio.utilities.structures.ReadOnlyCollection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class MaterializePhaseTest {
@Test
void materialize_builds_nodes_and_sets_root(@TempDir Path tempDir) throws IOException {
final Path root = tempDir;
final Path depA = root.resolve("depA");
Files.createDirectories(depA);
Files.createDirectories(root.resolve("src"));
Files.createDirectories(depA.resolve("src"));
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
final var cfg = new DependencyConfig(false, root, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
final var issues = new MaterializePhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "Materialização sem issues esperadas");
assertNotNull(ctx.root, "Root ProjectId deve ser definido");
assertEquals(2, ctx.projectNodes.size());
assertEquals(2, ctx.dependenciesByProject.size());
assertTrue(ctx.projectIdByDirectoryRoot.containsKey(root.toRealPath()));
assertTrue(ctx.projectIdByDirectoryRoot.containsKey(depA.toRealPath()));
final var rootNode = ctx.projectNodes.get(ctx.root.getIndex());
assertEquals("root-proj", rootNode.getName());
assertEquals("0.1.0", rootNode.getVersion());
assertEquals("pbs", rootNode.getLanguageId());
assertFalse(rootNode.getSourceRoots().isEmpty(), "Deve haver pelo menos um source root (src)");
}
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
final StringBuilder deps = new StringBuilder();
deps.append("[");
for (int i = 0; i < localDeps.length; i++) {
if (i > 0) deps.append(",");
deps.append("{\"canonPath\":\"").append(localDeps[i]).append("\"}");
}
deps.append("]");
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
Files.writeString(dir.resolve("prometeu.json"), json);
}
}
@@ -1,28 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.DependencyContext;
import p.studio.compiler.workspaces.phases.SeedPhase;
import p.studio.utilities.structures.ReadOnlyCollection;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class SeedPhaseTest {
@Test
void seed_initializes_root_and_pending(@TempDir Path tempDir) throws Exception {
final var cfg = new DependencyConfig(false, tempDir, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
final var issues = new SeedPhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "Nenhum issue esperado na SeedPhase");
assertNotNull(ctx.mainProjectRootPathCanon, "Caminho canônico do projeto raiz deve ser inicializado");
assertEquals(1, ctx.pending.size(), "Uma entrada deve ser enfileirada para descoberta inicial");
assertEquals(tempDir.toRealPath(), ctx.mainProjectRootPathCanon);
}
}
@@ -1,58 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.DependencyContext;
import p.studio.compiler.workspaces.phases.*;
import p.studio.utilities.structures.ReadOnlyCollection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class StackPhaseTest {
@Test
void stack_topologically_sorts_projects(@TempDir Path tempDir) throws IOException {
final Path root = tempDir;
final Path depA = root.resolve("depA");
Files.createDirectories(depA);
Files.createDirectories(root.resolve("src"));
Files.createDirectories(depA.resolve("src"));
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
final var cfg = new DependencyConfig(false, root, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new MaterializePhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new LocalizePhase().run(ctx)));
final var issues = new StackPhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "Ordenação sem issues");
assertNotNull(ctx.stack);
assertEquals(2, ctx.stack.projects().size());
final var depId = ctx.projectIdByDirectoryRoot.get(depA.toRealPath());
final var rootId = ctx.root;
assertEquals(rootId, ctx.stack.projects().get(0), "Root (sem entradas) vem primeiro pelo algoritmo atual");
assertEquals(depId, ctx.stack.projects().get(1));
}
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
final StringBuilder deps = new StringBuilder();
deps.append("[");
for (int i = 0; i < localDeps.length; i++) {
if (i > 0) deps.append(",");
deps.append("{\"canonPath\":\"").append(localDeps[i]).append("\"}");
}
deps.append("]");
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
Files.writeString(dir.resolve("prometeu.json"), json);
}
}
@@ -1,59 +0,0 @@
package p.studio.compiler.workspaces;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.DependencyConfig;
import p.studio.compiler.models.DependencyContext;
import p.studio.compiler.workspaces.phases.*;
import p.studio.utilities.structures.ReadOnlyCollection;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.jupiter.api.Assertions.*;
class ValidateAndPolicyPhasesTest {
@Test
void validate_checks_consistency_without_issues(@TempDir Path tempDir) throws IOException {
final Path root = tempDir;
final Path depA = root.resolve("depA");
Files.createDirectories(depA);
Files.createDirectories(root.resolve("src"));
Files.createDirectories(depA.resolve("src"));
writeManifest(root, "root-proj", "0.1.0", new String[]{"depA"});
writeManifest(depA, "dep-a", "0.1.0", new String[]{});
final var cfg = new DependencyConfig(false, root, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
assertTrue(ReadOnlyCollection.isEmpty(new SeedPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new DiscoverPhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new MaterializePhase().run(ctx)));
assertTrue(ReadOnlyCollection.isEmpty(new LocalizePhase().run(ctx)));
final var issues = new ValidatePhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "Validação não deve reportar issues no cenário feliz");
}
@Test
void policy_is_noop(@TempDir Path tempDir) {
final var cfg = new DependencyConfig(false, tempDir, java.util.List.of());
final var ctx = DependencyContext.seed(cfg);
final var issues = new PolicyPhase().run(ctx);
assertTrue(ReadOnlyCollection.isEmpty(issues), "PolicyPhase atualmente é no-op");
}
private static void writeManifest(Path dir, String name, String version, String[] localDeps) throws IOException {
final StringBuilder deps = new StringBuilder();
deps.append("[");
for (int i = 0; i < localDeps.length; i++) {
if (i > 0) deps.append(",");
deps.append("{\"canonPath\":\"").append(localDeps[i]).append("\"}");
}
deps.append("]");
final String json = "{\n \"name\": \"" + name + "\",\n \"version\": \"" + version + "\",\n \"dependencies\": " + deps + "\n}";
Files.writeString(dir.resolve("prometeu.json"), json);
}
}