import { describe, it, expect } from "vitest"
import type { ActionState } from "./action-state"

describe("ActionState 类型构造", () => {
  it("should create a success state with data", () => {
    const state: ActionState<string> = {
      success: true,
      message: "Operation succeeded",
      data: "result",
    }
    expect(state.success).toBe(true)
    expect(state.data).toBe("result")
  })

  it("should create an error state with field-level errors", () => {
    const state: ActionState = {
      success: false,
      message: "Operation failed",
      errors: { field: ["Error message"] },
    }
    expect(state.success).toBe(false)
    expect(state.errors).toBeDefined()
  })

  it("should create a void state without data", () => {
    const state: ActionState = {
      success: true,
      message: "Done",
    }
    expect(state.success).toBe(true)
    expect(state.data).toBeUndefined()
  })

  it("should allow multiple errors per field", () => {
    const state: ActionState = {
      success: false,
      errors: {
        email: ["必填", "格式不正确"],
        password: ["至少 8 位"],
      },
    }
    expect(state.errors?.email).toHaveLength(2)
    expect(state.errors?.password).toHaveLength(1)
  })

  it("should allow falsy data values (0, empty string, null)", () => {
    const zeroState: ActionState<number> = { success: true, data: 0 }
    expect(zeroState.data).toBe(0)

    const emptyState: ActionState<string> = { success: true, data: "" }
    expect(emptyState.data).toBe("")

    const nullState: ActionState<null> = { success: true, data: null }
    expect(nullState.data).toBeNull()
  })

  it("should allow empty message", () => {
    const state: ActionState = { success: true, message: "" }
    expect(state.message).toBe("")
  })

  it("should allow success state without message", () => {
    const state: ActionState<string> = { success: true, data: "result" }
    expect(state.message).toBeUndefined()
    expect(state.errors).toBeUndefined()
  })
})
