feat(infra): 落地 M01 基础框架与本机开发环境
交付 FastAPI/Celery/Vue 薄壳、FORCE RLS、Compose 回环与 review 修复证据,关闭 M01 DONE_LOCAL。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
8b2a809f06
commit
8fb16f4a4a
97 files changed
+8059
-22
No files matched your search
@@ -0,0 +1,123 @@
|
||||
import { flushPromises, mount } from "@vue/test-utils";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createI18n } from "vue-i18n";
|
||||
import { BusinessError } from "@watchman/api";
|
||||
import App from "./App.vue";
|
||||
import zhCN from "../locales/zh-CN/common.json";
|
||||
import en from "../locales/en/common.json";
|
||||
|
||||
vi.mock("@watchman/api", async () => {
|
||||
const actual = await vi.importActual<typeof import("@watchman/api")>("@watchman/api");
|
||||
return {
|
||||
...actual,
|
||||
requestJson: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
import { requestJson } from "@watchman/api";
|
||||
|
||||
const mockedRequestJson = vi.mocked(requestJson);
|
||||
|
||||
function mountApp(locale = "zh-CN") {
|
||||
const i18n = createI18n({
|
||||
legacy: false,
|
||||
locale,
|
||||
fallbackLocale: "zh-CN",
|
||||
messages: {
|
||||
"zh-CN": zhCN,
|
||||
en,
|
||||
// Intentionally omit th to prove fallbackLocale=zh-CN.
|
||||
},
|
||||
});
|
||||
return mount(App, {
|
||||
global: {
|
||||
plugins: [i18n],
|
||||
stubs: {
|
||||
ElConfigProvider: { template: "<div><slot /></div>" },
|
||||
ElCard: {
|
||||
template:
|
||||
'<div class="el-card"><div class="el-card__header"><slot name="header" /></div><slot /></div>',
|
||||
},
|
||||
ElTag: {
|
||||
props: ["type"],
|
||||
template: '<span data-testid="health-tag"><slot /></span>',
|
||||
},
|
||||
ElAlert: {
|
||||
props: ["title", "type"],
|
||||
template:
|
||||
'<div :data-testid="type === \'success\' ? \'health-success\' : \'health-failure\'">{{ title }}</div>',
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
describe("App health three states", () => {
|
||||
beforeEach(() => {
|
||||
mockedRequestJson.mockReset();
|
||||
});
|
||||
|
||||
it("shows loading before the request settles", async () => {
|
||||
let resolveRequest: (value: unknown) => void = () => undefined;
|
||||
mockedRequestJson.mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
resolveRequest = resolve;
|
||||
}),
|
||||
);
|
||||
const wrapper = mountApp();
|
||||
expect(wrapper.get("[data-testid=health-loading]").text()).toContain("健康检查加载中");
|
||||
resolveRequest({ status: "ok", app: "watchman", version: "0.1.0" });
|
||||
await flushPromises();
|
||||
});
|
||||
|
||||
it("shows success after a healthy response", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
const wrapper = mountApp();
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查成功");
|
||||
expect(wrapper.get("[data-testid=health-success]").text()).toContain("watchman 0.1.0");
|
||||
});
|
||||
|
||||
it("shows failure after a BusinessError", async () => {
|
||||
mockedRequestJson.mockRejectedValue(
|
||||
new BusinessError({
|
||||
code: 50000,
|
||||
message: "Internal server error",
|
||||
status: 500,
|
||||
requestId: "req-fail",
|
||||
}),
|
||||
);
|
||||
const wrapper = mountApp();
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查失败");
|
||||
expect(wrapper.get("[data-testid=health-failure]").text()).toContain("50000");
|
||||
});
|
||||
|
||||
it("falls back to zh-CN when locale messages are missing", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
const wrapper = mountApp("th");
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("健康检查成功");
|
||||
});
|
||||
|
||||
it("shows english health copy when locale is en", async () => {
|
||||
mockedRequestJson.mockResolvedValue({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
const wrapper = mountApp("en");
|
||||
await flushPromises();
|
||||
expect(wrapper.get("[data-testid=health-tag]").text()).toContain("Health check succeeded");
|
||||
expect(wrapper.get("[data-testid=health-success]").text()).toContain("Service available");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { ElAlert, ElCard, ElConfigProvider, ElTag } from "element-plus";
|
||||
import zhCn from "element-plus/es/locale/lang/zh-cn";
|
||||
import zhTw from "element-plus/es/locale/lang/zh-tw";
|
||||
import en from "element-plus/es/locale/lang/en";
|
||||
import th from "element-plus/es/locale/lang/th";
|
||||
import { requestJson } from "@watchman/api";
|
||||
import {
|
||||
healthFailureView,
|
||||
healthSuccessView,
|
||||
type HealthPayload,
|
||||
type HealthState,
|
||||
} from "./health";
|
||||
|
||||
const { t, locale } = useI18n();
|
||||
const elementLocale = computed(() => {
|
||||
switch (locale.value) {
|
||||
case "zh-TW":
|
||||
return zhTw;
|
||||
case "en":
|
||||
return en;
|
||||
case "th":
|
||||
return th;
|
||||
default:
|
||||
return zhCn;
|
||||
}
|
||||
});
|
||||
|
||||
const state = ref<HealthState>("loading");
|
||||
const detail = ref("");
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const data = await requestJson<HealthPayload>("/health");
|
||||
const view = healthSuccessView(data);
|
||||
state.value = view.state;
|
||||
detail.value = view.detail;
|
||||
} catch (error) {
|
||||
const view = healthFailureView(error);
|
||||
state.value = view.state;
|
||||
detail.value = view.detail;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ElConfigProvider :locale="elementLocale">
|
||||
<main class="min-h-screen p-8 bg-[var(--bg-surface-soft)] text-[length:var(--font-size-base)] text-[var(--text-primary)]">
|
||||
<ElCard>
|
||||
<template #header>
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<span>{{ t("appName") }}</span>
|
||||
<span data-testid="health-tag">
|
||||
<ElTag
|
||||
:type="state === 'loading' ? 'info' : state === 'success' ? 'success' : 'danger'"
|
||||
>
|
||||
{{
|
||||
state === "loading"
|
||||
? t("health.loading")
|
||||
: state === "success"
|
||||
? t("health.success")
|
||||
: t("health.failure")
|
||||
}}
|
||||
</ElTag>
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
<ElAlert
|
||||
v-if="state === 'success'"
|
||||
type="success"
|
||||
data-testid="health-success"
|
||||
:title="t('health.successDetail', { detail })"
|
||||
show-icon
|
||||
:closable="false"
|
||||
/>
|
||||
<ElAlert
|
||||
v-else-if="state === 'failure'"
|
||||
type="error"
|
||||
data-testid="health-failure"
|
||||
:title="t('health.failureDetail', { detail })"
|
||||
show-icon
|
||||
:closable="false"
|
||||
/>
|
||||
<p v-else data-testid="health-loading">{{ t("health.loading") }}</p>
|
||||
</ElCard>
|
||||
</main>
|
||||
</ElConfigProvider>
|
||||
</template>
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { BusinessError } from "@watchman/api";
|
||||
import { healthFailureView, healthSuccessView } from "./health";
|
||||
|
||||
describe("health view mapping", () => {
|
||||
it("maps success payload", () => {
|
||||
const view = healthSuccessView({
|
||||
status: "ok",
|
||||
app: "watchman",
|
||||
version: "0.1.0",
|
||||
});
|
||||
expect(view).toEqual({
|
||||
state: "success",
|
||||
detail: "watchman 0.1.0",
|
||||
});
|
||||
});
|
||||
|
||||
it("maps BusinessError failure", () => {
|
||||
const view = healthFailureView(
|
||||
new BusinessError({
|
||||
code: 50000,
|
||||
message: "Service error",
|
||||
status: 500,
|
||||
requestId: "req-1",
|
||||
}),
|
||||
);
|
||||
expect(view.state).toBe("failure");
|
||||
expect(view.detail).toContain("50000");
|
||||
expect(view.detail).toContain("Service error");
|
||||
});
|
||||
|
||||
it("maps unknown failure", () => {
|
||||
const view = healthFailureView(new Error("network down"));
|
||||
expect(view).toEqual({
|
||||
state: "failure",
|
||||
detail: "Error: network down",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Health check presentation helpers for the M01 thin shell.
|
||||
* Keeps fetch outcome mapping testable without mounting Element Plus.
|
||||
*/
|
||||
|
||||
import { BusinessError } from "@watchman/api";
|
||||
|
||||
export type HealthState = "loading" | "success" | "failure";
|
||||
|
||||
export type HealthView = {
|
||||
state: HealthState;
|
||||
detail: string;
|
||||
};
|
||||
|
||||
export type HealthPayload = {
|
||||
status: string;
|
||||
app: string;
|
||||
version: string;
|
||||
};
|
||||
|
||||
export function healthSuccessView(data: HealthPayload): HealthView {
|
||||
return {
|
||||
state: "success",
|
||||
detail: `${data.app} ${data.version}`,
|
||||
};
|
||||
}
|
||||
|
||||
export function healthFailureView(error: unknown): HealthView {
|
||||
if (error instanceof BusinessError) {
|
||||
return {
|
||||
state: "failure",
|
||||
detail: `${error.code}: ${error.message}`,
|
||||
};
|
||||
}
|
||||
return {
|
||||
state: "failure",
|
||||
detail: String(error),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { createI18n } from "vue-i18n";
|
||||
import zhCN from "../locales/zh-CN/common.json";
|
||||
import zhTW from "../locales/zh-TW/common.json";
|
||||
import en from "../locales/en/common.json";
|
||||
import th from "../locales/th/common.json";
|
||||
import zhCNErrors from "../locales/zh-CN/errors.json";
|
||||
import zhTWErrors from "../locales/zh-TW/errors.json";
|
||||
import enErrors from "../locales/en/errors.json";
|
||||
import thErrors from "../locales/th/errors.json";
|
||||
|
||||
const supported = new Set(["zh-CN", "zh-TW", "en", "th"]);
|
||||
|
||||
function resolveLocale(): string {
|
||||
if (typeof window !== "undefined") {
|
||||
const fromQuery = new URLSearchParams(window.location.search).get("lang");
|
||||
if (fromQuery && supported.has(fromQuery)) {
|
||||
window.localStorage.setItem("watchman.lang", fromQuery);
|
||||
return fromQuery;
|
||||
}
|
||||
const stored = window.localStorage.getItem("watchman.lang");
|
||||
if (stored && supported.has(stored)) {
|
||||
return stored;
|
||||
}
|
||||
}
|
||||
return "zh-CN";
|
||||
}
|
||||
|
||||
export const i18n = createI18n({
|
||||
legacy: false,
|
||||
locale: resolveLocale(),
|
||||
fallbackLocale: "zh-CN",
|
||||
messages: {
|
||||
"zh-CN": { ...zhCN, ...zhCNErrors },
|
||||
"zh-TW": { ...zhTW, ...zhTWErrors },
|
||||
en: { ...en, ...enErrors },
|
||||
th: { ...th, ...thErrors },
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { createApp } from "vue";
|
||||
import { createPinia } from "pinia";
|
||||
import "element-plus/dist/index.css";
|
||||
import "element-plus/theme-chalk/dark/css-vars.css";
|
||||
import "./styles.css";
|
||||
import App from "./App.vue";
|
||||
import { i18n } from "./i18n";
|
||||
|
||||
const app = createApp(App);
|
||||
app.use(createPinia());
|
||||
app.use(i18n);
|
||||
app.mount("#app");
|
||||
@@ -0,0 +1,24 @@
|
||||
/* Map Element Plus skin vars to web/design/tokens.css (standards://frontend/element-plus). */
|
||||
:root,
|
||||
html.dark {
|
||||
--el-color-primary: var(--color-primary);
|
||||
--el-font-size-base: var(--font-size-base);
|
||||
--el-font-size-small: 13px;
|
||||
--el-font-size-extra-small: 12px;
|
||||
--el-text-color-primary: var(--text-primary);
|
||||
--el-text-color-regular: var(--text-secondary);
|
||||
--el-text-color-secondary: var(--text-muted);
|
||||
--el-border-color: var(--border-default);
|
||||
--el-border-color-light: var(--border-subtle);
|
||||
--el-border-color-lighter: var(--border-subtle);
|
||||
--el-bg-color: var(--bg-surface);
|
||||
--el-fill-color-light: var(--bg-surface-soft);
|
||||
--el-border-radius-base: var(--radius-control);
|
||||
--el-component-size: 32px;
|
||||
}
|
||||
|
||||
/* Tailwind preflight sets svg { display:block }; keep Element button icons inline. */
|
||||
.el-button svg {
|
||||
display: inline-block;
|
||||
vertical-align: middle;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
@import "tailwindcss";
|
||||
@import "../../../design/tokens.css";
|
||||
@import "./shared/styles/element-theme.css";
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
/// <reference types="vite/client" />
|
||||
|
||||
declare module "*.vue" {
|
||||
import type { DefineComponent } from "vue";
|
||||
const component: DefineComponent<Record<string, unknown>, Record<string, unknown>, unknown>;
|
||||
export default component;
|
||||
}
|
||||
|
||||
declare module "*.json" {
|
||||
const value: Record<string, unknown>;
|
||||
export default value;
|
||||
}
|
||||
Reference in new issue
Block a user