plugins/DuneMenus/src/test/java/com/dunerealms/menus/SuppliedDocumentTest.java

57 lines
5.1 KiB
Java

package com.dunerealms.menus;
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class SuppliedDocumentTest {
@Test void actionsKeepTheirOriginalOpaqueSlotsWithoutAnExtraConfirmation(){
String html=SuppliedDocument.html("hub","Dune Realms","",List.of(new Card(10,"Travel",List.of("Imperial passage"),"compass"),new Card(51,"Collect",List.of(),"chest")),List.of());
assertTrue(html.contains("data-slot='10'"));assertTrue(html.contains("data-slot='51'"));assertFalse(html.contains("data-pick"));assertFalse(html.contains("Confirm"));
}
@Test void playerControlledTextCannotInjectActionsOrMarkup(){
String html=SuppliedDocument.html("exchange","<script>alert(1)</script>","quoted",List.of(new Card(12,"<button data-slot='999'>",List.of("A & B"),"paper")),List.of("<img src=x>"));
assertFalse(html.contains("<script>"));assertFalse(html.contains("<button data-slot='999'>"));assertTrue(html.contains("&lt;img src=x&gt;"));assertTrue(html.contains("A &amp; B"));
}
@Test void pagingControlsDoNotOverlapTheBackAction(){
String html=SuppliedDocument.html("exchange","Market","",List.of(new Card(45,"Previous",List.of(),"arrow"),new Card(46,"Back",List.of(),"arrow"),new Card(53,"Next",List.of(),"arrow")),List.of());
assertTrue(html.contains("class='pager n0' data-slot='45'"));
assertTrue(html.contains("class='back n1' data-slot='46'"));
assertTrue(html.contains("class='pager n2' data-slot='53'"));
}
@Test void legacyHitboxAliasesBecomeOneCardWithoutDroppingDistinctActions(){
Runnable same=()->{};Runnable different=()->System.nanoTime();
var unique=ServiceMenu.uniqueActions(java.util.Map.of(18,same,19,same,20,same,22,different));
assertEquals(java.util.Set.of(18,22),unique.keySet());assertSame(same,unique.get(18));assertSame(different,unique.get(22));
}
@Test void eachPrimaryLayoutHasItsOwnPresentation(){
for(String id:List.of("hub","travel","exchange","clan","company","missions","spice","war","politics","house","equipment","bank","atm")){
String html=SuppliedDocument.html(id,id,"",List.of(),List.of());assertTrue(html.contains("frame "+id));assertTrue(html.contains(SuppliedDocument.accent(id)));
}
assertTrue(SuppliedDocument.html("travel","Passage","",List.of(),List.of()).contains("main class='constellation'"));
assertTrue(SuppliedDocument.html("missions","Journal","",List.of(),List.of()).contains("main class='journal'"));
}
@Test void legacyFormattingIsRemovedWithoutLosingItemNamesOrMarks(){
assertEquals("Paul's chopper",SuppliedDocument.clean("\uE400§6Paul's chopper\uE401"));
assertEquals("LEGENDARY | MK XIV",SuppliedDocument.clean("§x§f§f§0§0§a§aLEGENDARY | MK XIV"));
}
@Test void longListingLoreIsBoundedInCardsAndRetainedInDetails(){
var lore=List.of("LEGENDARY | MK XIV","Used by Paul himself.","Efficiency VIII","Fortune IV","Mending I","Unbreaking IV","Seller: JARVIS HQ","1 available · 500 DM each");
String html=SuppliedDocument.html("exchange","Buy","",List.of(new Card(13,"Paul's chopper",lore,"netherite_axe")),List.of(),java.util.Map.of(),13);
String button=html.substring(html.indexOf("data-slot='13'"),html.indexOf("</button>",html.indexOf("data-slot='13'")));
assertFalse(button.contains("Mending I"));assertTrue(html.contains("class='item-details'"));assertTrue(html.contains("Mending I"));assertTrue(html.contains("data-slot='13'"));
}
@Test void adviserLabelDoesNotInventANpcProfession(){assertEquals("Your House adviser · Stilgar",ServiceMenu.displayTitle("Your Mentat · Stilgar"));assertEquals("Exchange · Buy",ServiceMenu.displayTitle("Exchange · Buy"));}
@Test void houseArrowIsAnOrdinaryCardAndEveryWrappedLineIsBounded(){
assertEquals("card",SuppliedDocument.kind(new Card(45,"Your House",List.of(),"arrow")));
for(String line:SuppliedDocument.lines("Personal level, Reputation and active quests with a verylongunbrokenwordthatmustalsofit",24))assertTrue(line.length()<=24);
}
@Test void localArtworkPathsAreEscapedAndNeverRemoteOrTraversable(){
var card=new Card(13,"Axe",List.of(),"netherite_axe");assertTrue(SuppliedDocument.image(card,java.util.Map.of(13,"pack/assets/dunerealms/textures/item/equipment/legendary_mk14_axe.png")).contains("<img"));
assertFalse(SuppliedDocument.image(card,java.util.Map.of(13,"https://evil/image.png")).contains("<img"));assertFalse(SuppliedDocument.image(card,java.util.Map.of(13,"pack/assets/../secret.png")).contains("<img"));
}
@Test void listingPriceAndStockStayVisibleAndOnlySelectedFullLoreIsRendered(){
var lore=List.of("LEGENDARY | MK XIV","Long flavour text","Efficiency VIII","Fortune IV","Mending I","Seller: Guild","4 available · 500 DM each");var first=new Card(13,"First",lore,"netherite_axe");var second=new Card(14,"Second",lore,"netherite_axe");
assertTrue(SuppliedDocument.summary(first).subList(0,3).stream().anyMatch(x->x.contains("500 DM")));
String html=SuppliedDocument.html("exchange","Buy","",List.of(first,second),List.of(),java.util.Map.of(),14);
assertEquals(1,html.split("class='item-details'",-1).length-1);assertTrue(html.contains("data-slot='1013'"));assertTrue(html.contains("data-slot='1014'"));assertTrue(html.contains("<div class='item-details'><h2>Second</h2>"));
}
}