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
235 changes: 235 additions & 0 deletions .github/scripts/keys-usage-layout-browser.mjs

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions .github/workflows/keys-usage-layout.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Keys usage layout

on:
push:
branches: [fix/keys-usage-layout-20261108]
pull_request:
paths:
- 'frontend/src/views/user/KeysView.vue'
- 'frontend/src/components/common/DataTable.vue'
- 'frontend/src/components/common/GroupBadge.vue'
- 'frontend/src/components/layout/TablePageLayout.vue'
- 'frontend/src/features/ip-audit/UserKeyRiskNotices.vue'
- 'frontend/src/features/ip-audit/__tests__/UserKeyRiskNotices.spec.ts'
- 'frontend/src/views/user/__tests__/KeysView.spec.ts'
- 'frontend/src/i18n/locales/*/ipAudit.ts'
- '.github/scripts/keys-usage-layout-browser.mjs'
- '.github/workflows/keys-usage-layout.yml'

permissions:
contents: read

concurrency:
group: keys-usage-layout-${{ github.event.pull_request.head.ref || github.ref_name }}
cancel-in-progress: true

jobs:
browser:
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@v6
with:
ref: ${{ github.event.pull_request.head.sha || github.sha }}
fetch-depth: 0
- uses: pnpm/action-setup@v6
with:
version: 9
- uses: actions/setup-node@v6
with:
node-version: '22'
cache: pnpm
cache-dependency-path: frontend/pnpm-lock.yaml
- run: pnpm --dir frontend install --frozen-lockfile
- name: Keys page and risk notice regressions
run: pnpm --dir frontend exec vitest run src/views/user/__tests__/KeysView.spec.ts src/features/ip-audit/__tests__/UserKeyRiskNotices.spec.ts
- name: Build production frontend
run: pnpm --dir frontend run build
- name: Install isolated CI browser
run: |
npm install --prefix "$RUNNER_TEMP/keys-layout-browser" --no-save --ignore-scripts playwright@1.56.1
"$RUNNER_TEMP/keys-layout-browser/node_modules/.bin/playwright" install --with-deps chromium
sudo apt-get install -y fonts-noto-cjk
- name: Render actual Keys page with synthetic data
env:
KEYS_LAYOUT_EXPECTED_HEAD: ${{ github.event.pull_request.head.sha || github.sha }}
KEYS_LAYOUT_SCREENSHOT_DIR: ${{ runner.temp }}/keys-layout-screenshots
PLAYWRIGHT_ROOT: ${{ runner.temp }}/keys-layout-browser/node_modules/playwright
run: node .github/scripts/keys-usage-layout-browser.mjs
- uses: actions/upload-artifact@v7
if: always()
with:
name: keys-layout-${{ github.event.pull_request.head.sha || github.sha }}
path: ${{ runner.temp }}/keys-layout-screenshots
91 changes: 59 additions & 32 deletions frontend/src/features/ip-audit/UserKeyRiskNotices.vue
Original file line number Diff line number Diff line change
@@ -1,46 +1,73 @@
<template>
<section class="rounded-lg border border-amber-200 bg-amber-50/40 p-4 dark:border-amber-900 dark:bg-amber-950/20" aria-labelledby="key-risk-notices-title">
<div class="flex items-center justify-between gap-3">
<h2 id="key-risk-notices-title" class="font-medium">{{ t('ipAudit.keyRisk.userTitle') }}</h2>
<button class="btn btn-secondary btn-sm" :disabled="busy" @click="load(0)">{{ t('common.refresh') }}</button>
</div>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-300">{{ t('ipAudit.keyRisk.userHint') }}</p>
<p v-if="error" role="alert" class="mt-2 text-sm text-red-600">{{ t('ipAudit.keyRisk.error') }}</p>
<p v-if="message" role="status" class="mt-2 text-sm">{{ message }}</p>
<p v-if="!busy && !error && !items.length" class="mt-2 text-sm">{{ t('ipAudit.keyRisk.noNotices') }}</p>
<ul class="mt-3 space-y-3">
<li v-for="item in items" :key="`${item.event_id}-${item.generation}`" class="rounded-lg border border-gray-200 bg-white p-3 dark:border-dark-600 dark:bg-dark-800">
<p class="font-medium">{{ t(`ipAudit.keyRisk.${item.rule}`) }} · #{{ item.event_id }}</p>
<div class="my-1 space-y-1 text-sm"><p class="text-xs text-gray-500">{{ t('ipAudit.keyRisk.keys') }}</p><div v-for="id in item.key_ids" :key="id"><RiskIdentity :id="id" :name="item.keys?.find(key => key.id === id)?.name" /></div><p v-if="!item.key_ids.length">—</p></div>
<p class="text-sm text-gray-500">{{ new Date(item.last_seen).toLocaleString() }} · {{ t('ipAudit.keyRisk.attempts', { count: item.attempts, max: item.max_reminders }) }}</p>
<p v-if="item.acknowledged_at" class="mt-2 text-sm" role="status">{{ t('ipAudit.keyRisk.acknowledged') }} · {{ new Date(item.acknowledged_at).toLocaleString() }}</p>
<p v-else-if="item.state !== 'pending'" class="mt-2 text-sm">{{ t(`ipAudit.keyRisk.${item.state}`) }}</p>
<template v-else>
<p v-if="item.attempts >= item.max_reminders" class="mt-2 text-sm">{{ t('ipAudit.keyRisk.limitReached') }}</p>
<button class="btn btn-secondary btn-sm mt-2" :disabled="busy" @click="confirming = item">{{ t('ipAudit.keyRisk.acknowledge') }}</button>
</template>
<div v-if="confirming?.event_id === item.event_id" class="mt-3 space-y-2 rounded border border-amber-300 p-3" role="group" :aria-label="t('ipAudit.keyRisk.acknowledge')">
<p class="text-sm">{{ t('ipAudit.keyRisk.ackConfirm') }}</p>
<div class="flex flex-wrap gap-2">
<button class="btn btn-primary btn-sm" :disabled="busy" data-test="confirm-risk" @click="acknowledge(item)">{{ t('ipAudit.keyRisk.ackSubmit') }}</button>
<button class="btn btn-secondary btn-sm" :disabled="busy" @click="confirming = null">{{ t('common.cancel') }}</button>
<section class="min-w-0 rounded-lg border border-amber-200 bg-amber-50/40 dark:border-amber-900 dark:bg-amber-950/20" :aria-labelledby="titleId">
<h2 :id="titleId">
<button
type="button"
class="flex w-full min-w-0 items-center justify-between gap-3 rounded-lg px-4 py-2 text-left text-sm"
:aria-expanded="expanded"
:aria-controls="detailsId"
data-test="toggle-risk-notices"
@click="expanded = !expanded"
>
<span class="min-w-0 truncate">
<span class="font-medium">{{ t('ipAudit.keyRisk.userTitle') }}</span>
<span v-if="error" class="ml-2 text-red-600">{{ t('ipAudit.keyRisk.error') }}</span>
<span v-else class="ml-2 text-gray-500 dark:text-gray-400">{{ t('ipAudit.keyRisk.noticeCount', { count: items.length }) }}</span>
</span>
<span class="shrink-0 text-xs text-gray-600 dark:text-gray-300">{{ t(expanded ? 'ipAudit.keyRisk.collapseNotices' : 'ipAudit.keyRisk.expandNotices') }}</span>
</button>
</h2>
<div
v-show="expanded"
:id="detailsId"
data-test="risk-notices-details"
class="max-h-[min(24vh,16rem)] overflow-y-auto overscroll-contain px-4 pb-4"
>
<div class="flex justify-end">
<button class="btn btn-secondary btn-sm" :disabled="busy" @click="load(0)">{{ t('common.refresh') }}</button>
</div>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-300">{{ t('ipAudit.keyRisk.userHint') }}</p>
<p v-if="error" role="alert" class="mt-2 text-sm text-red-600">{{ t('ipAudit.keyRisk.error') }}</p>
<p v-if="message" role="status" class="mt-2 text-sm">{{ message }}</p>
<p v-if="!busy && !error && !items.length" class="mt-2 text-sm">{{ t('ipAudit.keyRisk.noNotices') }}</p>
<ul class="mt-3 space-y-3">
<li v-for="item in items" :key="`${item.event_id}-${item.generation}`" class="rounded-lg border border-gray-200 bg-white p-3 dark:border-dark-600 dark:bg-dark-800">
<p class="font-medium">{{ t(`ipAudit.keyRisk.${item.rule}`) }} · #{{ item.event_id }}</p>
<div class="my-1 space-y-1 text-sm"><p class="text-xs text-gray-500">{{ t('ipAudit.keyRisk.keys') }}</p><div v-for="id in item.key_ids" :key="id"><RiskIdentity :id="id" :name="item.keys?.find(key => key.id === id)?.name" /></div><p v-if="!item.key_ids.length">—</p></div>
<p class="text-sm text-gray-500">{{ new Date(item.last_seen).toLocaleString() }} · {{ t('ipAudit.keyRisk.attempts', { count: item.attempts, max: item.max_reminders }) }}</p>
<p v-if="item.acknowledged_at" class="mt-2 text-sm" role="status">{{ t('ipAudit.keyRisk.acknowledged') }} · {{ new Date(item.acknowledged_at).toLocaleString() }}</p>
<p v-else-if="item.state !== 'pending'" class="mt-2 text-sm">{{ t(`ipAudit.keyRisk.${item.state}`) }}</p>
<template v-else>
<p v-if="item.attempts >= item.max_reminders" class="mt-2 text-sm">{{ t('ipAudit.keyRisk.limitReached') }}</p>
<button class="btn btn-secondary btn-sm mt-2" :disabled="busy" @click="confirming = item">{{ t('ipAudit.keyRisk.acknowledge') }}</button>
</template>
<div v-if="confirming?.event_id === item.event_id" class="mt-3 space-y-2 rounded border border-amber-300 p-3" role="group" :aria-label="t('ipAudit.keyRisk.acknowledge')">
<p class="text-sm">{{ t('ipAudit.keyRisk.ackConfirm') }}</p>
<div class="flex flex-wrap gap-2">
<button class="btn btn-primary btn-sm" :disabled="busy" data-test="confirm-risk" @click="acknowledge(item)">{{ t('ipAudit.keyRisk.ackSubmit') }}</button>
<button class="btn btn-secondary btn-sm" :disabled="busy" @click="confirming = null">{{ t('common.cancel') }}</button>
</div>
</div>
</div>
</li>
</ul>
<div v-if="before || items.length === 50" class="mt-3 flex gap-2">
<button class="btn btn-secondary btn-sm" :disabled="busy || !before" @click="load(0)">{{ t('ipAudit.keyRisk.firstPage') }}</button>
<button class="btn btn-secondary btn-sm" :disabled="busy || items.length < 50" @click="load(items[items.length - 1]!.event_id)">{{ t('ipAudit.next') }}</button>
</li>
</ul>
<div v-if="before || items.length === 50" class="mt-3 flex gap-2">
<button class="btn btn-secondary btn-sm" :disabled="busy || !before" @click="load(0)">{{ t('ipAudit.keyRisk.firstPage') }}</button>
<button class="btn btn-secondary btn-sm" :disabled="busy || items.length < 50" @click="load(items[items.length - 1]!.event_id)">{{ t('ipAudit.next') }}</button>
</div>
</div>
</section>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { onMounted, ref, useId } from 'vue'
import RiskIdentity from './RiskIdentity.vue'
import { useI18n } from 'vue-i18n'
import { userKeyRiskApi, type UserKeyRiskNotice } from './keyRiskApi'
const { t } = useI18n()
const noticeId = useId()
const titleId = `key-risk-notices-title-${noticeId}`
const detailsId = `key-risk-notices-details-${noticeId}`
const items = ref<UserKeyRiskNotice[]>([])
const expanded = ref(false)
const busy = ref(false), error = ref(false), message = ref(''), before = ref(0)
const confirming = ref<UserKeyRiskNotice | null>(null)
async function load(cursor: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils'
import { createI18n } from 'vue-i18n'
import UserKeyRiskNotices from '../UserKeyRiskNotices.vue'
import { userKeyRiskApi, type UserKeyRiskNotice } from '../keyRiskApi'
import en from '@/i18n/locales/en/ipAudit'
import zh from '@/i18n/locales/zh/ipAudit'
enableAutoUnmount(afterEach)
afterEach(() => { document.body.replaceChildren() })
vi.mock('../keyRiskApi', () => ({ userKeyRiskApi: { list: vi.fn(), acknowledge: vi.fn() } }))
const notice = (): UserKeyRiskNotice => ({ event_id: 4, generation: 2, rule: 'cross_user_ip', state: 'pending', key_ids: [11], keys: [{ id: 11, name: '<Production key>' }], first_seen: '2026-11-07T01:00:00Z', last_seen: '2026-11-07T01:01:00Z', attempts: 1, max_reminders: 3, acknowledged_at: null })
function render(locale = 'en') {
return mount(UserKeyRiskNotices, { global: { plugins: [createI18n({ legacy: false, locale, messages: { en: { ipAudit: en, common: { refresh: 'Refresh', cancel: 'Cancel' } }, zh: { ipAudit: zh, common: { refresh: '刷新', cancel: '取消' } } } })] } })
// jsdom invalidates computed-style caches only for nodes attached to its document.
return mount(UserKeyRiskNotices, { attachTo: document.body, global: { plugins: [createI18n({ legacy: false, locale, messages: { en: { ipAudit: en, common: { refresh: 'Refresh', cancel: 'Cancel' } }, zh: { ipAudit: zh, common: { refresh: '刷新', cancel: '取消' } } } })] } })
}
beforeEach(() => { vi.resetAllMocks(); vi.mocked(userKeyRiskApi.list).mockResolvedValue({ items: [notice()] }); vi.mocked(userKeyRiskApi.acknowledge).mockResolvedValue() })
describe('Customer risk acknowledgement', () => {
it.each(['en', 'zh'])('renders the risk and confirmation boundary in %s', async locale => {
const w = render(locale); await flushPromises()
expect(w.get('[data-test=toggle-risk-notices]').attributes('aria-expanded')).toBe('false')
const details = w.get('[data-test=risk-notices-details]')
expect(details.isVisible()).toBe(false)
expect(w.get('[data-test=toggle-risk-notices]').attributes('aria-controls')).toBe(details.attributes('id'))
expect(w.get('[data-test=toggle-risk-notices]').text()).not.toContain('<Production key>')
await w.get('[data-test=toggle-risk-notices]').trigger('click')
expect(details.isVisible()).toBe(true)
expect(w.text()).not.toContain('ipAudit.')
expect(w.text()).toContain('<Production key>#11')
expect(w.find('production').exists()).toBe(false)
Expand All @@ -22,6 +32,7 @@ describe('Customer risk acknowledgement', () => {
})
it('requires confirmation and displays the server acknowledgement', async () => {
const w = render(); await flushPromises()
await w.get('[data-test=toggle-risk-notices]').trigger('click')
await w.findAll('button').find(b => b.text() === en.keyRisk.acknowledge)!.trigger('click')
expect(userKeyRiskApi.acknowledge).not.toHaveBeenCalled()
expect(w.get('[role=group]').text()).toContain(en.keyRisk.ackConfirm)
Expand All @@ -35,6 +46,7 @@ describe('Customer risk acknowledgement', () => {
it('keeps an unsuccessful confirmation actionable', async () => {
vi.mocked(userKeyRiskApi.acknowledge).mockRejectedValue(new Error('stale episode'))
const w = render(); await flushPromises()
await w.get('[data-test=toggle-risk-notices]').trigger('click')
await w.findAll('button').find(b => b.text() === en.keyRisk.acknowledge)!.trigger('click')
await w.get('[data-test=confirm-risk]').trigger('click'); await flushPromises()
expect(w.get('[role=alert]').text()).toBe(en.keyRisk.error)
Expand All @@ -45,8 +57,45 @@ describe('Customer risk acknowledgement', () => {
it('permits acknowledgement after the reminder cap is reached', async () => {
vi.mocked(userKeyRiskApi.list).mockResolvedValue({ items: [{ ...notice(), attempts: 3 }] })
const w = render(); await flushPromises()
await w.get('[data-test=toggle-risk-notices]').trigger('click')
expect(w.text()).toContain(en.keyRisk.limitReached)
expect(w.findAll('button').some(b => b.text() === en.keyRisk.acknowledge)).toBe(true)
w.unmount()
})
it('collapses details without acknowledging or fetching the notices again', async () => {
const w = render(); await flushPromises()
const toggle = w.get('[data-test=toggle-risk-notices]')
await toggle.trigger('click')
expect(toggle.attributes('aria-expanded')).toBe('true')
expect(w.get('[data-test=risk-notices-details]').text()).toContain('<Production key>#11')
await toggle.trigger('click')
expect(toggle.attributes('aria-expanded')).toBe('false')
expect(w.get('[data-test=risk-notices-details]').isVisible()).toBe(false)
expect(toggle.attributes('aria-controls')).toBe(w.get('[data-test=risk-notices-details]').attributes('id'))
expect(userKeyRiskApi.acknowledge).not.toHaveBeenCalled()
expect(userKeyRiskApi.list).toHaveBeenCalledTimes(1)
w.unmount()
const reopened = render(); await flushPromises()
expect(reopened.get('[data-test=toggle-risk-notices]').attributes('aria-expanded')).toBe('false')
expect(reopened.get('[data-test=risk-notices-details]').isVisible()).toBe(false)
reopened.unmount()
})
it('keeps each disclosure linked to its own mounted panel', async () => {
const w = mount({ components: { UserKeyRiskNotices }, template: '<div><UserKeyRiskNotices /><UserKeyRiskNotices /></div>' }, {
attachTo: document.body,
global: { plugins: [createI18n({ legacy: false, locale: 'en', messages: { en: { ipAudit: en, common: { refresh: 'Refresh', cancel: 'Cancel' } } } })] }
})
await flushPromises()
const panels = w.findAll('[data-test=risk-notices-details]')
const toggles = w.findAll('[data-test=toggle-risk-notices]')
expect(panels).toHaveLength(2)
expect(panels[0]!.attributes('id')).not.toBe(panels[1]!.attributes('id'))
expect(toggles.map(toggle => toggle.attributes('aria-controls'))).toEqual(panels.map(panel => panel.attributes('id')))
await toggles[0]!.trigger('click')
expect(panels[0]!.isVisible()).toBe(true)
expect(panels[1]!.isVisible()).toBe(false)
const labelledSections = w.findAll('section')
expect(labelledSections.map(section => section.attributes('aria-labelledby'))).toEqual(w.findAll('h2').map(title => title.attributes('id')))
w.unmount()
})
})
3 changes: 3 additions & 0 deletions frontend/src/i18n/locales/en/ipAudit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export default {
"limitReached": "Reminder limit reached. No more email for this event; the risk still needs review.",
"suppressed": "No new notice: acknowledged, limit reached, or within the 24-hour deduplication window.",
"userTitle": "Key risk notices",
"noticeCount": "{count} on this page",
"expandNotices": "Expand",
"collapseNotices": "Collapse",
"userHint": "Review unusual usage here. Check your clients and replace or disable keys if needed. Acknowledging pauses reminders for this event; it does not resolve the risk.",
"noNotices": "No risk notices to display.",
"attempts": "Reminder attempts {count} / {max}",
Expand Down
3 changes: 3 additions & 0 deletions frontend/src/i18n/locales/zh/ipAudit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export default {
"limitReached": "已达到提醒上限,不再发送本次事件邮件;风险仍需核查。",
"suppressed": "未新增通知:可能已确认、达到上限或处于 24 小时去重期。",
"userTitle": "密钥风险提醒",
"noticeCount": "本页 {count} 条",
"expandNotices": "展开",
"collapseNotices": "收起",
"userHint": "发现异常使用时会在此展示。请核查客户端使用情况,必要时替换或停用密钥。确认知晓只暂停本次事件提醒,不代表风险已消除。",
"noNotices": "当前没有风险提醒记录。",
"attempts": "提醒尝试 {count} / {max} 次",
Expand Down
Loading
Loading