|
| 1 | +package com.demcha.compose.document.templates.cv.components; |
| 2 | + |
| 3 | +import com.demcha.compose.GraphCompose; |
| 4 | +import com.demcha.compose.document.api.DocumentPageSize; |
| 5 | +import com.demcha.compose.document.api.DocumentSession; |
| 6 | +import com.demcha.compose.document.node.DocumentNode; |
| 7 | +import com.demcha.compose.document.node.ExternalLinkTarget; |
| 8 | +import com.demcha.compose.document.node.InlineRun; |
| 9 | +import com.demcha.compose.document.node.InlineTextRun; |
| 10 | +import com.demcha.compose.document.node.ParagraphNode; |
| 11 | +import com.demcha.compose.document.templates.api.DocumentTemplate; |
| 12 | +import com.demcha.compose.document.templates.cv.data.CvDocument; |
| 13 | +import com.demcha.compose.document.templates.cv.data.CvIdentity; |
| 14 | +import com.demcha.compose.document.templates.cv.data.RowStyle; |
| 15 | +import com.demcha.compose.document.templates.cv.data.RowsSection; |
| 16 | +import com.demcha.compose.document.templates.cv.presets.EngineeringResume; |
| 17 | +import com.demcha.compose.document.templates.cv.presets.SidebarPortrait; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static org.assertj.core.api.Assertions.assertThat; |
| 24 | + |
| 25 | +/** |
| 26 | + * End-to-end wiring for inline-link CV project-card titles: a project row whose |
| 27 | + * label carries {@code [label](url)} markdown renders as a clickable link in the |
| 28 | + * two presets that hand-roll their project card outside the shared |
| 29 | + * {@code ProjectRenderer} — {@code EngineeringResume} (bordered card in the main |
| 30 | + * column) and {@code SidebarPortrait} (stacked row in the main column). Plain |
| 31 | + * labels stay plain and the trailing {@code " (stack)"} run is preserved, so the |
| 32 | + * change is backward-compatible. |
| 33 | + * |
| 34 | + * <p>The {@link com.demcha.compose.document.templates.core.text.MarkdownInline} |
| 35 | + * link primitive and {@link ProjectLabel} split are unit-tested separately; this |
| 36 | + * locks the preset wiring so a refactor cannot silently revert a project title to |
| 37 | + * a flat styled run (the state before this change), which would drop the link.</p> |
| 38 | + */ |
| 39 | +class ProjectTitleLinkTest { |
| 40 | + |
| 41 | + private static CvDocument docWithProject(String label) { |
| 42 | + return CvDocument.builder() |
| 43 | + .identity(CvIdentity.builder() |
| 44 | + .name("Test", "User").jobTitle("Engineer") |
| 45 | + .contact("+1 555 0100", "user@example.com", "City").build()) |
| 46 | + .section(RowsSection.builder("Projects", RowStyle.BULLETED_STACKED) |
| 47 | + .row(label, "Did meaningful work.") |
| 48 | + .build()) |
| 49 | + .build(); |
| 50 | + } |
| 51 | + |
| 52 | + /** Collects every inline text run in the composed document. */ |
| 53 | + private static List<InlineTextRun> textRuns(DocumentTemplate<CvDocument> template, |
| 54 | + CvDocument doc) { |
| 55 | + try (DocumentSession document = GraphCompose.document() |
| 56 | + .pageSize(DocumentPageSize.A4).margin(28, 28, 28, 28).create()) { |
| 57 | + template.compose(document, doc); |
| 58 | + List<ParagraphNode> paragraphs = new ArrayList<>(); |
| 59 | + collectParagraphs(document.roots(), paragraphs); |
| 60 | + List<InlineTextRun> runs = new ArrayList<>(); |
| 61 | + for (ParagraphNode paragraph : paragraphs) { |
| 62 | + for (InlineRun run : paragraph.inlineRuns()) { |
| 63 | + if (run instanceof InlineTextRun text) { |
| 64 | + runs.add(text); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return runs; |
| 69 | + } catch (Exception e) { |
| 70 | + throw new RuntimeException(e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static void collectParagraphs(List<DocumentNode> nodes, List<ParagraphNode> out) { |
| 75 | + for (DocumentNode node : nodes) { |
| 76 | + if (node instanceof ParagraphNode paragraph) { |
| 77 | + out.add(paragraph); |
| 78 | + } |
| 79 | + collectParagraphs(node.children(), out); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static boolean isExternalLink(InlineTextRun run) { |
| 84 | + return run.linkTarget() instanceof ExternalLinkTarget; |
| 85 | + } |
| 86 | + |
| 87 | + private static String uri(InlineTextRun run) { |
| 88 | + return ((ExternalLinkTarget) run.linkTarget()).options().uri(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * EngineeringResume's bordered project card: a link in the project title |
| 93 | + * reaches the output, and the trailing {@code " (stack)"} run is preserved. |
| 94 | + */ |
| 95 | + @Test |
| 96 | + void engineeringResumeProjectTitleBecomesLink() { |
| 97 | + List<InlineTextRun> runs = textRuns(EngineeringResume.create(), |
| 98 | + docWithProject("[Acme Corp](https://acme.example) (Java, PDFBox)")); |
| 99 | + assertThat(runs).filteredOn(ProjectTitleLinkTest::isExternalLink) |
| 100 | + .anySatisfy(run -> { |
| 101 | + assertThat(run.text()).isEqualTo("Acme Corp"); |
| 102 | + assertThat(uri(run)).isEqualTo("https://acme.example"); |
| 103 | + }); |
| 104 | + assertThat(runs).anySatisfy(run -> |
| 105 | + assertThat(run.text()).contains("Java, PDFBox")); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * SidebarPortrait's stacked project row: a link in the project title reaches |
| 110 | + * the output, and the trailing {@code " (stack)"} run is preserved. |
| 111 | + */ |
| 112 | + @Test |
| 113 | + void sidebarPortraitProjectTitleBecomesLink() { |
| 114 | + List<InlineTextRun> runs = textRuns(SidebarPortrait.create(), |
| 115 | + docWithProject("[Acme Corp](https://acme.example) (Java, PDFBox)")); |
| 116 | + assertThat(runs).filteredOn(ProjectTitleLinkTest::isExternalLink) |
| 117 | + .anySatisfy(run -> { |
| 118 | + assertThat(run.text()).isEqualTo("Acme Corp"); |
| 119 | + assertThat(uri(run)).isEqualTo("https://acme.example"); |
| 120 | + }); |
| 121 | + assertThat(runs).anySatisfy(run -> |
| 122 | + assertThat(run.text()).contains("Java, PDFBox")); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Backward-compatible: a plain project title is not rendered as a link. The |
| 127 | + * header contact block may render its own email/website links, so the control |
| 128 | + * asserts specifically that the plain project title run is not linked. |
| 129 | + */ |
| 130 | + @Test |
| 131 | + void plainProjectTitleIsNotLink() { |
| 132 | + assertThat(projectTitleLinked(EngineeringResume.create())).isFalse(); |
| 133 | + assertThat(projectTitleLinked(SidebarPortrait.create())).isFalse(); |
| 134 | + } |
| 135 | + |
| 136 | + private static boolean projectTitleLinked(DocumentTemplate<CvDocument> template) { |
| 137 | + return textRuns(template, docWithProject("Acme Corp (Java, PDFBox)")).stream() |
| 138 | + .filter(ProjectTitleLinkTest::isExternalLink) |
| 139 | + .anyMatch(run -> run.text().equalsIgnoreCase("Acme Corp")); |
| 140 | + } |
| 141 | +} |
0 commit comments