diff --git a/README.md b/README.md index 898b00e..57ae5b2 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,25 @@ Each account is fully isolated — its own aircraft, service timeline, flight lo Aviary is also an MCP server, so an AI assistant can import your existing maintenance records — a spreadsheet, a typed list, another tracker's export — into your service timeline. -Add `https://aviary.danielibarra.com/mcp` as a remote (HTTP) MCP server in any assistant that supports MCP, then sign in when it asks. +The easiest way in is the **Import with AI** button on the dashboard — it shows the address with a Copy button and these same steps. -You sign in on Aviary's own login page and approve access; the assistant never sees your password. Then hand it your records and ask it to import them. It shows you a **proposal** first — the items it will add, anything it isn't sure about, and any rows it couldn't use and why. Nothing is added until you tell it to go ahead, and it never changes or deletes items you already have. See or disconnect assistants any time under the robot icon on the dashboard (**Connected assistants**). +**Connect (one time):** + +1. In your AI assistant, add a **custom connector** (also called a **remote MCP server**). +2. Paste the address: `https://aviary.danielibarra.com/mcp` +3. When the assistant asks you to sign in, log in to Aviary and click **Allow**. + +You sign in on Aviary's own login page; the assistant never sees your password. The assistant must support remote MCP servers with OAuth sign-in. + +**Import:** + +1. Give the assistant your records — paste a list or attach a spreadsheet — and ask it to import them into Aviary. +2. It shows you a **proposal** first: the items it will add, anything flagged for you to check (unclear dates, no due date, items you already have), and any rows it couldn't use and why. +3. Tell it to go ahead. The items are added to the bottom of your service timeline. Nothing is added before you confirm, and existing items are never changed or deleted. + +Any row with a name can be imported — including on-condition items with no dates. Values that can't be used are left blank with a note. + +**Disconnect:** open **Import with AI** on the dashboard; your connected assistants are listed with a **Disconnect** button, plus what each one imported. A disconnected assistant is cut off on its next request. --- @@ -95,6 +111,33 @@ push to main - **Production** (Heroku app `aviarist-d300b0c36379`, served at the link at the top) is a separate app, promoted from staging deliberately once it looks good. - Heroku runs the app from the `Procfile` (`java -jar build/libs/AviaryService-0.0.1-SNAPSHOT.jar`) on Java 21 (`system.properties`). Schema changes apply via Hibernate `ddl-auto=update`. +## MCP server (developers) + +`/mcp` is an [MCP](https://modelcontextprotocol.io) server (Spring AI, Streamable HTTP). Aviary is also its own OAuth 2.1 authorization server, so assistants sign in as an owner. + +| | | +|---|---| +| Endpoint | `POST /mcp` — needs `Authorization: Bearer `; without one you get `401` with `WWW-Authenticate: Bearer resource_metadata=…` | +| Discovery | `/.well-known/oauth-protected-resource`, `/.well-known/oauth-authorization-server` | +| Sign-in | Authorization code + PKCE (S256). Clients identify with a **Client ID Metadata Document** (an https `client_id` URL); Dynamic Client Registration is off | +| Tokens | Opaque, 7 days, stored SHA-256 hashed; checked against the database on every call, so Disconnect is immediate | +| Scopes | `aviary.read`, `aviary.timeline.import` | + +**Tools** (each acts only on the signed-in owner's data): + +| Tool | What it does | +|---|---| +| `get_import_format` | Item fields, rules, and an example | +| `list_service_timeline` | The owner's current items | +| `propose_timeline_import` | Validates items into a proposal (24 h); changes nothing | +| `continue_poor_fit` | Continues a proposal where most rows didn't fit, after the owner agrees | +| `confirm_timeline_import` | Adds exactly the proposal's items, all or nothing | +| `list_flights` | The owner's logged flights | + +**Config:** `aviary.oauth.issuer` (env `AVIARY_OAUTH_ISSUER`) is the site's public URL; the MCP address is that plus `/mcp`. It defaults to `http://localhost:8080` locally and `https://aviary.danielibarra.com` on Heroku — set it on staging to the staging URL, or sign-in there points at production. + +**Try it locally:** run `./gradlew bootRun`, then add `http://localhost:8080/mcp` as a remote MCP server in an MCP client (or use the [MCP Inspector](https://github.com/modelcontextprotocol/inspector)) and sign in with a seeded account. + ## Architecture The app is a small, conventional Spring MVC project under `src/main/java/com/example/AviaryService/`: @@ -102,7 +145,9 @@ The app is a small, conventional Spring MVC project under `src/main/java/com/exa - **`controllers/UserController.java`** — every route: auth pages, the dashboard, timeline CRUD + reordering, hours updates, flight-log CRUD, the Garmin CSV parser, and "Complete Maintenance." - **`entity/`** — `User`, `ServiceTimeline`, `FlightLog`, `DescriptionOption` (+ a `DTO/` for timeline updates). - **`repositories/`** — Spring Data JPA repositories, all scoped by user. -- **`config/`** — `SecurityConfig` (login/logout, BCrypt, per-route access) and `DataSeeder` (the H2 demo data). +- **`config/`** — `SecurityConfig` (login/logout, BCrypt, per-route access, the `/mcp` bearer-token chain) and `DataSeeder` (the H2 demo data). +- **`oauth/`** — the OAuth authorization server for MCP sign-in (client metadata documents, hashed token storage, token checks). +- **`mcp/`** — MCP tools; **`services/imports/`** + **`util/TimelineImportRules`** — import proposals, confirm, and the Import with AI page's connections. - **`resources/templates/`** + **`resources/static/`** — Thymeleaf views and the dashboard's CSS/JS. Deeper design docs live in **[docs/](docs/)** — see `ARCHITECTURE.md`, `BACKEND.md`, `FRONTEND.md`, `DATABASE.md`, and `BUILD_AND_DEPLOY.md`. A **Share/Export** feature (print / PDF / email / text) is specced in `docs/specs/SHARE_EXPORT_SPEC.md` but not yet built. diff --git a/docs/architecture/FRONTEND.md b/docs/architecture/FRONTEND.md index 25a67d6..59091c8 100644 --- a/docs/architecture/FRONTEND.md +++ b/docs/architecture/FRONTEND.md @@ -46,3 +46,7 @@ Each stylesheet defines the same CSS-variable palette (`--primary` green `#2C9B6 - Log Book rows are readonly — to fix a typo a user must delete + re-add (a known deferred improvement). - Login/register field + button labels are injected via CSS `::before` pseudo-elements, which is fragile and not screen-reader friendly. - No frontend tests exist. + +## Import with AI page +- `GET /account/connections` (`connections.html`): titled "Import with AI". An always-visible connect box shows this site's MCP address (`mcpUrl` = `aviary.oauth.issuer` + `/mcp`, so staging shows staging's), a Copy button (`js/connections.js`, falls back to selecting the address), three steps, the per-assistant "where to find it" list (`fragments/assistant-hints.html` — one line per assistant, update there when an app moves its menu), and what to do next. The connected-assistant list and recent imports sit below under "Connected assistants". +- Dashboard entry points: header `#connections-link` (robot icon + "Import with AI"; text hidden under 420px, `aria-label` kept) and `#import-ai-prompt` above the empty Service Timeline (server-rendered only when there are no rows; `dashboard.js` removes it via a MutationObserver when the first row is added). Both are `no-print`. diff --git a/src/browserTest/java/com/example/AviaryService/browser/ConnectionsPageTest.java b/src/browserTest/java/com/example/AviaryService/browser/ConnectionsPageTest.java index 8dba0ce..5168221 100644 --- a/src/browserTest/java/com/example/AviaryService/browser/ConnectionsPageTest.java +++ b/src/browserTest/java/com/example/AviaryService/browser/ConnectionsPageTest.java @@ -3,6 +3,7 @@ import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; import java.time.Instant; +import java.util.List; import java.util.UUID; import org.junit.jupiter.api.Test; @@ -14,9 +15,9 @@ import com.example.AviaryService.repositories.TimelineImportRecordRepository; /** - * Account -> Connected assistants: a connected assistant is listed, the - * dashboard links to the page, and Disconnect removes it -- at desktop and - * phone widths. + * Import with AI page: the connect box (address, Copy, steps, hints) is always + * shown; a connected assistant is listed with its imports, and Disconnect + * removes it -- at desktop and phone widths. */ class ConnectionsPageTest extends BrowserTestBase { @@ -55,6 +56,7 @@ private void listAndDisconnect(int width, int height) { page.locator("#connections-link").click(); page.waitForURL(baseUrl() + "/account/connections"); + assertThat(page.locator("#connect-box")).isVisible(); assertThat(page.locator(".connection-name")).hasText("Test Assistant"); assertThat(page.locator(".connection-meta")).containsText("assistant.example.com"); assertThat(page.locator(".connection-imports")).containsText("added 3 items"); @@ -68,7 +70,49 @@ private void listAndDisconnect(int width, int height) { page.locator("button.connection-disconnect").click(); page.waitForURL("**/account/connections?disconnected"); assertThat(page.locator(".connection")).hasCount(0); - assertThat(page.locator("body")).containsText("No AI assistants are connected"); + assertThat(page.locator("body")).containsText("No assistants connected yet"); + assertThat(page.locator("#connect-box")).isVisible(); + } + + private void assertNoSidewaysScroll(int width) { + Object overflow = page.evaluate("document.documentElement.scrollWidth > window.innerWidth"); + if (Boolean.TRUE.equals(overflow)) { + throw new AssertionError("Import with AI page scrolls horizontally at " + width + "px"); + } + } + + @Test + void connectBoxShowsWithNoConnections() { + page.setViewportSize(390, 844); + String username = seedUser("password123"); + loginAs(username, "password123"); + page.navigate(baseUrl() + "/account/connections"); + + assertThat(page.locator("h1")).hasText("Import with AI"); + assertThat(page.locator("#connect-box")).isVisible(); + String address = page.locator("#mcp-url").inputValue(); + if (!address.endsWith("/mcp")) throw new AssertionError("Unexpected address: " + address); + assertThat(page.locator("#connect-steps li")).hasCount(3); + if (page.locator("#assistant-hints li").count() < 1) throw new AssertionError("No assistant hints"); + assertThat(page.locator("#connect-next")).containsText("preview"); + assertThat(page.locator("body")).containsText("No assistants connected yet"); + assertNoSidewaysScroll(390); + } + + @Test + void copyButtonCopiesTheAddress() { + context.grantPermissions(List.of("clipboard-read", "clipboard-write"), new com.microsoft.playwright.BrowserContext.GrantPermissionsOptions().setOrigin(baseUrl())); + String username = seedUser("password123"); + loginAs(username, "password123"); + page.navigate(baseUrl() + "/account/connections"); + + String address = page.locator("#mcp-url").inputValue(); + page.locator("#copy-mcp-url").click(); + assertThat(page.locator("#copy-mcp-url")).hasText("Copied"); + Object clipboard = page.evaluate("navigator.clipboard.readText()"); + if (!address.equals(clipboard)) { + throw new AssertionError("Clipboard has '" + clipboard + "', expected '" + address + "'"); + } } @Test diff --git a/src/browserTest/java/com/example/AviaryService/browser/ImportWithAiEntryTest.java b/src/browserTest/java/com/example/AviaryService/browser/ImportWithAiEntryTest.java new file mode 100644 index 0000000..439348e --- /dev/null +++ b/src/browserTest/java/com/example/AviaryService/browser/ImportWithAiEntryTest.java @@ -0,0 +1,102 @@ +package com.example.AviaryService.browser; + +import static com.microsoft.playwright.assertions.PlaywrightAssertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import com.example.AviaryService.entity.ServiceTimeline; +import com.example.AviaryService.repositories.ServiceTimelineRepository; +import com.microsoft.playwright.options.LoadState; + +/** + * Ways into the Import with AI page from the dashboard: the labeled header + * control (text on desktop, named icon on a phone) and the prompt shown only + * while the service timeline is empty. + */ +class ImportWithAiEntryTest extends BrowserTestBase { + + @Autowired + ServiceTimelineRepository timelineRepository; + + private void assertNoSidewaysScroll(int width) { + Object overflow = page.evaluate("document.documentElement.scrollWidth > window.innerWidth"); + if (Boolean.TRUE.equals(overflow)) { + throw new AssertionError("Dashboard scrolls horizontally at " + width + "px"); + } + } + + // ---- header control (US2) ---- + + @Test + void headerShowsLabeledControlOnDesktop() { + page.setViewportSize(1280, 800); + loginAs(seedUser("password123"), "password123"); + + assertThat(page.locator("#connections-link")).containsText("Import with AI"); + assertThat(page.locator("#connections-link .connections-link-text")).isVisible(); + page.locator("#connections-link").click(); + page.waitForURL(baseUrl() + "/account/connections"); + } + + @Test + void headerShowsNamedIconOnPhone() { + page.setViewportSize(390, 844); + loginAs(seedUser("password123"), "password123"); + page.waitForLoadState(LoadState.NETWORKIDLE); + + assertThat(page.locator("#connections-link")).isVisible(); + assertThat(page.locator("#connections-link")).hasAttribute("aria-label", "Import with AI"); + assertNoSidewaysScroll(390); + } + + // ---- empty-timeline prompt (US3) ---- + + @Test + void emptyTimelineShowsPrompt() { + page.setViewportSize(1280, 800); + loginAs(seedUser("password123"), "password123"); + + assertThat(page.locator("#import-ai-prompt")).isVisible(); + page.locator("#import-ai-prompt a").click(); + page.waitForURL(baseUrl() + "/account/connections"); + } + + @Test + void neitherControlPrints() { + page.setViewportSize(1280, 800); + loginAs(seedUser("password123"), "password123"); + page.emulateMedia(new com.microsoft.playwright.Page.EmulateMediaOptions() + .setMedia(com.microsoft.playwright.options.Media.PRINT)); + assertThat(page.locator("#import-ai-prompt")).isHidden(); + assertThat(page.locator("#connections-link")).isHidden(); + } + + @Test + void timelineWithItemsHasNoPrompt() { + page.setViewportSize(1280, 800); + String username = seedUser("password123"); + ServiceTimeline t = new ServiceTimeline(); + t.setUser(userRepository.findByUsername(username)); + t.setItem("Annual"); + t.setTimelineOrder(0); + timelineRepository.save(t); + loginAs(username, "password123"); + + assertThat(page.locator("#import-ai-prompt")).hasCount(0); + } + + @Test + void addingFirstRowRemovesPromptWithoutReload() { + page.setViewportSize(1280, 800); + loginAs(seedUser("password123"), "password123"); + page.waitForLoadState(LoadState.NETWORKIDLE); + assertThat(page.locator("#import-ai-prompt")).isVisible(); + + page.locator(".add-row td[data-label='Item'] textarea").fill("Oil change"); + page.locator("td.add-cell form button.add-button").click(); + + assertThat(page.locator(".sortable tr[data-id]")).hasCount(1); + assertThat(page.locator("#import-ai-prompt")).hasCount(0); + } +} diff --git a/src/main/java/com/example/AviaryService/controllers/ConnectionsController.java b/src/main/java/com/example/AviaryService/controllers/ConnectionsController.java index dda2221..597a85e 100644 --- a/src/main/java/com/example/AviaryService/controllers/ConnectionsController.java +++ b/src/main/java/com/example/AviaryService/controllers/ConnectionsController.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.NoSuchElementException; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -21,20 +22,25 @@ import com.example.AviaryService.repositories.UserRepository; import com.example.AviaryService.services.imports.ConnectionService; -// Account -> Connected assistants: the AI assistants signed in to this -// owner's account, with a Disconnect button for each. +// Import with AI: how to connect an AI assistant (this site's MCP address and +// steps), then the assistants signed in to this owner's account, each with a +// Disconnect button. @Controller public class ConnectionsController { private final ConnectionService connectionService; private final UserRepository userRepository; private final TimelineImportRecordRepository importRecordRepository; + // This site's MCP address -- production, staging, and local each show their own. + private final String mcpUrl; public ConnectionsController(ConnectionService connectionService, UserRepository userRepository, - TimelineImportRecordRepository importRecordRepository) { + TimelineImportRecordRepository importRecordRepository, + @Value("${aviary.oauth.issuer}") String issuer) { this.connectionService = connectionService; this.userRepository = userRepository; this.importRecordRepository = importRecordRepository; + this.mcpUrl = issuer + "/mcp"; } @GetMapping("/account/connections") @@ -49,6 +55,7 @@ public String connections(Principal principal, Model model) { } } model.addAttribute("imports", imports); + model.addAttribute("mcpUrl", mcpUrl); return "connections"; } diff --git a/src/main/resources/static/css/connectionsstyle.css b/src/main/resources/static/css/connectionsstyle.css index 70f48ed..efcd21f 100644 --- a/src/main/resources/static/css/connectionsstyle.css +++ b/src/main/resources/static/css/connectionsstyle.css @@ -98,3 +98,85 @@ .connections-back { color: var(--primary-dark); } + +/* Import with AI: connect box */ +#connect-box { + display: flex; + flex-direction: column; + gap: 12px; + padding: 16px; + border: 1px solid var(--border); + border-radius: var(--radius); + background: var(--primary-light); +} + +.connect-address { + display: flex; + gap: 8px; +} + +#mcp-url { + flex: 1; + min-width: 0; + padding: 10px; + border: 1px solid var(--border); + border-radius: var(--radius); + font: inherit; + font-size: 0.9rem; + background: var(--surface); + color: var(--text); +} + +#copy-mcp-url { + flex-shrink: 0; + padding: 10px 14px; + border: 1px solid var(--primary); + border-radius: var(--radius); + background: var(--primary); + color: #fff; + font: inherit; + font-weight: 600; + cursor: pointer; +} + +#copy-mcp-url:hover { + background: var(--primary-dark); +} + +#connect-steps { + padding-left: 20px; + display: flex; + flex-direction: column; + gap: 4px; +} + +.assistant-hints-title { + font-weight: 600; + font-size: 0.9rem; +} + +#assistant-hints { + padding-left: 20px; + color: var(--text-muted); + font-size: 0.85rem; + overflow-wrap: anywhere; +} + +#assistant-hints code { + font-size: 0.8rem; +} + +#connect-next { + font-size: 0.9rem; +} + +.connections-heading { + font-size: 1.05rem; + margin-top: 8px; +} + +@media (max-width: 420px) { + .connect-address { + flex-direction: column; + } +} diff --git a/src/main/resources/static/css/dashboardstyle.css b/src/main/resources/static/css/dashboardstyle.css index 2495186..800883c 100644 --- a/src/main/resources/static/css/dashboardstyle.css +++ b/src/main/resources/static/css/dashboardstyle.css @@ -182,8 +182,24 @@ input[type="checkbox"]:focus-visible { .fixed-header .header-right .logout-btn:hover, .fixed-header .share-btn:hover, .fixed-header #subscribe:hover { background: rgba(255,255,255,0.3); } -/* Connected assistants link: a link styled like the Logout button. */ +/* Import with AI link: a link styled like the Logout button. On narrow + phones only the robot icon shows (aria-label keeps the name). */ .fixed-header .header-right #connections-link { text-decoration: none; } +@media (max-width: 420px) { + .fixed-header .header-right #connections-link .connections-link-text { display: none; } +} + +/* Empty timeline: nudge new owners to import existing records. */ +#import-ai-prompt { + margin: 0 0 12px; + padding: 10px 14px; + border: 1px solid #D8E6DE; + border-radius: 8px; + background: #E6F4EE; + color: #5E7A6A; + font-size: 14px; +} +#import-ai-prompt a { color: #1E7A54; font-weight: 600; } .edit-hours-btn, .back-to-full-list-button { diff --git a/src/main/resources/static/js/connections.js b/src/main/resources/static/js/connections.js new file mode 100644 index 0000000..2985ed3 --- /dev/null +++ b/src/main/resources/static/js/connections.js @@ -0,0 +1,24 @@ +// Import with AI page: Copy button for the MCP address. Falls back to +// selecting the address when the browser won't allow clipboard access. +document.addEventListener('DOMContentLoaded', function () { + const button = document.getElementById('copy-mcp-url'); + const field = document.getElementById('mcp-url'); + if (!button || !field) return; + + function showFailure() { + button.textContent = 'Copy failed — select the address'; + field.focus(); + field.select(); + } + + button.addEventListener('click', function () { + if (!navigator.clipboard || !navigator.clipboard.writeText) { + showFailure(); + return; + } + navigator.clipboard.writeText(field.value).then(function () { + button.textContent = 'Copied'; + setTimeout(function () { button.textContent = 'Copy'; }, 2000); + }, showFailure); + }); +}); diff --git a/src/main/resources/static/js/dashboard.js b/src/main/resources/static/js/dashboard.js index f5ace69..48a34ae 100644 --- a/src/main/resources/static/js/dashboard.js +++ b/src/main/resources/static/js/dashboard.js @@ -2734,3 +2734,18 @@ document.addEventListener('DOMContentLoaded', () => { loadPrefs(); loadRecipients(); }); + +// Empty-timeline "Import with AI" prompt: remove it as soon as the timeline +// gets its first row, so it never sits above real items. +document.addEventListener('DOMContentLoaded', function () { + const prompt = document.getElementById('import-ai-prompt'); + const tbody = document.querySelector('.sortable'); + if (!prompt || !tbody) return; + const observer = new MutationObserver(function () { + if (tbody.querySelector('tr:not(.add-row)')) { + prompt.remove(); + observer.disconnect(); + } + }); + observer.observe(tbody, { childList: true }); +}); diff --git a/src/main/resources/templates/connections.html b/src/main/resources/templates/connections.html index 319c62d..a620949 100644 --- a/src/main/resources/templates/connections.html +++ b/src/main/resources/templates/connections.html @@ -4,7 +4,7 @@ - Connected assistants + Import with AI @@ -13,12 +13,35 @@
-

Connected assistants

+

Import with AI

+ +
+

+ Connect an AI assistant to bring in your existing maintenance records — + a spreadsheet, a typed list, or another tracker's export. +

+
+ + +
+
    +
  1. In your AI assistant, add a custom connector (also called a remote MCP server).
  2. +
  3. Paste the address above.
  4. +
  5. Sign in to Aviary when asked, then click Allow.
  6. +
+
+

+ Then paste your maintenance spreadsheet or list and ask it to import into Aviary. + You'll see a preview first — nothing is added until you say so. +

+
+ +

Connected assistants

Disconnected.

- No AI assistants are connected. Add Aviary to an assistant such as - an AI assistant to import your service timeline. + No assistants connected yet.

+ diff --git a/src/main/resources/templates/dashboard.html b/src/main/resources/templates/dashboard.html index 6e24069..6e9cfb1 100644 --- a/src/main/resources/templates/dashboard.html +++ b/src/main/resources/templates/dashboard.html @@ -52,7 +52,7 @@ + title="Import with AI" aria-label="Import with AI">Import with AI
@@ -260,6 +260,12 @@

My Hours

+ +

+ Have records in a spreadsheet? Import with AI +

+ diff --git a/src/main/resources/templates/fragments/assistant-hints.html b/src/main/resources/templates/fragments/assistant-hints.html new file mode 100644 index 0000000..d2f78f2 --- /dev/null +++ b/src/main/resources/templates/fragments/assistant-hints.html @@ -0,0 +1,19 @@ + + + + +
+

Where to find it

+
    +
  • Claude (web or desktop): Settings → Connectors → Add custom connector
  • +
  • Claude Code: run claude mcp add --transport http aviary <address>, then /mcp to sign in
  • +
+
+ + diff --git a/src/test/java/com/example/AviaryService/controllers/ConnectionsControllerTest.java b/src/test/java/com/example/AviaryService/controllers/ConnectionsControllerTest.java new file mode 100644 index 0000000..7e8d826 --- /dev/null +++ b/src/test/java/com/example/AviaryService/controllers/ConnectionsControllerTest.java @@ -0,0 +1,49 @@ +package com.example.AviaryService.controllers; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; + +import com.example.AviaryService.entity.User; +import com.example.AviaryService.repositories.UserRepository; + +@SpringBootTest +@AutoConfigureMockMvc +@ActiveProfiles("test") +@Transactional +class ConnectionsControllerTest { + + @Autowired MockMvc mvc; + @Autowired UserRepository userRepository; + + @Test + void pageShowsThisSitesMcpAddress() throws Exception { + User u = new User(); + u.setUsername("connect-" + System.nanoTime()); + u.setPassword("x"); + userRepository.save(u); + + mvc.perform(get("/account/connections").with(user(u.getUsername()))) + .andExpect(status().isOk()) + .andExpect(view().name("connections")) + .andExpect(model().attribute("mcpUrl", "http://localhost:8080/mcp")); + } + + @Test + void requiresLogin() throws Exception { + mvc.perform(get("/account/connections")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("**/login")); + } +}