102 lines
3.2 KiB
Java
102 lines
3.2 KiB
Java
package games.dmg.spigotquestboard;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
import java.io.IOException;
|
|
import java.time.Clock;
|
|
import java.time.Instant;
|
|
import java.time.ZoneOffset;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import org.bukkit.entity.Player;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
final class QuestBoardDialogUiTest {
|
|
private static final Instant NOW = Instant.parse("2026-09-05T03:00:00Z");
|
|
|
|
@Test
|
|
void dialogSubmissionUsesEquivalentCreationFlow() {
|
|
RecordingCreator creator = new RecordingCreator(false);
|
|
QuestBoardDialogUi ui = new QuestBoardDialogUi(
|
|
creator, now -> List.of(), Clock.fixed(NOW, ZoneOffset.UTC)
|
|
);
|
|
Player player = mock(Player.class);
|
|
|
|
ui.submit(player, "stone", "64");
|
|
|
|
assertEquals("stone", creator.material);
|
|
assertEquals(64, creator.quantity);
|
|
assertEquals(NOW, creator.createdAt);
|
|
verify(player).sendMessage(
|
|
"Quest 00000000-0000-0000-0000-000000000010 created. "
|
|
+ "Your exact held stack is now escrowed."
|
|
);
|
|
}
|
|
|
|
@Test
|
|
void everyOpeningBuildsAListingFromTheCurrentActiveQuests() {
|
|
RecordingCreator creator = new RecordingCreator(false);
|
|
Quest active = creator.quest(NOW);
|
|
QuestBoardDialogUi ui = new QuestBoardDialogUi(
|
|
creator, now -> List.of(active), Clock.fixed(NOW, ZoneOffset.UTC)
|
|
);
|
|
|
|
assertEquals(QuestListingFormatter.format(active, NOW), ui.listingText());
|
|
}
|
|
|
|
@Test
|
|
void persistenceFailureExplainsThatRewardWasRestored() {
|
|
QuestBoardDialogUi ui = new QuestBoardDialogUi(
|
|
new RecordingCreator(true), now -> List.of(), Clock.fixed(NOW, ZoneOffset.UTC)
|
|
);
|
|
Player player = mock(Player.class);
|
|
|
|
ui.submit(player, "stone", "64");
|
|
|
|
verify(player).sendMessage(
|
|
"The quest could not be saved. Your held reward was restored."
|
|
);
|
|
}
|
|
|
|
private static final class RecordingCreator implements QuestCreationGateway {
|
|
private final boolean fail;
|
|
private String material;
|
|
private int quantity;
|
|
private Instant createdAt;
|
|
|
|
private RecordingCreator(boolean fail) {
|
|
this.fail = fail;
|
|
}
|
|
|
|
@Override
|
|
public Quest create(
|
|
Player player, String requestedMaterial, int requestedAmount, Instant instant
|
|
) throws IOException {
|
|
if (fail) {
|
|
throw new IOException("disk full");
|
|
}
|
|
material = requestedMaterial;
|
|
quantity = requestedAmount;
|
|
createdAt = instant;
|
|
return quest(instant);
|
|
}
|
|
|
|
private Quest quest(Instant instant) {
|
|
return new Quest(
|
|
UUID.fromString("00000000-0000-0000-0000-000000000010"),
|
|
UUID.fromString("00000000-0000-0000-0000-000000000001"),
|
|
"Issuer", "STONE", 64,
|
|
List.of(new EscrowItem("DIAMOND", 1, null)),
|
|
instant, instant.plusSeconds(604800)
|
|
);
|
|
}
|
|
|
|
@Override
|
|
public List<String> suggestBlockMaterials(String prefix) {
|
|
return List.of();
|
|
}
|
|
}
|
|
}
|