112 lines
4.3 KiB
Java
112 lines
4.3 KiB
Java
package online.astradev.content.projectile;
|
|||
|
|
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||
|
|
|
||
|
|
import com.google.gson.JsonObject;
|
||
|
|
|
||
|
|
import online.astradev.plus.api.editor.EditorDefinitions;
|
||
|
|
|
||
|
|
import org.junit.jupiter.api.AfterEach;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.junit.jupiter.api.io.TempDir;
|
||
|
|
|
||
|
|
import java.nio.file.Files;
|
||
|
|
import java.nio.file.Path;
|
||
|
|
|
||
|
|
class ProjectileCapabilitiesTest {
|
||
|
|
@TempDir Path directory;
|
||
|
|
|
||
|
|
private ProjectileCapabilities runtime;
|
||
|
|
|
||
|
|
@AfterEach
|
||
|
|
void closeRuntime() {
|
||
|
|
if (runtime != null) runtime.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void seedsRegistersAndPublishesValidatedLiveFileEdits() throws Exception {
|
||
|
|
Path file = directory.resolve("config/projectiles.json");
|
||
|
|
String defaults =
|
||
|
|
"{\"rules\":[{\"selector\":{\"item\":\"minecraft:snowball\"},\"kind\":\"SNOWBALL\"}]}";
|
||
|
|
runtime =
|
||
|
|
ProjectileCapabilities.start(
|
||
|
|
file,
|
||
|
|
"test:projectiles",
|
||
|
|
"Test projectiles",
|
||
|
|
defaults,
|
||
|
|
"{\"fields\":[]}",
|
||
|
|
new JsonObject());
|
||
|
|
|
||
|
|
assertTrue(ProjectileCapabilities.active());
|
||
|
|
assertEquals(defaults, Files.readString(file));
|
||
|
|
var provider = EditorDefinitions.find("test:projectiles").orElseThrow();
|
||
|
|
assertEquals(runtime.definitionProvider(), provider);
|
||
|
|
|
||
|
|
String changed =
|
||
|
|
"{\"rules\":[{\"selector\":{\"customId\":\"test:ammo\"},\"kind\":\"ENDER_PEARL\"}]}";
|
||
|
|
provider.save(changed, provider.snapshot().revision());
|
||
|
|
assertEquals(changed, provider.snapshot().documentJson());
|
||
|
|
assertThrows(
|
||
|
|
IllegalArgumentException.class,
|
||
|
|
() -> provider.save("{\"rules\":[{}]}", provider.snapshot().revision()));
|
||
|
|
assertEquals(changed, provider.snapshot().documentJson());
|
||
|
|
|
||
|
|
runtime.close();
|
||
|
|
runtime = null;
|
||
|
|
assertFalse(ProjectileCapabilities.active());
|
||
|
|
assertTrue(EditorDefinitions.find("test:projectiles").isEmpty());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void existingAdministratorFileWinsOverPackagedDefaults() throws Exception {
|
||
|
|
Path file = directory.resolve("config/projectiles.json");
|
||
|
|
Files.createDirectories(file.getParent());
|
||
|
|
String existing = "{\"rules\":[]}";
|
||
|
|
Files.writeString(file, existing);
|
||
|
|
|
||
|
|
runtime =
|
||
|
|
ProjectileCapabilities.start(
|
||
|
|
file,
|
||
|
|
"test:existing_projectiles",
|
||
|
|
"Existing projectiles",
|
||
|
|
"{\"rules\":[{\"selector\":{\"item\":\"minecraft:tnt\"},\"kind\":\"TNT_PAYLOAD\",\"parameters\":{\"fuseTicks\":80,\"velocityScale\":1}}]}",
|
||
|
|
"{\"fields\":[]}",
|
||
|
|
new JsonObject());
|
||
|
|
|
||
|
|
assertEquals(existing, Files.readString(file));
|
||
|
|
assertEquals(existing, runtime.definitionProvider().snapshot().documentJson());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void rejectsInvalidMetadataAndPackagedDefaultsBeforeCreatingAFile() {
|
||
|
|
Path invalidId = directory.resolve("config/invalid-id.json");
|
||
|
|
assertThrows(
|
||
|
|
IllegalArgumentException.class,
|
||
|
|
() ->
|
||
|
|
ProjectileCapabilities.start(
|
||
|
|
invalidId,
|
||
|
|
"not-an-id",
|
||
|
|
"Invalid",
|
||
|
|
"{\"rules\":[]}",
|
||
|
|
"{\"fields\":[]}",
|
||
|
|
new JsonObject()));
|
||
|
|
assertFalse(Files.exists(invalidId));
|
||
|
|
|
||
|
|
Path invalidDefaults = directory.resolve("config/invalid-defaults.json");
|
||
|
|
assertThrows(
|
||
|
|
IllegalArgumentException.class,
|
||
|
|
() ->
|
||
|
|
ProjectileCapabilities.start(
|
||
|
|
invalidDefaults,
|
||
|
|
"test:invalid_defaults",
|
||
|
|
"Invalid defaults",
|
||
|
|
"{\"rules\":[{}]}",
|
||
|
|
"{\"fields\":[]}",
|
||
|
|
new JsonObject()));
|
||
|
|
assertFalse(Files.exists(invalidDefaults));
|
||
|
|
}
|
||
|
|
}
|