Skip to content

A platform for community discussion. Free, open, simple.

A11Y: additional keyboard and screenreader improvements when Ask AI is enabled (#43739)

Kris837f9e81e026
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 3a749e58ea3..190a56e1df4 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -4093,6 +4093,9 @@ en:
full_page_title: "Search"
results: "results"
no_results: "No results found."
+ results_announcement:
+ one: "%{count} result found."
+ other: "%{count} results found."
no_more_results: "No more results found."
post_format: "#%{post_number} by %{username}"
results_page: "Search results for '%{term}'"
diff --git a/frontend/discourse/app/components/search-menu.gjs b/frontend/discourse/app/components/search-menu.gjs
index b17018f9402..204711dfb02 100644
--- a/frontend/discourse/app/components/search-menu.gjs
+++ b/frontend/discourse/app/components/search-menu.gjs
@@ -34,6 +34,7 @@ import DButton from "discourse/ui-kit/d-button";
import dConcatClass from "discourse/ui-kit/helpers/d-concat-class";
import dLoadingSpinner from "discourse/ui-kit/helpers/d-loading-spinner";
import { CANCELLED_STATUS } from "discourse/ui-kit/modifiers/d-autocomplete";
+import { i18n } from "discourse-i18n";
const CATEGORY_SLUG_REGEXP = /(\#[a-zA-Z0-9\-:]*)$/gi;
const USERNAME_REGEXP = /(\@[a-zA-Z0-9\-\_]*)$/gi;
@@ -46,6 +47,7 @@ export default class SearchMenu extends Component {
@service currentUser;
@service siteSettings;
@service appEvents;
+ @service a11y;
@tracked loading = false;
@tracked isPMInboxCleared = false;
@@ -354,6 +356,7 @@ export default class SearchMenu extends Component {
}
this.suggestionKeyword = false;
+ const announceOutcome = this.typeFilter !== DEFAULT_TYPE_FILTER;
if (!this.search.activeGlobalSearchTerm) {
this.search.noResults = false;
@@ -367,6 +370,10 @@ export default class SearchMenu extends Component {
this.search.results = {};
this.loading = false;
this.invalidTerm = true;
+
+ if (announceOutcome) {
+ this.a11y.announce(i18n("search.too_short"), "polite");
+ }
} else {
this.loading = true;
this.invalidTerm = false;
@@ -387,6 +394,10 @@ export default class SearchMenu extends Component {
) {
this.search.noResults = results.resultTypes.length === 0;
this.search.results = results;
+
+ if (announceOutcome) {
+ this.#announceResults(results);
+ }
}
})
.catch(popupAjaxError)
@@ -450,6 +461,20 @@ export default class SearchMenu extends Component {
}
}
+ #announceResults(results) {
+ const count = results.resultTypes.reduce(
+ (total, type) => total + type.results.length,
+ 0
+ );
+
+ this.a11y.announce(
+ count
+ ? i18n("search.results_announcement", { count })
+ : i18n("search.no_results"),
+ "polite"
+ );
+ }
+
<template>
{{! eslint-disable ember/template-no-invalid-interactive }}
<div
diff --git a/frontend/discourse/app/services/search.js b/frontend/discourse/app/services/search.js
index d28809f7531..2342425c611 100644
--- a/frontend/discourse/app/services/search.js
+++ b/frontend/discourse/app/services/search.js
@@ -144,7 +144,18 @@ export default class Search extends Service {
) || navigationItems[0];
firstResult.focus();
} else if (index === 0 && e.key === "ArrowUp") {
- this.focusSearchInput();
+ // the menu's own input, since the one the search experience names
+ // isn't rendered everywhere (e.g. a scrolled topic swaps the header field
+ // for the topic title and search opens from the icon instead)
+ const input = focused
+ .closest(".search-menu")
+ .querySelector(".search-term__input");
+
+ if (input) {
+ input.focus();
+ } else {
+ this.focusSearchInput();
+ }
} else if (index > -1) {
// change focus to the next navigation item if present
index += e.key === "ArrowDown" ? 1 : -1;
diff --git a/frontend/discourse/app/ui-kit/d-toggle-switch.gjs b/frontend/discourse/app/ui-kit/d-toggle-switch.gjs
index 6331553c7ec..c18718cf021 100644
--- a/frontend/discourse/app/ui-kit/d-toggle-switch.gjs
+++ b/frontend/discourse/app/ui-kit/d-toggle-switch.gjs
@@ -1,8 +1,11 @@
import Component from "@glimmer/component";
+import { guidFor } from "@ember/object/internals";
import dIcon from "discourse/ui-kit/helpers/d-icon";
import { i18n } from "discourse-i18n";
export default class DToggleSwitch extends Component {
+ labelId = `${guidFor(this)}-label`;
+
get computedLabel() {
if (this.args.label) {
return i18n(this.args.label);
@@ -15,6 +18,7 @@ export default class DToggleSwitch extends Component {
<label class="d-toggle-switch__label">
<button
aria-checked={{if @state "true" "false"}}
+ aria-labelledby={{if this.computedLabel this.labelId}}
class="d-toggle-switch__checkbox"
role="switch"
type="button"
@@ -29,7 +33,7 @@ export default class DToggleSwitch extends Component {
</label>
{{#if this.computedLabel}}
- <span class="d-toggle-switch__checkbox-label">
+ <span class="d-toggle-switch__checkbox-label" id={{this.labelId}}>
{{this.computedLabel}}
</span>
{{/if}}
diff --git a/frontend/discourse/tests/integration/components/search-menu-test.gjs b/frontend/discourse/tests/integration/components/search-menu-test.gjs
index af48bf66cdb..4b3c4dbae71 100644
--- a/frontend/discourse/tests/integration/components/search-menu-test.gjs
+++ b/frontend/discourse/tests/integration/components/search-menu-test.gjs
@@ -1,4 +1,5 @@
import { tracked } from "@glimmer/tracking";
+import Service from "@ember/service";
import {
click,
fillIn,
@@ -252,4 +253,71 @@ module("Integration | Component | SearchMenu", function (hooks) {
"PM context button reappears after selecting 'in:messages' suggestion"
);
});
+ test("announces the outcome of a submitted search, but not suggestions", async function (assert) {
+ const announcements = [];
+ this.owner.register(
+ "service:a11y",
+ class extends Service {
+ announce(message, priority) {
+ announcements.push({ message, priority });
+ }
+ }
+ );
+
+ pretender.get("/search/query", (request) => {
+ if (request.queryParams.term === "nothing") {
+ return response({ grouped_search_result: {} });
+ }
+ if (request.queryParams.type_filter === DEFAULT_TYPE_FILTER) {
+ return response({
+ users: searchFixtures["search/query"]["users"],
+ grouped_search_result:
+ searchFixtures["search/query"]["grouped_search_result"],
+ });
+ }
+ return response(searchFixtures["search/query"]);
+ });
+
+ await render(
+ <template>
+ <SearchMenu @location="test" @searchInputId="icon-search-input" />
+ </template>
+ );
+
+ await click("#icon-search-input");
+ await fillIn("#icon-search-input", "test");
+
+ assert.deepEqual(announcements, [], "suggestions while typing are silent");
+
+ await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
+
+ assert.strictEqual(
+ announcements.length,
+ 1,
+ "a submitted search is announced"
+ );
+ assert.strictEqual(announcements[0].priority, "polite");
+ assert.true(
+ /^\d+ results? found\.$/.test(announcements[0].message),
+ "with the number of results shown"
+ );
+
+ await fillIn("#icon-search-input", "nothing");
+ await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
+
+ assert.deepEqual(
+ announcements.at(-1),
+ { message: i18n("search.no_results"), priority: "polite" },
+ "an empty search says so"
+ );
+
+ await fillIn("#icon-search-input", "a");
+ await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
+
+ assert.deepEqual(
+ announcements.at(-1),
+ { message: i18n("search.too_short"), priority: "polite" },
+ "a term too short to search says why"
+ );
+ });
});
diff --git a/frontend/discourse/tests/integration/ui-kit/d-toggle-switch-test.gjs b/frontend/discourse/tests/integration/ui-kit/d-toggle-switch-test.gjs
index dfc485e13f2..7afed9cd2cf 100644
--- a/frontend/discourse/tests/integration/ui-kit/d-toggle-switch-test.gjs
+++ b/frontend/discourse/tests/integration/ui-kit/d-toggle-switch-test.gjs
@@ -1,4 +1,4 @@
-import { render } from "@ember/test-helpers";
+import { find, render } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import DToggleSwitch from "discourse/ui-kit/d-toggle-switch";
@@ -52,4 +52,28 @@ module("Integration | ui-kit | DToggleSwitch", function (hooks) {
assert.dom(".d-toggle-switch__checkbox-label").hasText("bar");
});
+
+ test("it names the switch with its label", async function (assert) {
+ await render(
+ <template>
+ <DToggleSwitch @state={{false}} @translatedLabel="bar" />
+ </template>
+ );
+
+ assert
+ .dom(".d-toggle-switch__checkbox")
+ .hasAria(
+ "labelledby",
+ find(".d-toggle-switch__checkbox-label").id,
+ "the switch is announced with its visible label"
+ );
+ });
+
+ test("it leaves an unlabelled switch to its caller", async function (assert) {
+ await render(<template><DToggleSwitch @state={{false}} /></template>);
+
+ assert
+ .dom(".d-toggle-switch__checkbox")
+ .doesNotHaveAttribute("aria-labelledby");
+ });
});
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs
index 53d63374655..19f5a50f86e 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-discoveries-search-options.gjs
@@ -375,6 +375,7 @@ export default class AiDiscoveriesSearchOptions extends Component {
{{if option.active 'is-active'}}"
data-search-menu-navigation-item
@action={{option.action}}
+ @ariaPressed={{option.active}}
@icon={{option.icon}}
@label={{option.label}}
@translatedLabel={{option.translatedLabel}}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs
index 413dad337c0..aadfdb9ade0 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs
+++ b/plugins/discourse-ai/assets/javascripts/discourse/components/ai-search-discoveries.gjs
@@ -179,6 +179,12 @@ export default class AiSearchDiscoveries extends Component {
return this.siteSettings.ai_ask_ai_summary_detail !== "quiet";
}
+ get answerLabel() {
+ return this.showAnswerTitle && this.discobotDiscoveries.discoveryTitle
+ ? this.discobotDiscoveries.discoveryTitle
+ : i18n("discourse_ai.discobot_discoveries.answer_label");
+ }
+
get relatedCount() {
return this.siteSettings.ai_ask_ai_related_count;
}
@@ -250,10 +256,14 @@ export default class AiSearchDiscoveries extends Component {
return this.discobotDiscoveries.suggestedFollowUp || "";
}
+ // offered once an answer has settled, so it isn't what a screen reader meets
+ // while waiting for one
get showAskAiDefaultToggle() {
return (
Boolean(this.currentUser) &&
!this.hasNoContent &&
+ !this.discobotDiscoveries.loadingDiscoveries &&
+ !this.discobotDiscoveries.isStreaming &&
!this.askAiDefaultDismissed
);
}
@@ -484,13 +494,18 @@ export default class AiSearchDiscoveries extends Component {
{{else if this.discobotDiscoveries.discoveryTimedOut}}
{{i18n "discourse_ai.discobot_discoveries.timed_out"}}
{{else if this.noAnswer}}
- <div class="ai-search-discoveries__no-answer">
+ {{! eslint-disable-next-line ember/template-no-invalid-interactive }}
+ <div
+ class="ai-search-discoveries__no-answer"
+ {{on "keydown" this.search.handleArrowUpOrDown}}
+ >
<p class="ai-search-discoveries__no-answer-message">
{{i18n "discourse_ai.discobot_discoveries.no_answer"}}
</p>
{{#if this.currentUser.can_create_topic}}
<DButton
class="btn-primary btn-small ai-search-discoveries__create-topic"
+ data-search-menu-navigation-item
@action={{this.createTopic}}
@label="discourse_ai.discobot_discoveries.create_topic"
/>
@@ -498,13 +513,21 @@ export default class AiSearchDiscoveries extends Component {
</div>
{{else}}
{{! eslint-disable ember/template-no-invalid-interactive }}
+ {{! a stop in the search menu's arrow and tab order, rather than only
+ reachable by a screen reader's virtual cursor; on the full page it
+ is already in reading order }}
<article
+ aria-busy={{if this.discobotDiscoveries.isStreaming "true"}}
+ aria-label={{this.answerLabel}}
class={{dConcatClass
"ai-search-discoveries__discovery"
(if this.discobotDiscoveries.isStreaming "streaming")
"streamable-content"
}}
+ data-search-menu-navigation-item={{unless @fullPage true}}
+ tabindex={{unless @fullPage "0"}}
{{on "click" this.handleDiscoveryClick}}
+ {{on "keydown" this.search.handleArrowUpOrDown}}
>
<DCookText
class="cooked"
@@ -547,10 +570,15 @@ export default class AiSearchDiscoveries extends Component {
<ul
class="ai-discovery-sources__list"
{{on "click" this.handleDiscoveryClick}}
+ {{on "keydown" this.search.handleArrowUpOrDown}}
>
{{#each this.visibleSources as |source|}}
<li class="ai-discovery-sources__item">
- <a class="ai-discovery-source" href={{source.url}}>
+ <a
+ class="ai-discovery-source"
+ data-search-menu-navigation-item
+ href={{source.url}}
+ >
{{#if (and this.showSourceAvatars source.avatar_template)}}
<span class="ai-discovery-source__avatar">
{{dAvatar source imageSize="medium"}}
@@ -599,6 +627,7 @@ export default class AiSearchDiscoveries extends Component {
"discourse_ai.discobot_discoveries.follow_up.label"
}}
class="ai-search-discoveries__follow-up-input"
+ data-search-menu-navigation-item
disabled={{this.loadingConversationTopic}}
maxlength="1000"
placeholder={{i18n
@@ -608,6 +637,7 @@ export default class AiSearchDiscoveries extends Component {
value={{this.followUpValue}}
{{on "focus" this.clearSuggestedFollowUp}}
{{on "input" this.updateFollowUpQuestion}}
+ {{on "keydown" this.search.handleArrowUpOrDown}}
/>
<DButton
class="btn-primary btn-small ai-search-discoveries__follow-up-submit"
@@ -627,15 +657,19 @@ export default class AiSearchDiscoveries extends Component {
<div class="ai-search-discoveries__default-preference">
<DToggleSwitch
class="ai-search-discoveries__default-toggle"
+ data-search-menu-navigation-item
@label="discourse_ai.discobot_discoveries.make_default"
@state={{this.askAiIsDefault}}
{{on "click" this.toggleAskAiDefault}}
+ {{on "keydown" this.search.handleArrowUpOrDown}}
/>
<DButton
class="btn-transparent ai-search-discoveries__dismiss-default"
+ data-search-menu-navigation-item
@action={{this.dismissAskAiDefaultToggle}}
@icon="xmark"
@title="discourse_ai.discobot_discoveries.dismiss_default_preference"
+ {{on "keydown" this.search.handleArrowUpOrDown}}
/>
</div>
{{/if}}
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js
index d5e76db3eaa..a1689afbbab 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/initializers/ai-search-discoveries.js
@@ -257,6 +257,13 @@ export default apiInitializer((api) => {
offersDiscoveries(context?.location) ? [...value, "--with-ask-ai"] : value
);
+ // the options row always shows which scope is selected
+ api.registerValueTransformer(
+ "search-menu-search-context-enabled",
+ ({ value, context }) =>
+ offersDiscoveries(context?.location) ? false : value
+ );
+
// advanced search is offered in the options row instead
api.registerValueTransformer(
"search-menu-advanced-button-enabled",
diff --git a/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js b/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js
index 02d5ec48dd7..ec4b543d52a 100644
--- a/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js
+++ b/plugins/discourse-ai/assets/javascripts/discourse/services/discobot-discoveries.js
@@ -218,6 +218,10 @@ export default class DiscobotDiscoveries extends Service {
const requestId = buildRequestId();
this.loadingDiscoveries = true;
this.activeRequestId = requestId;
+ this.a11y.announce(
+ i18n("discourse_ai.discobot_discoveries.asking"),
+ "polite"
+ );
this.scheduleDiscoveryTimeout();
diff --git a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss
index bcc0c84d21f..ff9ec97c978 100644
--- a/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss
+++ b/plugins/discourse-ai/assets/stylesheets/modules/ai-bot/common/ai-discobot-discoveries.scss
@@ -56,6 +56,12 @@
color: var(--token-color-text-default);
line-height: var(--line-height-medium);
+ &:focus-visible {
+ outline: 2px solid var(--tertiary);
+ outline-offset: var(--space-1);
+ border-radius: var(--token-radius-normal);
+ }
+
.cooked > :last-child {
margin-bottom: 0;
}
@@ -246,22 +252,23 @@
.d-button-label {
@include ellipsis;
}
+ }
+
+ & &__option.is-active {
+ border-color: var(--tertiary);
+ background: var(--d-selected);
+ color: var(--d-selected-text-color);
- &.is-active {
+ &:hover,
+ &:focus-visible {
border-color: var(--tertiary);
background: var(--d-selected);
color: var(--d-selected-text-color);
+ }
- &:hover,
- &:focus-visible {
- border-color: var(--tertiary);
- background: var(--d-selected);
- color: var(--d-selected-text-color);
- }
-
- &:focus-visible {
- @include default-focus;
- }
+ &:focus-visible {
+ outline: 2px solid var(--d-input-focused-color);
+ outline-offset: 2px;
}
}
diff --git a/plugins/discourse-ai/config/locales/client.en.yml b/plugins/discourse-ai/config/locales/client.en.yml
index a3d297a435f..93126934dbb 100644
--- a/plugins/discourse-ai/config/locales/client.en.yml
+++ b/plugins/discourse-ai/config/locales/client.en.yml
@@ -1714,6 +1714,8 @@ en:
request_failed: "Ask AI could not start this search. Try again or use Search."
no_answer: "AI couldn’t find a clear answer. Ask the community."
create_topic: "Create topic"
+ asking: "Asking AI…"
+ answer_label: "AI answer"
answer_ready: "Ask AI answer ready."
user_setting: "Enable AI search discoveries"
legacy:
diff --git a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js
index b8b36c7f93d..c3df0da4fca 100644
--- a/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js
+++ b/plugins/discourse-ai/test/javascripts/acceptance/ai-search-discoveries-test.js
@@ -5,6 +5,7 @@ import {
fillIn,
find,
findAll,
+ focus,
settled,
triggerKeyEvent,
visit,
@@ -474,13 +475,18 @@ acceptance("AI Discoveries - header search", function (needs) {
.exists("all topics is offered");
await click(".ai-discoveries-search-options__option.--topic");
assert
- .dom(".search-context")
- .exists("the topic chip appears before typing");
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .hasClass("is-active", "the topic scope is selected before typing");
+ assert
+ .dom(".search-menu .search-context")
+ .doesNotExist("the options show the scope, so the input carries no chip");
assert.dom("#icon-search-input").isFocused("the query is ready for typing");
await fillIn("#icon-search-input", "雰囲気");
await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
- assert.dom(".search-context").exists("typing preserves the selected topic");
+ assert
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .hasClass("is-active", "typing preserves the selected topic");
assert
.dom(".search-result-post")
.exists("the query returns posts within the topic");
@@ -508,9 +514,6 @@ acceptance("AI Discoveries - header search", function (needs) {
await click(".ai-discoveries-search-options__option.--topic");
- assert
- .dom(".search-menu .search-context")
- .exists("the input shows the removable topic scope");
assert
.dom(".ai-discoveries-search-options__option.--topic")
.hasClass("is-active", "the inline option also shows the selected scope");
@@ -524,7 +527,6 @@ acceptance("AI Discoveries - header search", function (needs) {
assert
.dom(".ai-discoveries-search-options__option.--topic")
.hasClass("is-active", "editing keeps the topic scope selected");
- assert.dom(".search-menu .search-context").exists("the chip stays visible");
await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
assert.strictEqual(
@@ -533,22 +535,11 @@ acceptance("AI Discoveries - header search", function (needs) {
"the edited query still searches within the topic"
);
- await click(".search-menu .search-context");
- assert
- .dom(".search-menu .search-context")
- .doesNotExist("the chip removes the scope");
+ await click(".ai-discoveries-search-options__option.--search");
+
assert
.dom("#icon-search-input")
- .hasValue("dev tooling 猫", "removing the scope keeps the query");
- assert
- .dom(".ai-discoveries-search-options__option.--topic")
- .doesNotHaveClass(
- "is-active",
- "removing the chip deselects the topic option"
- );
-
- await click(".ai-discoveries-search-options__option.--topic");
- await click(".ai-discoveries-search-options__option.--search");
+ .hasValue("dev tooling 猫", "widening the scope keeps the query");
assert
.dom(".ai-discoveries-search-options__option.--topic")
@@ -567,8 +558,8 @@ acceptance("AI Discoveries - header search", function (needs) {
await triggerKeyEvent(document.activeElement, "keyup", "Enter");
assert
- .dom(".search-menu .search-context")
- .exists("the topic scope is selected");
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .hasClass("is-active", "the topic scope is selected");
assert
.dom("#icon-search-input")
.isFocused("the short query is ready to edit");
@@ -584,8 +575,8 @@ acceptance("AI Discoveries - header search", function (needs) {
assert.strictEqual(discoveryRequests, 0, "Enter does not switch to Ask AI");
assert
- .dom(".search-menu .search-context")
- .exists("the corrected search stays scoped");
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .hasClass("is-active", "the corrected search stays scoped");
assert.dom(".search-result-post").exists("matching posts are displayed");
assert.dom("#icon-search-input").isFocused("the query remains editable");
});
@@ -601,13 +592,15 @@ acceptance("AI Discoveries - header search", function (needs) {
await fillIn("#icon-search-input", term);
await triggerKeyEvent("#icon-search-input", "keyup", "Backspace");
assert
- .dom(".search-context")
- .exists("deleting query text keeps the topic chip");
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .hasClass("is-active", "deleting query text keeps the topic scope");
}
await fillIn("#icon-search-input", "猫の雰囲気");
await triggerKeyEvent("#icon-search-input", "keyup", "Enter");
- assert.dom(".search-context").exists("the replacement query stays scoped");
+ assert
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .hasClass("is-active", "the replacement query stays scoped");
assert
.dom(".search-result-post")
.exists("the replacement query searches the topic");
@@ -616,8 +609,11 @@ acceptance("AI Discoveries - header search", function (needs) {
await triggerKeyEvent("#icon-search-input", "keydown", "Backspace");
await triggerKeyEvent("#icon-search-input", "keyup", "Backspace");
assert
- .dom(".search-context")
- .doesNotExist("Backspace in an already empty box removes the chip");
+ .dom(".ai-discoveries-search-options__option.--topic")
+ .doesNotHaveClass(
+ "is-active",
+ "Backspace in an already empty box releases the scope"
+ );
});
test("reopening search after selecting a post keeps the topic scope and results", async function (assert) {
@@ -648,7 +644,6 @@ acceptance("AI Discoveries - header search", function (needs) {
await triggerKeyEvent(document, "keypress", "/".charCodeAt(0));
assert.dom("#icon-search-input").hasValue("dev", "the query is restored");
- assert.dom(".search-context").exists("the topic chip is restored");
assert
.dom(".ai-discoveries-search-options__option.--topic")
.hasClass("is-active", "the topic option still matches the results");
@@ -661,9 +656,10 @@ acceptance("AI Discoveries - header search", function (needs) {
await triggerKeyEvent("#icon-search-input", "keydown", "Escape");
await visit("/latest");
await triggerKeyEvent(document, "keypress", "/".charCodeAt(0));
- assert
- .dom(".search-context")
- .doesNotExist("leaving the topic clears its scope");
+ assert.false(
+ getOwner(this).lookup("service:search").inTopicContext,
+ "leaving the topic clears its scope"
+ );
assert
.dom(".search-result-post")
.doesNotExist("the old topic's posts are no longer shown");
@@ -699,7 +695,10 @@ acceptance("AI Discoveries - header search", function (needs) {
await Promise.all([selectTopic, closeSearch, navigateAway]);
await triggerKeyEvent(document, "keypress", "/".charCodeAt(0));
- assert.dom(".search-context").doesNotExist("the topic scope is cleared");
+ assert.false(
+ getOwner(this).lookup("service:search").inTopicContext,
+ "the topic scope is cleared"
+ );
assert
.dom(".ai-discoveries-search-options__option.--search")
.exists("all topics remains available");
@@ -822,6 +821,64 @@ acceptance("AI Discoveries - header search", function (needs) {
});
});
+ test("the answer and its sources are reachable with the arrow keys", async function (assert) {
+ await visit("/");
+ await click("#search-button");
+ await fillIn("#icon-search-input", "dev");
+ find(".ai-discoveries-search-options__option.--ask").dispatchEvent(
+ new MouseEvent("click", { bubbles: true })
+ );
+ await waitFor(".ai-discobot-discoveries");
+ await waitUntil(() => submittedRequestId);
+ await publishToMessageBus("/discourse-ai/discoveries", {
+ request_id: submittedRequestId,
+ query: "dev",
+ ai_discover_reply: "An answer.",
+ sources: [
+ {
+ url: "/t/internationalization-localization/280",
+ title: "Internationalization / localization",
+ },
+ ],
+ answerable: true,
+ done: true,
+ });
+ await waitFor(".ai-discovery-source");
+ await waitFor(".ai-search-discoveries__default-toggle");
+
+ assert
+ .dom(".ai-search-discoveries__discovery")
+ .hasAria(
+ "label",
+ i18n("discourse_ai.discobot_discoveries.answer_label"),
+ "the answer is named"
+ );
+
+ await focus(".ai-discoveries-search-options__option.--ask");
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert
+ .dom(".ai-search-discoveries__discovery")
+ .isFocused("down from the options lands on the answer");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert
+ .dom(".ai-discovery-source")
+ .isFocused("and continues into its sources");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowDown");
+ assert
+ .dom(".ai-search-discoveries__default-toggle")
+ .isFocused("then on to the controls beneath the answer");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ await triggerKeyEvent(document.activeElement, "keydown", "ArrowUp");
+ assert
+ .dom(".ai-discoveries-search-options__option.--ask")
+ .isFocused("up retraces the way back to the options");
+ });
+
test("the toggle beside an answer saves the preference on the spot", async function (assert) {
let saved;
pretender.put("/u/eviltrout.json", (request) => {
@@ -842,6 +899,11 @@ acceptance("AI Discoveries - header search", function (needs) {
);
await waitFor(".ai-discobot-discoveries");
await waitUntil(() => submittedRequestId);
+
+ assert
+ .dom(".ai-search-discoveries__default-toggle")
+ .doesNotExist("the preference waits until there is an answer to judge");
+
await publishToMessageBus("/discourse-ai/discoveries", {
request_id: submittedRequestId,
query: "dev",
@@ -850,6 +912,16 @@ acceptance("AI Discoveries - header search", function (needs) {
done: true,
});
+ assert
+ .dom(".ai-search-discoveries__default-toggle")
+ .hasAria(
+ "labelledby",
+ find(
+ ".ai-search-discoveries__default-preference .d-toggle-switch__checkbox-label"
+ ).id,
+ "and is named by its label"
+ );
+
await click(".ai-search-discoveries__default-toggle");
assert.strictEqual(
diff --git a/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs b/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs
index 0fe730d53ff..38fefa3efd3 100644
--- a/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs
+++ b/plugins/discourse-ai/test/javascripts/integration/components/ai-discoveries-search-options-test.gjs
@@ -313,6 +313,18 @@ module(
"is-active",
"the indexed search is what the menu is showing"
);
+ assert
+ .dom(".ai-discoveries-search-options__option.--search")
+ .hasAttribute("aria-pressed", "true", "and reads as pressed");
+ assert
+ .dom(".ai-discoveries-search-options__option.--ask")
+ .hasAttribute("aria-pressed", "false", "while the others do not");
+ assert
+ .dom(".ai-discoveries-search-options__option.--advanced")
+ .doesNotHaveAttribute(
+ "aria-pressed",
+ "advanced search is an action rather than a choice"
+ );
assert
.dom(".ai-discoveries-search-options__option.--advanced")
.exists("advanced search is available for all topics");
@@ -336,6 +348,9 @@ module(
assert
.dom(".ai-discoveries-search-options__option.--ask")
.hasClass("is-active", "asking takes over once it owns the term");
+ assert
+ .dom(".ai-discoveries-search-options__option.--ask")
+ .hasAttribute("aria-pressed", "true", "and is the one pressed");
assert
.dom(".ai-discoveries-search-options__option.--search")
.doesNotHaveClass("is-active", "and the other option steps back");
diff --git a/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js b/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
index aae3a966144..9bbd556a5de 100644
--- a/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
+++ b/plugins/discourse-ai/test/javascripts/unit/services/discobot-discoveries-test.js
@@ -237,6 +237,32 @@ module("Unit | Service | discobot-discoveries", function (hooks) {
);
});
+ test("announces that asking has started", async function (assert) {
+ const announcements = [];
+ this.owner.register(
+ "service:a11y",
+ class extends Service {
+ announce(message, priority) {
+ announcements.push({ message, priority });
+ }
+ }
+ );
+ pretender.post("/discourse-ai/discoveries/reply", (request) =>
+ response(200, {
+ request_id: new URLSearchParams(request.requestBody).get("request_id"),
+ })
+ );
+ const service = getOwner(this).lookup("service:discobot-discoveries");
+
+ await service.triggerDiscovery("What is Discourse?");
+
+ assert.deepEqual(
+ announcements,
+ [{ message: "Asking AI…", priority: "polite" }],
+ "the wait for an answer is not silent"
+ );
+ });
+
test("announces a completed answer", async function (assert) {
const announcements = [];
this.owner.register(