Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public String html() {
+ " Swagger UI</title>\n"
+ " <link rel=\"icon\" href=\"/favicon.svg\" type=\"image/svg+xml\">\n"
+ " <link rel=\"stylesheet\" href=\"https://unpkg.com/swagger-ui-dist/swagger-ui.css\">\n"
+ " <link rel=\"stylesheet\" href=\"/css/swagger-copy-for-ai.css\">\n"
+ " <style>body{margin:0;background:#fafafa}.swagger-topbar{height:44px;display:flex;align-items:center;gap:12px;padding:0 14px;background:#1f2733;color:#fff;font-family:Arial,Helvetica,sans-serif}.swagger-topbar a{color:#fff}.swagger-title{font-weight:700}</style>\n"
+ "</head>\n"
+ "<body>\n"
Expand All @@ -41,6 +42,8 @@ public String html() {
+ " });\n"
+ "};\n"
+ "</script>\n"
+ "<script>window.thingifierSwaggerCopyForAi = { openApiUrl: \"/openapi.json\" };</script>\n"
+ "<script src=\"/js/swagger-copy-for-ai.js\" charset=\"UTF-8\"></script>\n"
+ "</body>\n"
+ "</html>\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ public void swaggerUiPageEmbedsUnpkgSwaggerUiForCurrentOpenApiJson() {
Assertions.assertTrue(response.body().contains("href=\"/favicon.svg\""));
Assertions.assertTrue(response.body().contains("SwaggerUIBundle"));
Assertions.assertTrue(response.body().contains("/openapi.json"));
Assertions.assertTrue(response.body().contains("/css/swagger-copy-for-ai.css"));
Assertions.assertTrue(response.body().contains("/js/swagger-copy-for-ai.js"));
Assertions.assertTrue(response.body().contains("window.thingifierSwaggerCopyForAi"));
Assertions.assertTrue(response.body().contains("openApiUrl: \"/openapi.json\""));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void staticPagesAndGeneratedDocumentationAreReachable() {
assertStatusContains("/schema", 200, "Thingifier Schema Edit");
assertStatusContains("/docs", 200, "API Documentation");
assertStatusContains("/swagger", 200, "SwaggerUIBundle");
assertStatusContains("/swagger", 200, "swagger-copy-for-ai.js");
assertStatusContains("/openapi.json", 200, "\"openapi\"");
assertStatusContains("/docs/swagger", 200, "\"openapi\"");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package uk.co.compendiumdev.thingifier.crudui.e2e;

import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import uk.co.compendiumdev.thingifier.crudui.e2e.pages.SwaggerPage;

public class SwaggerCopyForAiUiIT extends BrowserTestBase {

private SwaggerPage swagger;

@BeforeEach
void openSwagger() {
resetToProjectTasks();
swagger = new SwaggerPage(page, server().baseUrl());
swagger.copy().installClipboardStub();
swagger.open();
}

@Test
public void operationCopyForAiCopiesMarkdownWithCurrentOriginAndApiPrefix() {
assertThat(swagger.copy().fullApiButton()).isVisible();
assertThat(swagger.copy().operationButton("POST", "/projects")).isVisible();

swagger.copy().operationButton("POST", "/projects").click();
swagger.copy().waitForCopiedTextContaining("POST " + server().baseUrl() + "/api/projects");

final String copied = swagger.copy().copiedText();
Assertions.assertTrue(copied.contains("# POST " + server().baseUrl() + "/api/projects"));
Assertions.assertTrue(copied.contains("- OpenAPI path: `/projects`"));
Assertions.assertTrue(copied.contains("## Request Body"));
Assertions.assertTrue(copied.contains("## Responses"));
Assertions.assertTrue(copied.contains("## Referenced Schemas"));
}

@Test
public void fullApiCopyForAiCopiesAllOperationsAndSchemas() {
assertThat(swagger.copy().firstOperationButton()).isVisible();

swagger.copy().fullApiButton().click();
swagger.copy().waitForCopiedTextContaining("## Component Schemas");

final String copied = swagger.copy().copiedText();
Assertions.assertTrue(copied.contains("# Project Tasks"));
Assertions.assertTrue(copied.contains("- Base URL: `" + server().baseUrl() + "/api`"));
Assertions.assertTrue(copied.contains("GET " + server().baseUrl() + "/api/projects"));
Assertions.assertTrue(copied.contains("POST " + server().baseUrl() + "/api/todos"));
Assertions.assertTrue(copied.contains("## Component Schemas"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.co.compendiumdev.thingifier.crudui.e2e.components;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;

public final class SwaggerCopyComponent {

private final Page page;

public SwaggerCopyComponent(final Page page) {
this.page = page;
}

public void installClipboardStub() {
page.addInitScript(
"window.__copiedText = '';\n"
+ "Object.defineProperty(navigator, 'clipboard', {\n"
+ " configurable: true,\n"
+ " value: { writeText: async function(text) { window.__copiedText = text; } }\n"
+ "});");
}

public Locator fullApiButton() {
return TestSelectors.byTestId(page, "swagger-copy-full-ai");
}

public Locator firstOperationButton() {
return TestSelectors.byTestId(page, "swagger-copy-operation-ai").first();
}

public Locator operationButton(final String method, final String path) {
return page.locator(".opblock")
.filter(new Locator.FilterOptions().setHasText(method.toUpperCase()))
.filter(new Locator.FilterOptions().setHasText(path))
.locator("[data-testid='swagger-copy-operation-ai']")
.first();
}

public String copiedText() {
return String.valueOf(page.evaluate("() => window.__copiedText || ''"));
}

public void waitForCopiedTextContaining(final String expectedText) {
page.waitForFunction(
"expected => (window.__copiedText || '').includes(expected)", expectedText);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uk.co.compendiumdev.thingifier.crudui.e2e.pages;

import com.microsoft.playwright.Page;
import uk.co.compendiumdev.thingifier.crudui.e2e.components.SwaggerCopyComponent;
import uk.co.compendiumdev.thingifier.crudui.e2e.components.TestSelectors;

public final class SwaggerPage {

private final Page page;
private final String baseUrl;
private final SwaggerCopyComponent copy;

public SwaggerPage(final Page page, final String baseUrl) {
this.page = page;
this.baseUrl = baseUrl;
this.copy = new SwaggerCopyComponent(page);
}

public SwaggerPage open() {
page.navigate(baseUrl + "/swagger");
TestSelectors.byTestId(page, "swagger-copy-full-ai").waitFor();
TestSelectors.byTestId(page, "swagger-copy-operation-ai").first().waitFor();
return this;
}

public SwaggerCopyComponent copy() {
return copy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public final class SwaggerUiPage {
"https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js";
private static final String SWAGGER_UI_PRESET =
"https://unpkg.com/swagger-ui-dist/swagger-ui-standalone-preset.js";
private static final String COPY_FOR_AI_CSS = "/css/swagger-copy-for-ai.css";
private static final String COPY_FOR_AI_SCRIPT = "/js/swagger-copy-for-ai.js";

private final ThingifierApiDocumentationDefn apiDefn;
private final DefaultGUIHTML guiManagement;
Expand Down Expand Up @@ -74,6 +76,14 @@ public String html() {
html.append("}, 0);");
html.append("};");
html.append("</script>");
html.append("<script>");
html.append("window.thingifierSwaggerCopyForAi = {");
html.append("openApiUrl: \"").append(escapeJavaScriptString(openApiUrl)).append("\"");
html.append("};");
html.append("</script>");
html.append("<script src='")
.append(COPY_FOR_AI_SCRIPT)
.append("' charset='UTF-8'></script>");
html.append("</div>");
html.append(guiManagement.getEndOfMainContentMarker());
html.append(guiManagement.getPageFooter());
Expand All @@ -85,6 +95,9 @@ private String headInject() {
return "<link rel='stylesheet' href='"
+ SWAGGER_UI_CSS
+ "'>"
+ "<link rel='stylesheet' href='"
+ COPY_FOR_AI_CSS
+ "'>"
+ "<style>"
+ lightThemeCss()
+ "</style>";
Expand Down
44 changes: 44 additions & 0 deletions thingifier/src/main/resources/public/css/swagger-copy-for-ai.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.swagger-copy-ai-toolbar {
display: flex;
align-items: center;
gap: 0.75rem;
margin: 1rem 0;
font-family: Arial, Helvetica, sans-serif;
}

.swagger-copy-ai-button {
border: 1px solid #777;
border-radius: 4px;
background: #fff;
color: #222;
cursor: pointer;
font-size: 12px;
font-weight: 700;
line-height: 1.2;
padding: 0.45rem 0.7rem;
}

.swagger-copy-ai-button:hover,
.swagger-copy-ai-button:focus {
border-color: #1f5f8b;
color: #1f5f8b;
}

.swagger-copy-ai-button:disabled {
cursor: wait;
opacity: 0.75;
}

.swagger-copy-ai-operation {
margin-left: auto;
white-space: nowrap;
}

.swagger-copy-ai-status {
color: #555;
font-size: 12px;
}

.swagger-ui .opblock .opblock-summary {
gap: 0.35rem;
}
Loading
Loading