asset workspace progress
This commit is contained in:
+231
@@ -0,0 +1,231 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.OutputFormatCatalog;
|
||||
import p.packer.assets.AssetReference;
|
||||
import p.packer.assets.PackerBuildParticipation;
|
||||
import p.packer.assets.PackerAssetState;
|
||||
import p.packer.events.PackerEvent;
|
||||
import p.packer.events.PackerEventKind;
|
||||
import p.packer.messages.CreateAssetRequest;
|
||||
import p.packer.messages.CreateAssetResult;
|
||||
import p.packer.messages.GetAssetDetailsRequest;
|
||||
import p.packer.messages.ListAssetsRequest;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class FileSystemPackerWorkspaceServiceTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void listsRegisteredAndUnregisteredAssetsFromWorkspaceScan() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-mixed", tempDir.resolve("mixed"));
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final var result = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertEquals(2, result.assets().size());
|
||||
assertTrue(result.assets().stream().anyMatch(asset -> asset.state() == PackerAssetState.REGISTERED));
|
||||
assertTrue(result.assets().stream().anyMatch(asset -> asset.state() == PackerAssetState.UNREGISTERED));
|
||||
assertTrue(result.assets().stream().allMatch(asset -> asset.identity().assetUuid() != null && !asset.identity().assetUuid().isBlank()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void surfacesMissingRegisteredRootAsStructuralDiagnostic() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-missing-root", tempDir.resolve("missing-root"));
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final var result = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(PackerOperationStatus.PARTIAL, result.status());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("missing asset.json")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void includesInvalidDeclarationsInSnapshotWithDiagnostics() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-invalid", tempDir.resolve("invalid"));
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final var result = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertEquals(1, result.assets().size());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.assets().getFirst().state());
|
||||
assertEquals(PackerBuildParticipation.EXCLUDED, result.assets().getFirst().buildParticipation());
|
||||
assertTrue(result.assets().getFirst().hasDiagnostics());
|
||||
}
|
||||
|
||||
@Test
|
||||
void emitsDiscoveryAndDiagnosticsEventsDuringScan() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/read-invalid", tempDir.resolve("events"));
|
||||
final List<PackerEvent> events = new CopyOnWriteArrayList<>();
|
||||
final FileSystemPackerWorkspaceService service = service(events::add);
|
||||
|
||||
service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.ASSET_DISCOVERED));
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.DIAGNOSTICS_UPDATED));
|
||||
assertTrue(events.stream().allMatch(event -> event.sequence() >= 0L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsRegisteredAssetAndWritesManifest() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("created");
|
||||
final List<PackerEvent> events = new CopyOnWriteArrayList<>();
|
||||
final FileSystemPackerWorkspaceService service = service(events::add);
|
||||
|
||||
final var result = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/new-atlas",
|
||||
"new_atlas",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertNotNull(result.assetReference());
|
||||
assertTrue(Files.isRegularFile(projectRoot.resolve("assets/ui/new-atlas/asset.json")));
|
||||
final String manifestJson = Files.readString(projectRoot.resolve("assets/ui/new-atlas/asset.json"));
|
||||
assertTrue(manifestJson.contains("\"asset_uuid\""));
|
||||
|
||||
final var snapshot = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
assertEquals(1, snapshot.assets().size());
|
||||
assertEquals(PackerAssetState.REGISTERED, snapshot.assets().getFirst().state());
|
||||
assertEquals("new_atlas", snapshot.assets().getFirst().identity().assetName());
|
||||
assertNotNull(snapshot.assets().getFirst().identity().assetUuid());
|
||||
assertEquals(PackerBuildParticipation.INCLUDED, snapshot.assets().getFirst().buildParticipation());
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.ACTION_APPLIED));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsCreatedAssetThroughRuntimeBackedDetailsWithoutRescanMismatch() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("created-details");
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
|
||||
final CreateAssetResult createResult = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/new-atlas",
|
||||
"new_atlas",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, createResult.status());
|
||||
final AssetReference assetReference = createResult.assetReference();
|
||||
assertNotNull(assetReference);
|
||||
|
||||
final var detailsResult = service.getAssetDetails(new GetAssetDetailsRequest(
|
||||
project(projectRoot),
|
||||
assetReference));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, detailsResult.status());
|
||||
assertEquals(PackerAssetState.REGISTERED, detailsResult.details().summary().state());
|
||||
assertEquals("new_atlas", detailsResult.details().summary().identity().assetName());
|
||||
assertNotNull(detailsResult.details().summary().identity().assetUuid());
|
||||
assertTrue(detailsResult.diagnostics().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnsupportedFormatForSelectedFamily() {
|
||||
final Path projectRoot = tempDir.resolve("unsupported");
|
||||
final List<PackerEvent> events = new CopyOnWriteArrayList<>();
|
||||
final FileSystemPackerWorkspaceService service = service(events::add);
|
||||
|
||||
final var result = service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"audio/bad",
|
||||
"bad_asset",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.SOUND_BANK_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
false));
|
||||
|
||||
assertEquals(PackerOperationStatus.FAILED, result.status());
|
||||
assertNull(result.assetReference());
|
||||
assertTrue(result.summary().contains("not supported"));
|
||||
assertTrue(events.stream().anyMatch(event -> event.kind() == PackerEventKind.ACTION_FAILED));
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesConcurrentCreateAssetRequestsPerProject() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("concurrent-create");
|
||||
final FileSystemPackerWorkspaceService service = service();
|
||||
try (var executor = Executors.newFixedThreadPool(2)) {
|
||||
final Future<CreateAssetResult> first = executor.submit(() -> service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/atlas-a",
|
||||
"atlas_a",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
true)));
|
||||
final Future<CreateAssetResult> second = executor.submit(() -> service.createAsset(new CreateAssetRequest(
|
||||
project(projectRoot),
|
||||
"ui/atlas-b",
|
||||
"atlas_b",
|
||||
AssetFamilyCatalog.IMAGE_BANK,
|
||||
OutputFormatCatalog.TILES_INDEXED_V1,
|
||||
OutputCodecCatalog.NONE,
|
||||
false)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, first.get().status());
|
||||
assertEquals(PackerOperationStatus.SUCCESS, second.get().status());
|
||||
}
|
||||
|
||||
final var snapshot = service.listAssets(new ListAssetsRequest(project(projectRoot)));
|
||||
assertEquals(2, snapshot.assets().size());
|
||||
assertEquals(2, new HashSet<>(snapshot.assets().stream().map(asset -> asset.identity().assetId()).toList()).size());
|
||||
}
|
||||
|
||||
private PackerProjectContext project(Path root) {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
|
||||
private FileSystemPackerWorkspaceService service() {
|
||||
return service(ignored -> {
|
||||
});
|
||||
}
|
||||
|
||||
private FileSystemPackerWorkspaceService service(p.packer.events.PackerEventSink eventSink) {
|
||||
final var foundation = new p.packer.services.PackerWorkspaceFoundation();
|
||||
final var parser = new p.packer.services.PackerAssetDeclarationParser();
|
||||
final var runtimeRegistry = new p.packer.services.PackerRuntimeRegistry(new p.packer.services.PackerRuntimeLoader(foundation, parser));
|
||||
final var detailsService = new p.packer.services.PackerAssetDetailsService(runtimeRegistry);
|
||||
final var writeCoordinator = new p.packer.services.PackerProjectWriteCoordinator();
|
||||
return new FileSystemPackerWorkspaceService(foundation, detailsService, runtimeRegistry, writeCoordinator, eventSink);
|
||||
}
|
||||
|
||||
private Path copyFixture(String relativePath, Path targetRoot) throws Exception {
|
||||
final Path sourceRoot = PackerFixtureLocator.fixtureRoot(relativePath);
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
for (Path source : stream.sorted(Comparator.naturalOrder()).toList()) {
|
||||
final Path target = targetRoot.resolve(sourceRoot.relativize(source).toString());
|
||||
if (Files.isDirectory(source)) {
|
||||
Files.createDirectories(target);
|
||||
} else {
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetRoot;
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.assets.AssetFamilyCatalog;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.diagnostics.PackerDiagnosticCategory;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class PackerAssetDeclarationParserTest {
|
||||
private final PackerAssetDeclarationParser parser = new PackerAssetDeclarationParser();
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void parsesValidDeclarationFixture() {
|
||||
final var result = parser.parse(PackerFixtureLocator.fixtureRoot("workspaces/managed-basic/assets/ui/atlas/asset.json"));
|
||||
|
||||
assertTrue(result.valid());
|
||||
assertNotNull(result.declaration());
|
||||
assertEquals(1, result.declaration().schemaVersion());
|
||||
assertEquals("fixture-uuid-1", result.declaration().assetUuid());
|
||||
assertEquals("ui_atlas", result.declaration().name());
|
||||
assertEquals(AssetFamilyCatalog.IMAGE_BANK, result.declaration().assetFamily());
|
||||
assertEquals("TILES/indexed_v1", result.declaration().outputFormat());
|
||||
assertEquals(OutputCodecCatalog.NONE, result.declaration().outputCodec());
|
||||
assertTrue(result.declaration().preloadEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMalformedJsonWithStructuralDiagnostic() {
|
||||
final var result = parser.parse(PackerFixtureLocator.fixtureRoot("workspaces/invalid-malformed/assets/bad/asset.json"));
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertNull(result.declaration());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.category() == PackerDiagnosticCategory.STRUCTURAL));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingRequiredFields() {
|
||||
final var result = parser.parse(PackerFixtureLocator.fixtureRoot("workspaces/invalid-missing-fields/assets/bad/asset.json"));
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("name")));
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("asset_uuid")));
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("format")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnsupportedSchemaVersion() {
|
||||
final var result = parser.parse(PackerFixtureLocator.fixtureRoot("workspaces/invalid-version/assets/bad/asset.json"));
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.category() == PackerDiagnosticCategory.VERSIONING));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUntrustedInputPaths() throws Exception {
|
||||
final Path manifest = tempDir.resolve("asset.json");
|
||||
Files.writeString(manifest, """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "uuid-outside",
|
||||
"name": "bad_asset",
|
||||
"type": "image_bank",
|
||||
"inputs": { "sprites": ["../outside.png"] },
|
||||
"output": { "format": "TILES/indexed_v1", "codec": "NONE" },
|
||||
"preload": { "enabled": true }
|
||||
}
|
||||
""");
|
||||
|
||||
final var result = parser.parse(manifest);
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("untrusted path")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnknownAssetFamily() throws Exception {
|
||||
final Path manifest = tempDir.resolve("asset.json");
|
||||
Files.writeString(manifest, """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "uuid-video",
|
||||
"name": "bad_asset",
|
||||
"type": "video_bank",
|
||||
"inputs": { "sprites": ["atlas.png"] },
|
||||
"output": { "format": "TILES/indexed_v1", "codec": "NONE" },
|
||||
"preload": { "enabled": true }
|
||||
}
|
||||
""");
|
||||
|
||||
final var result = parser.parse(manifest);
|
||||
|
||||
assertFalse(result.valid());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("Field 'type' must be one of")));
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerOperationStatus;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.assets.AssetReference;
|
||||
import p.packer.assets.PackerBuildParticipation;
|
||||
import p.packer.assets.OutputCodecCatalog;
|
||||
import p.packer.assets.PackerAssetState;
|
||||
import p.packer.messages.GetAssetDetailsRequest;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class PackerAssetDetailsServiceTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void returnsRegisteredDetailsForRegisteredAssetReferenceById() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("managed"));
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forAssetId(1)));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertEquals(PackerAssetState.REGISTERED, result.details().summary().state());
|
||||
assertEquals(PackerBuildParticipation.INCLUDED, result.details().summary().buildParticipation());
|
||||
assertEquals("fixture-uuid-1", result.details().summary().identity().assetUuid());
|
||||
assertEquals("ui_atlas", result.details().summary().identity().assetName());
|
||||
assertEquals("TILES/indexed_v1", result.details().outputFormat());
|
||||
assertEquals(List.of(OutputCodecCatalog.NONE), result.details().availableOutputCodecs());
|
||||
assertEquals(List.of(), result.details().codecConfigurationFieldsByCodec().get(OutputCodecCatalog.NONE));
|
||||
assertTrue(result.diagnostics().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsUnregisteredDetailsForValidUnregisteredRootReference() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/orphan-valid", tempDir.resolve("orphan"));
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forRelativeAssetRoot("orphans/ui_sounds")));
|
||||
|
||||
assertEquals(PackerOperationStatus.SUCCESS, result.status());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.details().summary().state());
|
||||
assertEquals(PackerBuildParticipation.EXCLUDED, result.details().summary().buildParticipation());
|
||||
assertEquals("orphan-uuid-1", result.details().summary().identity().assetUuid());
|
||||
assertEquals("ui_sounds", result.details().summary().identity().assetName());
|
||||
assertEquals(List.of(OutputCodecCatalog.NONE), result.details().availableOutputCodecs());
|
||||
assertEquals(List.of(), result.details().codecConfigurationFieldsByCodec().get(OutputCodecCatalog.NONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsInvalidDetailsForInvalidDeclaration() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/invalid-missing-fields", tempDir.resolve("invalid"));
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(projectRoot), AssetReference.forRelativeAssetRoot("bad")));
|
||||
|
||||
assertEquals(PackerOperationStatus.FAILED, result.status());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.details().summary().state());
|
||||
assertEquals(PackerBuildParticipation.EXCLUDED, result.details().summary().buildParticipation());
|
||||
assertFalse(result.diagnostics().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void returnsFailureWhenReferenceCannotBeResolved() {
|
||||
final PackerAssetDetailsService service = service();
|
||||
|
||||
final var result = service.getAssetDetails(new GetAssetDetailsRequest(project(tempDir.resolve("empty")), AssetReference.forRelativeAssetRoot("missing/root")));
|
||||
|
||||
assertEquals(PackerOperationStatus.FAILED, result.status());
|
||||
assertEquals(PackerAssetState.UNREGISTERED, result.details().summary().state());
|
||||
assertEquals(PackerBuildParticipation.EXCLUDED, result.details().summary().buildParticipation());
|
||||
assertTrue(result.diagnostics().stream().anyMatch(diagnostic -> diagnostic.message().contains("could not be resolved")));
|
||||
}
|
||||
|
||||
private PackerProjectContext project(Path root) {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
|
||||
private PackerAssetDetailsService service() {
|
||||
final var foundation = new p.packer.services.PackerWorkspaceFoundation();
|
||||
final var parser = new PackerAssetDeclarationParser();
|
||||
return new PackerAssetDetailsService(new PackerRuntimeRegistry(new PackerRuntimeLoader(foundation, parser)));
|
||||
}
|
||||
|
||||
private Path copyFixture(String relativePath, Path targetRoot) throws Exception {
|
||||
final Path sourceRoot = PackerFixtureLocator.fixtureRoot(relativePath);
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
for (Path source : stream.sorted(Comparator.naturalOrder()).toList()) {
|
||||
final Path target = targetRoot.resolve(sourceRoot.relativize(source).toString());
|
||||
if (Files.isDirectory(source)) {
|
||||
Files.createDirectories(target);
|
||||
} else {
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetRoot;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerProjectContext;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
final class PackerProjectWriteCoordinatorTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private final PackerProjectWriteCoordinator coordinator = new PackerProjectWriteCoordinator();
|
||||
|
||||
@AfterEach
|
||||
void cleanup() {
|
||||
coordinator.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void serializesSameProjectWritesInSubmissionOrder() throws Exception {
|
||||
final PackerProjectContext project = new PackerProjectContext("main", tempDir.resolve("main"));
|
||||
final List<String> trace = new CopyOnWriteArrayList<>();
|
||||
final CountDownLatch firstStarted = new CountDownLatch(1);
|
||||
|
||||
final Future<String> first = coordinator.submit(project, () -> {
|
||||
trace.add("first-start");
|
||||
firstStarted.countDown();
|
||||
Thread.sleep(100L);
|
||||
trace.add("first-end");
|
||||
return "first";
|
||||
});
|
||||
|
||||
assertTrue(firstStarted.await(2, TimeUnit.SECONDS));
|
||||
final Future<String> second = coordinator.submit(project, () -> {
|
||||
trace.add("second");
|
||||
return "second";
|
||||
});
|
||||
|
||||
assertEquals("first", first.get(2, TimeUnit.SECONDS));
|
||||
assertEquals("second", second.get(2, TimeUnit.SECONDS));
|
||||
assertEquals(List.of("first-start", "first-end", "second"), trace);
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.testing.PackerFixtureLocator;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class PackerRuntimeRegistryTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void bootstrapsOneRuntimePerProjectAndReusesItUntilRefresh() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("managed"));
|
||||
final PackerRuntimeRegistry registry = runtimeRegistry();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
|
||||
final PackerProjectRuntime first = registry.getOrLoad(project);
|
||||
final PackerProjectRuntime second = registry.getOrLoad(project);
|
||||
|
||||
assertSame(first, second);
|
||||
assertEquals(1L, first.snapshot().generation());
|
||||
assertEquals(1, first.snapshot().registry().assets().size());
|
||||
assertEquals(1, first.snapshot().assets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshRebuildsSnapshotWithoutReplacingRuntimeInstance() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("refresh"));
|
||||
final PackerRuntimeRegistry registry = runtimeRegistry();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
final PackerProjectRuntime runtime = registry.getOrLoad(project);
|
||||
|
||||
Files.createDirectories(projectRoot.resolve("assets/audio/ui_sounds"));
|
||||
Files.writeString(projectRoot.resolve("assets/audio/ui_sounds/asset.json"), """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "runtime-refresh-uuid",
|
||||
"name": "ui_sounds",
|
||||
"type": "audio.bank",
|
||||
"inputs": {},
|
||||
"output": {
|
||||
"format": "SOUND/bank_v1",
|
||||
"codec": "none"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": false
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
final PackerProjectRuntime refreshed = registry.refresh(project);
|
||||
|
||||
assertSame(runtime, refreshed);
|
||||
assertTrue(refreshed.snapshot().generation() > 1L);
|
||||
assertEquals(2, refreshed.snapshot().assets().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void disposeMarksRuntimeInactiveAndRemovesItFromRegistry() throws Exception {
|
||||
final Path projectRoot = copyFixture("workspaces/managed-basic", tempDir.resolve("dispose"));
|
||||
final PackerRuntimeRegistry registry = runtimeRegistry();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
final PackerProjectRuntime runtime = registry.getOrLoad(project);
|
||||
|
||||
registry.dispose(project);
|
||||
|
||||
assertTrue(runtime.disposed());
|
||||
assertTrue(registry.find(project).isEmpty());
|
||||
assertThrows(IllegalStateException.class, runtime::snapshot);
|
||||
}
|
||||
|
||||
private PackerRuntimeRegistry runtimeRegistry() {
|
||||
final var foundation = new PackerWorkspaceFoundation();
|
||||
final var parser = new PackerAssetDeclarationParser();
|
||||
return new PackerRuntimeRegistry(new PackerRuntimeLoader(foundation, parser));
|
||||
}
|
||||
|
||||
private PackerProjectContext project(Path root) {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
|
||||
private Path copyFixture(String relativePath, Path targetRoot) throws Exception {
|
||||
final Path sourceRoot = PackerFixtureLocator.fixtureRoot(relativePath);
|
||||
try (var stream = Files.walk(sourceRoot)) {
|
||||
for (Path source : stream.sorted(Comparator.naturalOrder()).toList()) {
|
||||
final Path target = targetRoot.resolve(sourceRoot.relativize(source).toString());
|
||||
if (Files.isDirectory(source)) {
|
||||
Files.createDirectories(target);
|
||||
} else {
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target);
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetRoot;
|
||||
}
|
||||
}
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
package p.packer.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.packer.PackerProjectContext;
|
||||
import p.packer.messages.InitWorkspaceRequest;
|
||||
import p.packer.models.PackerRegistryEntry;
|
||||
import p.packer.models.PackerRegistryState;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
final class PackerWorkspaceFoundationTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void initWorkspaceCreatesAssetsControlStructureAndRegistry() throws Exception {
|
||||
final PackerWorkspaceFoundation foundation = new PackerWorkspaceFoundation();
|
||||
final PackerProjectContext project = project(tempDir.resolve("main"));
|
||||
|
||||
final var result = foundation.initWorkspace(new InitWorkspaceRequest(project));
|
||||
|
||||
assertEquals(Path.of("assets/.prometeu/index.json"), project.rootPath().relativize(result.registryPath()));
|
||||
assertTrue(Files.isDirectory(project.rootPath().resolve("assets")));
|
||||
assertTrue(Files.isDirectory(project.rootPath().resolve("assets/.prometeu")));
|
||||
assertTrue(Files.isRegularFile(project.rootPath().resolve("assets/.prometeu/index.json")));
|
||||
final String registryJson = Files.readString(project.rootPath().resolve("assets/.prometeu/index.json"));
|
||||
assertTrue(registryJson.contains("\"schema_version\""));
|
||||
assertTrue(registryJson.contains("\"next_asset_id\""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void registryRoundTripPreservesAllocatorAndEntries() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
final PackerWorkspaceFoundation foundation = new PackerWorkspaceFoundation();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
foundation.initWorkspace(new InitWorkspaceRequest(project));
|
||||
|
||||
final PackerRegistryState state = new PackerRegistryState(
|
||||
1,
|
||||
3,
|
||||
java.util.List.of(
|
||||
new PackerRegistryEntry(1, "uuid-1", "ui/atlas"),
|
||||
new PackerRegistryEntry(2, "uuid-2", "audio/ui_sounds")));
|
||||
foundation.saveRegistry(project, state);
|
||||
|
||||
final PackerRegistryState reloaded = foundation.loadRegistry(project);
|
||||
|
||||
assertEquals(1, reloaded.schemaVersion());
|
||||
assertEquals(3, reloaded.nextAssetId());
|
||||
assertEquals(2, reloaded.assets().size());
|
||||
assertEquals("audio/ui_sounds", reloaded.assets().get(1).root());
|
||||
}
|
||||
|
||||
@Test
|
||||
void allocatorIsMonotonicAndPersistedAcrossSaveLoad() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
final PackerWorkspaceFoundation foundation = new PackerWorkspaceFoundation();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
foundation.initWorkspace(new InitWorkspaceRequest(project));
|
||||
Files.createDirectories(projectRoot.resolve("assets/ui/atlas"));
|
||||
|
||||
final PackerRegistryEntry entry = foundation.allocateIdentity(project, foundation.loadRegistry(project), projectRoot.resolve("assets/ui/atlas"));
|
||||
final PackerRegistryState updated = foundation.appendAllocatedEntry(foundation.loadRegistry(project), entry);
|
||||
foundation.saveRegistry(project, updated);
|
||||
|
||||
final PackerRegistryState reloaded = foundation.loadRegistry(project);
|
||||
|
||||
assertEquals(2, reloaded.nextAssetId());
|
||||
assertEquals(1, reloaded.assets().size());
|
||||
assertEquals(1, reloaded.assets().getFirst().assetId());
|
||||
assertEquals("ui/atlas", reloaded.assets().getFirst().root());
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateRootsFailClearly() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
Files.createDirectories(projectRoot.resolve("assets/.prometeu"));
|
||||
Files.writeString(projectRoot.resolve("assets/.prometeu/index.json"), """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"next_asset_id": 3,
|
||||
"assets": [
|
||||
{ "asset_id": 1, "asset_uuid": "uuid-1", "root": "ui/atlas" },
|
||||
{ "asset_id": 2, "asset_uuid": "uuid-2", "root": "ui/atlas" }
|
||||
]
|
||||
}
|
||||
""");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("Duplicate asset root"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedRegistryFailsClearly() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
Files.createDirectories(projectRoot.resolve("assets/.prometeu"));
|
||||
Files.writeString(projectRoot.resolve("assets/.prometeu/index.json"), "{ nope ");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("Unable to load registry"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsupportedRegistrySchemaFailsClearly() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
Files.createDirectories(projectRoot.resolve("assets/.prometeu"));
|
||||
Files.writeString(projectRoot.resolve("assets/.prometeu/index.json"), """
|
||||
{
|
||||
"schema_version": 99,
|
||||
"next_asset_id": 1,
|
||||
"assets": []
|
||||
}
|
||||
""");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("Unsupported registry schema_version"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void untrustedRegistryRootFailsClearly() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
Files.createDirectories(projectRoot.resolve("assets/.prometeu"));
|
||||
Files.writeString(projectRoot.resolve("assets/.prometeu/index.json"), """
|
||||
{
|
||||
"schema_version": 1,
|
||||
"next_asset_id": 2,
|
||||
"assets": [
|
||||
{ "asset_id": 1, "asset_uuid": "uuid-1", "root": "../escape" }
|
||||
]
|
||||
}
|
||||
""");
|
||||
final FileSystemPackerRegistryRepository repository = new FileSystemPackerRegistryRepository();
|
||||
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> repository.load(project(projectRoot)));
|
||||
|
||||
assertTrue(exception.getMessage().contains("trusted assets boundary"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void lookupResolvesByIdUuidAndRootAndFailsOnMissingRoot() throws Exception {
|
||||
final Path projectRoot = tempDir.resolve("main");
|
||||
final PackerWorkspaceFoundation foundation = new PackerWorkspaceFoundation();
|
||||
final PackerProjectContext project = project(projectRoot);
|
||||
foundation.initWorkspace(new InitWorkspaceRequest(project));
|
||||
Files.createDirectories(projectRoot.resolve("assets/ui/atlas"));
|
||||
final PackerRegistryState state = new PackerRegistryState(
|
||||
1,
|
||||
2,
|
||||
java.util.List.of(new PackerRegistryEntry(1, "uuid-1", "ui/atlas")));
|
||||
|
||||
final PackerRegistryLookup lookup = foundation.lookup();
|
||||
|
||||
assertTrue(lookup.findByAssetId(state, 1).isPresent());
|
||||
assertTrue(lookup.findByAssetUuid(state, "uuid-1").isPresent());
|
||||
assertTrue(lookup.findByRoot(project, state, projectRoot.resolve("assets/ui/atlas")).isPresent());
|
||||
assertEquals(projectRoot.resolve("assets/ui/atlas").toAbsolutePath().normalize(), lookup.resolveExistingRoot(project, state.assets().getFirst()));
|
||||
|
||||
Files.delete(projectRoot.resolve("assets/ui/atlas"));
|
||||
final p.packer.exceptions.PackerRegistryException exception = assertThrows(p.packer.exceptions.PackerRegistryException.class, () -> lookup.resolveExistingRoot(project, state.assets().getFirst()));
|
||||
assertTrue(exception.getMessage().contains("does not exist"));
|
||||
}
|
||||
|
||||
private PackerProjectContext project(Path root) {
|
||||
return new PackerProjectContext("main", root);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package p.packer.testing;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
final class PackerFixtureCatalogTest {
|
||||
@Test
|
||||
void exposesWorkspaceFixturesForFuturePackerTests() {
|
||||
final Path emptyWorkspace = PackerFixtureLocator.fixtureRoot("workspaces/empty");
|
||||
final Path managedWorkspace = PackerFixtureLocator.fixtureRoot("workspaces/managed-basic");
|
||||
|
||||
assertTrue(Files.isDirectory(emptyWorkspace));
|
||||
assertTrue(Files.isDirectory(managedWorkspace));
|
||||
assertTrue(Files.isRegularFile(managedWorkspace.resolve("assets/.prometeu/index.json")));
|
||||
assertTrue(Files.isRegularFile(managedWorkspace.resolve("assets/ui/atlas/asset.json")));
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package p.packer.testing;
|
||||
|
||||
import java.net.URL;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class PackerFixtureLocator {
|
||||
private PackerFixtureLocator() {
|
||||
}
|
||||
|
||||
public static Path fixtureRoot(String relativePath) {
|
||||
final String normalized = Objects.requireNonNull(relativePath, "relativePath").replace('\\', '/');
|
||||
final URL resource = PackerFixtureLocator.class.getClassLoader().getResource("fixtures/" + normalized);
|
||||
if (resource == null) {
|
||||
throw new IllegalArgumentException("fixture not found: " + normalized);
|
||||
}
|
||||
try {
|
||||
return Path.of(resource.toURI()).toAbsolutePath().normalize();
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("unable to resolve fixture: " + normalized, exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
{ nope
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"type": "image_bank",
|
||||
"inputs": "wrong",
|
||||
"output": {
|
||||
"codec": ""
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"schema_version": 9,
|
||||
"asset_uuid": "future-uuid-1",
|
||||
"name": "future_asset",
|
||||
"type": "image_bank",
|
||||
"inputs": {
|
||||
"sprites": ["future.png"]
|
||||
},
|
||||
"output": {
|
||||
"format": "TILES/indexed_v1",
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"next_asset_id": 2,
|
||||
"assets": [
|
||||
{
|
||||
"asset_id": 1,
|
||||
"asset_uuid": "fixture-uuid-1",
|
||||
"root": "ui/atlas"
|
||||
}
|
||||
]
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "fixture-uuid-1",
|
||||
"name": "ui_atlas",
|
||||
"type": "image_bank",
|
||||
"inputs": {
|
||||
"sprites": ["sprites/confirm.png"]
|
||||
},
|
||||
"output": {
|
||||
"format": "TILES/indexed_v1",
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "orphan-uuid-1",
|
||||
"name": "ui_sounds",
|
||||
"type": "sound_bank",
|
||||
"inputs": {
|
||||
"sources": ["confirm.wav"]
|
||||
},
|
||||
"output": {
|
||||
"format": "SOUND/bank_v1",
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": false
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"type": "image_bank",
|
||||
"inputs": "wrong",
|
||||
"output": {
|
||||
"codec": ""
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"next_asset_id": 2,
|
||||
"assets": [
|
||||
{
|
||||
"asset_id": 1,
|
||||
"asset_uuid": "uuid-1",
|
||||
"root": "missing/atlas"
|
||||
}
|
||||
]
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"next_asset_id": 2,
|
||||
"assets": [
|
||||
{
|
||||
"asset_id": 1,
|
||||
"asset_uuid": "uuid-1",
|
||||
"root": "ui/atlas"
|
||||
}
|
||||
]
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "orphan-uuid-2",
|
||||
"name": "ui_sounds",
|
||||
"type": "sound_bank",
|
||||
"inputs": {
|
||||
"sources": ["confirm.wav"]
|
||||
},
|
||||
"output": {
|
||||
"format": "SOUND/bank_v1",
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": false
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"schema_version": 1,
|
||||
"asset_uuid": "uuid-1",
|
||||
"name": "ui_atlas",
|
||||
"type": "image_bank",
|
||||
"inputs": {
|
||||
"sprites": ["sprites/confirm.png"]
|
||||
},
|
||||
"output": {
|
||||
"format": "TILES/indexed_v1",
|
||||
"codec": "NONE"
|
||||
},
|
||||
"preload": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user