Runway 1.0.1
This commit is contained in:
1 parent
dcf819146b
commit
ce217fc952
259 files changed
+22226
-206
No files matched your search
@@ -0,0 +1,21 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
import mx.quest.runway.spi.service.PluginSettings;
|
||||
|
||||
/**
|
||||
* Read-only status model for the console surfaces (dashboard card + admin fragment), read by template bean name,
|
||||
* e.g. {@code ${@carouselManagerAdminModel.slideCount(tenantSiteId)}}.
|
||||
*/
|
||||
public class CarouselAdminModel {
|
||||
|
||||
private final PluginSettings settings;
|
||||
|
||||
public CarouselAdminModel(PluginSettings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
/** How many valid slides the site has configured (the same count the widget would render). */
|
||||
public int slideCount(String siteId) {
|
||||
return CarouselWidget.slides(settings.site(siteId)).size();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
import mx.quest.runway.spi.ContentWidget;
|
||||
import mx.quest.runway.spi.service.PluginSettings;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* The plugin's {@code mainClass} — the single {@code @Configuration} registered into the host context before
|
||||
* refresh. Beans are wired EXPLICITLY (no {@code @ComponentScan}). The package {@code mx.quest.carousel} lives
|
||||
* OUTSIDE {@code mx.quest.runway} so the host never double-scans it, and the plugin links nothing but the SPI.
|
||||
*
|
||||
* <p><b>Bean names start with the plugin id in lowerCamelCase</b> ({@code carouselManager…}): plugins share the
|
||||
* host's bean namespace, and the host refuses any other name. The templates read
|
||||
* the admin model by that name ({@code ${@carouselManagerAdminModel…}}).
|
||||
*
|
||||
* <p>{@link PluginSettings} is injected as a {@code @Bean} method parameter of this class, which comes from the
|
||||
* plugin's own jar — that is what binds the instance to {@code carousel-manager}'s settings; no id is passed.
|
||||
* The {@link CarouselWidget} is a list-collected {@code ContentWidget} SPI — a plain {@code @Bean}, NEVER
|
||||
* {@code @Primary}.
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class CarouselConfig {
|
||||
|
||||
@Bean
|
||||
public ContentWidget carouselManagerWidget(PluginSettings settings) {
|
||||
return new CarouselWidget(settings);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CarouselAdminModel carouselManagerAdminModel(PluginSettings settings) {
|
||||
return new CarouselAdminModel(settings);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import mx.quest.runway.spi.ContentWidget;
|
||||
import mx.quest.runway.spi.model.Capabilities;
|
||||
import mx.quest.runway.spi.model.RenderFragment;
|
||||
import mx.quest.runway.spi.model.SettingsView;
|
||||
import mx.quest.runway.spi.model.WidgetView;
|
||||
import mx.quest.runway.spi.service.PluginSettings;
|
||||
|
||||
/**
|
||||
* The {@code carousel} content widget: a site manager drops
|
||||
* {@code <div id="hero" data-widget="carousel/hero"></div>} on a page and the host asks this widget to render an
|
||||
* image carousel from the site's per-tenant configuration. It emits self-contained markup that references the
|
||||
* plugin's own stylesheet and script ({@code /__plugin/carousel-manager/static/carousel.{css,js}}) — the script
|
||||
* is idempotent, so multiple carousels on a page are fine.
|
||||
*
|
||||
* <p><b>One slide set per site:</b> this plugin declares no {@code widgetSettings:} schema, so the host's
|
||||
* per-placement config ({@code WidgetView.config()}, edited at {@code /__admin/widgets}) stays empty for it and
|
||||
* every {@code carousel/*} anchor on a site renders the SAME configured slide set. Distinct carousels per
|
||||
* placement need this plugin to declare such a schema and read {@code config()}; the {@code instanceId} is
|
||||
* already carried through for that. With no slides configured the widget renders nothing
|
||||
* ({@link RenderFragment#empty()}).
|
||||
*
|
||||
* <p><b>Settings</b> come from {@link PluginSettings#site(String)}: the descriptor's {@code siteSettings:}
|
||||
* defaults overlaid by what the site saved. The code fallbacks below equal the descriptor defaults
|
||||
* ({@code intervalSeconds: 5}, {@code showCaptions: true}, {@code showControls: true}), so the widget renders the
|
||||
* same whether or not the host applied them; {@code intervalSeconds} is clamped to the declared {@code 0..60}.
|
||||
*/
|
||||
public class CarouselWidget implements ContentWidget {
|
||||
|
||||
private static final String STATIC = "/__plugin/carousel-manager/static";
|
||||
|
||||
private final PluginSettings settings;
|
||||
|
||||
public CarouselWidget(PluginSettings settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String key() {
|
||||
return "carousel";
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderFragment render(WidgetView view) {
|
||||
SettingsView cfg = settings.site(view.site().id());
|
||||
List<Slide> slides = slides(cfg);
|
||||
if (slides.isEmpty()) {
|
||||
return RenderFragment.empty();
|
||||
}
|
||||
long interval = cfg.integer("intervalSeconds", 5, 0, 60);
|
||||
boolean captions = cfg.bool("showCaptions", true);
|
||||
boolean controls = cfg.bool("showControls", true);
|
||||
|
||||
StringBuilder b = new StringBuilder(512);
|
||||
b.append("<link rel=\"stylesheet\" href=\"").append(STATIC).append("/carousel.css\">")
|
||||
.append("<div class=\"rw-carousel\" data-interval=\"").append(interval)
|
||||
.append("\" data-controls=\"").append(controls).append("\"><div class=\"rw-carousel-track\">");
|
||||
for (int i = 0; i < slides.size(); i++) {
|
||||
Slide s = slides.get(i);
|
||||
b.append("<figure class=\"rw-carousel-slide").append(i == 0 ? " is-active" : "").append("\">");
|
||||
boolean linked = !s.link().isBlank();
|
||||
if (linked) {
|
||||
b.append("<a class=\"rw-carousel-link\" href=\"").append(Html.escape(s.link())).append("\">");
|
||||
}
|
||||
b.append("<img class=\"rw-carousel-img\" src=\"").append(Html.escape(s.imageUrl()))
|
||||
.append("\" alt=\"").append(Html.escape(s.caption())).append("\" loading=\"lazy\">");
|
||||
if (linked) {
|
||||
b.append("</a>");
|
||||
}
|
||||
if (captions && !s.caption().isBlank()) {
|
||||
b.append("<figcaption class=\"rw-carousel-caption\">").append(Html.escape(s.caption()))
|
||||
.append("</figcaption>");
|
||||
}
|
||||
b.append("</figure>");
|
||||
}
|
||||
b.append("</div>");
|
||||
if (controls && slides.size() > 1) {
|
||||
b.append("<button type=\"button\" class=\"rw-carousel-prev\" aria-label=\"Previous\">‹</button>")
|
||||
.append("<button type=\"button\" class=\"rw-carousel-next\" aria-label=\"Next\">›</button>");
|
||||
}
|
||||
b.append("</div><script src=\"").append(STATIC).append("/carousel.js\" defer></script>");
|
||||
return RenderFragment.of(b.toString());
|
||||
}
|
||||
|
||||
/** Parses the per-tenant {@code slides} text (one {@code imageUrl | caption | link} per line). */
|
||||
static List<Slide> slides(SettingsView cfg) {
|
||||
List<Slide> out = new ArrayList<>();
|
||||
for (String line : cfg.string("slides", "").split("\\R")) {
|
||||
Slide slide = Slide.parse(line);
|
||||
if (slide != null) {
|
||||
out.add(slide);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Capabilities capabilities() {
|
||||
return Capabilities.of("widget.carousel");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
/**
|
||||
* Minimal HTML escaping for the values the carousel widget interpolates (image URLs, captions, links from the
|
||||
* per-tenant config). The plugin owns escaping of its own output so a hand-entered caption or URL can never
|
||||
* inject markup into a rendered page.
|
||||
*/
|
||||
final class Html {
|
||||
|
||||
private Html() {
|
||||
}
|
||||
|
||||
static String escape(String value) {
|
||||
if (value == null || value.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
return value.replace("&", "&").replace("<", "<").replace(">", ">")
|
||||
.replace("\"", """).replace("'", "'");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
/**
|
||||
* One carousel slide, parsed from a per-tenant {@code slides} list entry of the form
|
||||
* {@code imageUrl | caption | linkUrl} (caption and link optional). Immutable; all fields already trimmed and
|
||||
* HTML-escaped at render time by the widget.
|
||||
*
|
||||
* @param imageUrl the slide image URL (required — a slide with no image is dropped)
|
||||
* @param caption the optional caption text ({@code ""} if none)
|
||||
* @param link the optional link the slide navigates to when clicked ({@code ""} if none)
|
||||
*/
|
||||
record Slide(String imageUrl, String caption, String link) {
|
||||
|
||||
/** Parses one {@code imageUrl | caption | linkUrl} entry; returns {@code null} if it has no usable image. */
|
||||
static Slide parse(String entry) {
|
||||
if (entry == null || entry.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String[] parts = entry.split("\\|", 3);
|
||||
String image = parts[0].strip();
|
||||
if (image.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String caption = parts.length > 1 ? parts[1].strip() : "";
|
||||
String link = parts.length > 2 ? parts[2].strip() : "";
|
||||
return new Slide(image, caption, link);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.Map;
|
||||
import mx.quest.runway.spi.testkit.FakePluginSettings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** The console status model counts the slides the widget would render, per site. */
|
||||
class CarouselAdminModelTest {
|
||||
|
||||
@Test
|
||||
void countsOnlyRenderableSlidesPerSite() {
|
||||
FakePluginSettings settings = new FakePluginSettings()
|
||||
.site("acme", Map.of("slides", "/img/a.jpg | A\n\n | no image | /x\n/img/b.jpg"))
|
||||
.site("other", Map.of("slides", "/img/c.jpg"));
|
||||
CarouselAdminModel model = new CarouselAdminModel(settings);
|
||||
assertEquals(2, model.slideCount("acme"), "blank lines and image-less lines are not slides");
|
||||
assertEquals(1, model.slideCount("other"));
|
||||
assertEquals(0, model.slideCount("unconfigured"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import mx.quest.runway.spi.ContentWidget;
|
||||
import mx.quest.runway.spi.service.PluginSettings;
|
||||
import mx.quest.runway.spi.testkit.FakePluginSettings;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* The plugin's contract with the host, checked at build time: the wiring, the bean-name rule, the bean names the
|
||||
* templates reference, the descriptor floor, and that the compiled classes link nothing of the host but the SPI —
|
||||
* the same things the kernel's load-time checks refuse.
|
||||
*/
|
||||
class CarouselConfigTest {
|
||||
|
||||
private static final String PREFIX = "carouselManager";
|
||||
|
||||
private static Set<String> beanMethodNames() {
|
||||
return Arrays.stream(CarouselConfig.class.getDeclaredMethods())
|
||||
.filter(m -> m.isAnnotationPresent(Bean.class))
|
||||
.map(Method::getName)
|
||||
.collect(Collectors.toCollection(TreeSet::new));
|
||||
}
|
||||
|
||||
@Test
|
||||
void wiresTheWidgetAndTheAdminModelUnderPrefixedNames() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
|
||||
ctx.registerBean(PluginSettings.class,
|
||||
() -> new FakePluginSettings().site("acme", Map.of("slides", "/img/a.jpg\n/img/b.jpg")));
|
||||
ctx.register(CarouselConfig.class);
|
||||
ctx.refresh();
|
||||
assertInstanceOf(CarouselWidget.class, ctx.getBean("carouselManagerWidget", ContentWidget.class));
|
||||
assertEquals(2, ctx.getBean("carouselManagerAdminModel", CarouselAdminModel.class).slideCount("acme"),
|
||||
"the admin model reads the injected settings");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyBeanNameStartsWithThePluginIdInLowerCamelCase() {
|
||||
Set<String> names = beanMethodNames();
|
||||
assertEquals(Set.of("carouselManagerWidget", "carouselManagerAdminModel"), names);
|
||||
for (Method m : CarouselConfig.class.getDeclaredMethods()) {
|
||||
Bean bean = m.getAnnotation(Bean.class);
|
||||
if (bean != null) {
|
||||
for (String alias : bean.name()) {
|
||||
assertTrue(alias.startsWith(PREFIX), "an explicit bean name must start with " + PREFIX + ": " + alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyBeanTheTemplatesReferenceIsOneOfOurs() throws IOException {
|
||||
Set<String> referenced = new TreeSet<>();
|
||||
try (Stream<Path> files = Files.list(Path.of("templates"))) {
|
||||
for (Path file : files.filter(p -> p.toString().endsWith(".th.xml")).toList()) {
|
||||
String text = Files.readString(file, StandardCharsets.UTF_8);
|
||||
Matcher m = Pattern.compile("\\$\\{\\s*@(\\w+)").matcher(text);
|
||||
while (m.find()) {
|
||||
referenced.add(m.group(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
assertFalse(referenced.isEmpty(), "the console templates read the admin model");
|
||||
assertTrue(beanMethodNames().containsAll(referenced), "templates reference unknown beans: " + referenced);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDescriptorDeclaresTheSpiFloorAndTheReleaseVersion() throws IOException {
|
||||
String yml = Files.readString(Path.of("plugin.yml"), StandardCharsets.UTF_8);
|
||||
assertTrue(yml.contains("\nrequiresSpi: \">=1.0.0\"\n"), "built for the frozen SPI 1.0.0 contract");
|
||||
String pom = Files.readString(Path.of("pom.xml"), StandardCharsets.UTF_8);
|
||||
Matcher version = Pattern.compile("<artifactId>runway-plugin-carousel-manager</artifactId>\\s*<version>([^<]+)")
|
||||
.matcher(pom);
|
||||
assertTrue(version.find());
|
||||
assertTrue(yml.contains("\nversion: " + version.group(1) + "\n"), "plugin.yml and pom.xml agree on the version");
|
||||
assertFalse(pom.contains("<artifactId>runway-app</artifactId>"), "no runway-app dependency in any scope");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCompiledClassesLinkNoHostInternals() throws IOException {
|
||||
Path classes = Path.of("target", "classes");
|
||||
Pattern host = Pattern.compile("mx[/.]quest[/.]runway[/.](?!spi[/.])[\\w/.$]*");
|
||||
List<String> problems = new ArrayList<>();
|
||||
int scanned = 0;
|
||||
try (Stream<Path> files = Files.walk(classes)) {
|
||||
for (Path file : files.filter(p -> p.toString().endsWith(".class")).toList()) {
|
||||
scanned++;
|
||||
String rel = classes.relativize(file).toString().replace('\\', '/');
|
||||
if (rel.startsWith("mx/quest/runway/")) {
|
||||
problems.add(rel + " lives under mx.quest.runway");
|
||||
}
|
||||
// The constant pool holds class names and descriptors as (modified) UTF-8; Latin-1 keeps every
|
||||
// byte as one char, so an ASCII pattern finds them without parsing the class file.
|
||||
Matcher m = host.matcher(new String(Files.readAllBytes(file), StandardCharsets.ISO_8859_1));
|
||||
while (m.find()) {
|
||||
problems.add(rel + " → " + m.group());
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue(scanned >= 4, "the compiled classes were found (" + scanned + ")");
|
||||
assertTrue(problems.isEmpty(), "host linkage: " + problems);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
package mx.quest.carousel;
|
||||
|
||||
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 java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import mx.quest.runway.spi.model.PageMeta;
|
||||
import mx.quest.runway.spi.model.RenderFragment;
|
||||
import mx.quest.runway.spi.model.SiteView;
|
||||
import mx.quest.runway.spi.model.WidgetView;
|
||||
import mx.quest.runway.spi.testkit.FakePluginSettings;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* The carousel widget: slide parsing, rendering, escaping, setting gating, defaults and clamping, and the
|
||||
* empty-when-unconfigured case — against the SPI testkit's {@link FakePluginSettings}, configured with the
|
||||
* descriptor's real {@code siteSettings:} defaults (read from {@code plugin.yml}) the way the kernel applies them.
|
||||
*/
|
||||
class CarouselWidgetTest {
|
||||
|
||||
private FakePluginSettings settings;
|
||||
private CarouselWidget widget;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
settings = new FakePluginSettings().defaults(descriptorDefaults());
|
||||
widget = new CarouselWidget(settings);
|
||||
}
|
||||
|
||||
/** The {@code default:} of every {@code siteSettings:} entry in {@code plugin.yml}, typed as the kernel types them. */
|
||||
static Map<String, Object> descriptorDefaults() throws IOException {
|
||||
String yml = Files.readString(Path.of("plugin.yml"), StandardCharsets.UTF_8);
|
||||
Matcher m = Pattern.compile("\\{\\s*key:\\s*(\\w+)\\s*,\\s*type:\\s*(\\w+)[^}]*?default:\\s*([^,}]+?)\\s*[,}]")
|
||||
.matcher(yml);
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
while (m.find()) {
|
||||
String raw = m.group(3);
|
||||
Object value = switch (m.group(2)) {
|
||||
case "boolean" -> Boolean.valueOf(raw);
|
||||
case "int" -> Long.valueOf(raw);
|
||||
default -> raw;
|
||||
};
|
||||
out.put(m.group(1), value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
private String html(String siteId) {
|
||||
SiteView site = new SiteView(siteId, "Acme", Locale.ENGLISH, List.of(Locale.ENGLISH), null);
|
||||
PageMeta page = new PageMeta("home", "Home", "", List.of(Locale.ENGLISH), Locale.ENGLISH, true, false);
|
||||
RenderFragment fragment = widget.render(new WidgetView(site, page, Locale.ENGLISH, "hero", false, Map.of()));
|
||||
return fragment.isEmpty() ? "" : fragment.html();
|
||||
}
|
||||
|
||||
private String render(Map<String, ?> stored) {
|
||||
settings.site("acme", stored);
|
||||
return html("acme");
|
||||
}
|
||||
|
||||
private static String attr(String html, String name) {
|
||||
Matcher m = Pattern.compile(name + "=\"([^\"]*)\"").matcher(html);
|
||||
return m.find() ? m.group(1) : null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void keyIsCarousel() {
|
||||
assertEquals("carousel", widget.key());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rendersConfiguredSlidesWithCaptionsAndLinks() {
|
||||
String html = render(Map.of("slides", "/img/a.jpg | Welcome | /about\n/img/b.jpg | Our work | /work"));
|
||||
assertTrue(html.contains("src=\"/img/a.jpg\""), "first slide image");
|
||||
assertTrue(html.contains("src=\"/img/b.jpg\""), "second slide image");
|
||||
assertTrue(html.contains(">Welcome</figcaption>"), "caption rendered");
|
||||
assertTrue(html.contains("href=\"/about\""), "link rendered");
|
||||
assertTrue(html.contains("/__plugin/carousel-manager/static/carousel.css"), "references the plugin CSS");
|
||||
assertTrue(html.contains("/__plugin/carousel-manager/static/carousel.js"), "references the plugin JS");
|
||||
assertEquals(1, html.split("is-active", -1).length - 1, "exactly the first slide is active");
|
||||
assertTrue(html.indexOf("is-active") < html.indexOf("/img/a.jpg"), "…and it is the first one");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anImageOnlySlideNeedsNoCaptionOrLink() {
|
||||
String html = render(Map.of("slides", "/img/solo.jpg"));
|
||||
assertTrue(html.contains("src=\"/img/solo.jpg\""));
|
||||
assertFalse(html.contains("<a class=\"rw-carousel-link\""), "no link element when none configured");
|
||||
assertFalse(html.contains("rw-carousel-caption"), "no caption element when none configured");
|
||||
}
|
||||
|
||||
@Test
|
||||
void noSlidesRendersEmpty() {
|
||||
assertTrue(render(Map.of()).isEmpty(), "an unconfigured carousel renders nothing");
|
||||
assertTrue(render(Map.of("slides", " \n ")).isEmpty(), "blank slide lines render nothing");
|
||||
assertTrue(render(Map.of("slides", " | caption only | /x")).isEmpty(), "a line with no image is dropped");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCaptionWithACommaSurvivesAndWindowsLineEndingsSplit() {
|
||||
String html = render(Map.of("slides", "/img/a.jpg | Fast, fresh, local | /x\r\n/img/b.jpg"));
|
||||
assertTrue(html.contains(">Fast, fresh, local</figcaption>"), "the text setting is never comma-split");
|
||||
assertTrue(html.contains("src=\"/img/b.jpg\""), "a CRLF line ending separates slides");
|
||||
}
|
||||
|
||||
@Test
|
||||
void captionsAndControlsHonorTheirFlags() {
|
||||
String noCaptions = render(Map.of("slides", "/img/a.jpg | Hi | /x\n/img/b.jpg | Yo | /y",
|
||||
"showCaptions", false));
|
||||
assertFalse(noCaptions.contains("rw-carousel-caption"), "captions off ⇒ no caption element");
|
||||
assertTrue(noCaptions.contains("alt=\"Hi\""), "…the caption still serves as the image's alt text");
|
||||
String noControls = render(Map.of("slides", "/img/a.jpg\n/img/b.jpg", "showControls", false));
|
||||
assertFalse(noControls.contains("rw-carousel-next"), "controls off ⇒ no next button");
|
||||
assertEquals("false", attr(noControls, "data-controls"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void flagsStoredAsTextAreReadToo() {
|
||||
String html = render(Map.of("slides", "/img/a.jpg | Hi\n/img/b.jpg", "showCaptions", "off",
|
||||
"showControls", "no"));
|
||||
assertFalse(html.contains("rw-carousel-caption"), "\"off\" turns captions off");
|
||||
assertFalse(html.contains("rw-carousel-next"), "\"no\" turns controls off");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theDefaultsShowCaptionsAndControlsAndAdvanceEveryFiveSeconds() {
|
||||
String html = render(Map.of("slides", "/img/a.jpg | Hi | /x\n/img/b.jpg"));
|
||||
assertEquals("5", attr(html, "data-interval"));
|
||||
assertEquals("true", attr(html, "data-controls"));
|
||||
assertTrue(html.contains("rw-carousel-prev") && html.contains("rw-carousel-next"), "two slides ⇒ controls");
|
||||
assertTrue(html.contains("rw-carousel-caption"), "captions on by default");
|
||||
}
|
||||
|
||||
@Test
|
||||
void controlsNeedMoreThanOneSlide() {
|
||||
String html = render(Map.of("slides", "/img/a.jpg"));
|
||||
assertFalse(html.contains("rw-carousel-prev"), "a single slide has nothing to step through");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theIntervalIsReadAndClampedToTheDeclaredRange() {
|
||||
assertEquals("12", attr(render(Map.of("slides", "/img/a.jpg", "intervalSeconds", 12L)), "data-interval"));
|
||||
assertEquals("12", attr(render(Map.of("slides", "/img/a.jpg", "intervalSeconds", "12")), "data-interval"));
|
||||
assertEquals("0", attr(render(Map.of("slides", "/img/a.jpg", "intervalSeconds", 0L)), "data-interval"),
|
||||
"0 = auto-advance off");
|
||||
assertEquals("60", attr(render(Map.of("slides", "/img/a.jpg", "intervalSeconds", 600L)), "data-interval"));
|
||||
assertEquals("0", attr(render(Map.of("slides", "/img/a.jpg", "intervalSeconds", -3L)), "data-interval"));
|
||||
assertEquals("5", attr(render(Map.of("slides", "/img/a.jpg", "intervalSeconds", "soon")), "data-interval"),
|
||||
"an unparsable hand-edited value falls back to the default");
|
||||
}
|
||||
|
||||
@Test
|
||||
void codeFallbacksEqualTheDescriptorDefaults() throws IOException {
|
||||
Map<String, Object> declared = descriptorDefaults();
|
||||
assertEquals(Map.of("intervalSeconds", 5L, "showCaptions", true, "showControls", true), declared,
|
||||
"plugin.yml declares the defaults this test (and the docs) assume");
|
||||
String slides = "/img/a.jpg | Hi | /x\n/img/b.jpg | Yo";
|
||||
String withDefaults = render(Map.of("slides", slides));
|
||||
widget = new CarouselWidget(new FakePluginSettings().site("acme", Map.of("slides", slides)));
|
||||
assertEquals(withDefaults, html("acme"), "the widget renders the same with or without the host's defaults");
|
||||
}
|
||||
|
||||
@Test
|
||||
void eachSiteRendersItsOwnSlides() {
|
||||
settings.site("acme", Map.of("slides", "/img/acme.jpg"));
|
||||
settings.site("other", Map.of("slides", "/img/other.jpg"));
|
||||
assertTrue(html("acme").contains("/img/acme.jpg") && !html("acme").contains("/img/other.jpg"));
|
||||
assertTrue(html("other").contains("/img/other.jpg") && !html("other").contains("/img/acme.jpg"));
|
||||
assertTrue(html("nobody").isEmpty(), "a site with nothing saved renders nothing");
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCraftedCaptionOrUrlIsEscaped() {
|
||||
String html = render(Map.of("slides", "/img/a.jpg\" onerror=\"x | <script>x</script> | javascript:\"x"));
|
||||
assertFalse(html.contains("<script>x</script>"), "the caption is HTML-escaped, never injected");
|
||||
assertTrue(html.contains("<script>"), "…it appears escaped");
|
||||
assertFalse(html.contains("\" onerror=\""), "an image URL cannot break out of its attribute");
|
||||
assertTrue(html.contains("src=\"/img/a.jpg" onerror="x\""));
|
||||
assertTrue(html.contains("href=\"javascript:"x\""), "the link's quote is escaped too");
|
||||
}
|
||||
|
||||
@Test
|
||||
void anUnsafeSiteIdIsRefusedNotRead() {
|
||||
SiteView site = new SiteView("../etc", "x", Locale.ENGLISH, List.of(Locale.ENGLISH), null);
|
||||
PageMeta page = new PageMeta("home", "Home", "", List.of(Locale.ENGLISH), Locale.ENGLISH, true, false);
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> widget.render(new WidgetView(site, page, Locale.ENGLISH, "hero", false, Map.of())),
|
||||
"the settings service refuses it; the host's containment turns that into an empty widget");
|
||||
}
|
||||
|
||||
@Test
|
||||
void declaresItsCapability() {
|
||||
assertTrue(widget.capabilities().toString().contains("widget.carousel"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user