test: 前端单测覆盖率Phase2+3,行覆盖率22%→51% (#575)
CI Build & Deploy Pipeline / Staging E2E Tests (push) Failing after 1m14s
CI Build & Deploy Pipeline / Production Browser E2E (push) Failing after 1419h8m25s
CI Build & Deploy Pipeline / Build Production Worker Image (push) Failing after 1419h8m28s
CI Build & Deploy Pipeline / Build Production Web Image (push) Failing after 1419h8m29s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 1419h11m0s
CI Build & Deploy Pipeline / Build Production API Image (push) Failing after 1419h8m29s
CI Build & Deploy Pipeline / Build Staging API Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Staging Web Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Staging Worker Image (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been skipped
CI Build & Deploy Pipeline / Staging API Integration Tests (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Production (push) Failing after 1419h40m33s
CI/CD Pipeline / Validate Code Quality And Tests (push) Has been skipped
CI/CD Pipeline / Unit Tests (push) Has been skipped
CI/CD Pipeline / Integration Tests (push) Has been skipped
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (push) Has been skipped
CI Build & Deploy Pipeline / Staging E2E Tests (push) Failing after 1m14s
CI Build & Deploy Pipeline / Production Browser E2E (push) Failing after 1419h8m25s
CI Build & Deploy Pipeline / Build Production Worker Image (push) Failing after 1419h8m28s
CI Build & Deploy Pipeline / Build Production Web Image (push) Failing after 1419h8m29s
CI/CD Pipeline / Check if frontend-only change (push) Failing after 1419h11m0s
CI Build & Deploy Pipeline / Build Production API Image (push) Failing after 1419h8m29s
CI Build & Deploy Pipeline / Build Staging API Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Staging Web Image (push) Has been skipped
CI Build & Deploy Pipeline / Build Staging Worker Image (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been skipped
CI Build & Deploy Pipeline / Staging API Integration Tests (push) Has been skipped
CI Build & Deploy Pipeline / Deploy Production (push) Failing after 1419h40m33s
CI/CD Pipeline / Validate Code Quality And Tests (push) Has been skipped
CI/CD Pipeline / Unit Tests (push) Has been skipped
CI/CD Pipeline / Integration Tests (push) Has been skipped
CI/CD Pipeline / Frontend Lint (push) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (push) Has been skipped
Co-authored-by: xiaoxia <dev@xiaoxiajianji.com> Co-committed-by: xiaoxia <dev@xiaoxiajianji.com>
This commit was merged in pull request #575.
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* auth API 纯函数测试
|
||||
* - normalizeUser
|
||||
*/
|
||||
import { describe, it, expect } from "vitest"
|
||||
import { normalizeUser } from "@/api/auth"
|
||||
|
||||
describe("normalizeUser", () => {
|
||||
it("应该正确映射标准用户数据", () => {
|
||||
const input = {
|
||||
id: "123",
|
||||
user_id: "123",
|
||||
email: "test@example.com",
|
||||
username: "testuser",
|
||||
display_name: "Test User",
|
||||
is_email_verified: true,
|
||||
email_verified: true,
|
||||
created_at: "2024-01-01T00:00:00Z",
|
||||
}
|
||||
|
||||
const result = normalizeUser(input)
|
||||
|
||||
expect(result.id).toBe("123")
|
||||
expect(result.user_id).toBe("123")
|
||||
expect(result.email).toBe("test@example.com")
|
||||
expect(result.username).toBe("testuser")
|
||||
expect(result.display_name).toBe("Test User")
|
||||
expect(result.is_email_verified).toBe(true)
|
||||
expect(result.email_verified).toBe(true)
|
||||
expect(result.created_at).toBe("2024-01-01T00:00:00Z")
|
||||
})
|
||||
|
||||
it("id 优先于 user_id", () => {
|
||||
const input = {
|
||||
id: "id-from-id",
|
||||
user_id: "id-from-user-id",
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
}
|
||||
|
||||
const result = normalizeUser(input)
|
||||
expect(result.id).toBe("id-from-id")
|
||||
expect(result.user_id).toBe("id-from-id")
|
||||
})
|
||||
|
||||
it("没有 id 时使用 user_id", () => {
|
||||
const input = {
|
||||
user_id: "fallback-user-id",
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
}
|
||||
|
||||
const result = normalizeUser(input as any)
|
||||
expect(result.id).toBe("fallback-user-id")
|
||||
expect(result.user_id).toBe("fallback-user-id")
|
||||
})
|
||||
|
||||
it("id 和 user_id 都没有时返回空字符串", () => {
|
||||
const input = {
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
}
|
||||
|
||||
const result = normalizeUser(input as any)
|
||||
expect(result.id).toBe("")
|
||||
expect(result.user_id).toBe("")
|
||||
})
|
||||
|
||||
it("is_email_verified 优先于 email_verified", () => {
|
||||
const input = {
|
||||
id: "1",
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
is_email_verified: true,
|
||||
email_verified: false,
|
||||
}
|
||||
|
||||
const result = normalizeUser(input)
|
||||
expect(result.is_email_verified).toBe(true)
|
||||
expect(result.email_verified).toBe(true)
|
||||
})
|
||||
|
||||
it("没有 is_email_verified 时使用 email_verified", () => {
|
||||
const input = {
|
||||
id: "1",
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
email_verified: true,
|
||||
}
|
||||
|
||||
const result = normalizeUser(input as any)
|
||||
expect(result.is_email_verified).toBe(true)
|
||||
expect(result.email_verified).toBe(true)
|
||||
})
|
||||
|
||||
it("两个都没有时默认为 false", () => {
|
||||
const input = {
|
||||
id: "1",
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
}
|
||||
|
||||
const result = normalizeUser(input as any)
|
||||
expect(result.is_email_verified).toBe(false)
|
||||
expect(result.email_verified).toBe(false)
|
||||
})
|
||||
|
||||
it("缺失可选字段时返回 undefined", () => {
|
||||
const input = {
|
||||
id: "1",
|
||||
email: "a@b.com",
|
||||
username: "user",
|
||||
}
|
||||
|
||||
const result = normalizeUser(input as any)
|
||||
expect(result.display_name).toBeUndefined()
|
||||
expect(result.created_at).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user