import { describe, expect, it } from "vitest"
import { buildInitialMigrationSql } from "./full-migration"

describe("buildInitialMigrationSql", () => {
    it("包含当前业务基础表，且不再创建 Todo、普通聊天、便签和向量表", () => {
        const sql = buildInitialMigrationSql()

        expect(sql).toContain("create table if not exists petrichor_user")
        expect(sql).toContain("email text not null")
        expect(sql).toContain("user_type text not null default 'LOCAL'")
        expect(sql).toContain("auth_user_id text")
        expect(sql).toContain("linuxdo_account_id text")
        expect(sql).toContain("ux_petrichor_user_linuxdo_account_id")
        expect(sql).toContain("create table if not exists better_auth_user")
        expect(sql).toContain("create table if not exists better_auth_session")
        expect(sql).toContain("create table if not exists better_auth_account")
        expect(sql).toContain("create table if not exists better_auth_verification")
        expect(sql).toContain("create table if not exists better_auth_two_factor")
        expect(sql).toContain("two_factor_enabled boolean not null default false")
        expect(sql).toContain("add column if not exists two_factor_enabled boolean")
        expect(sql).toContain("insert into better_auth_user")
        expect(sql).toContain("provider_id, user_id, password")
        expect(sql).toContain("create table if not exists petrichor_auth_session")
        expect(sql).toContain("create table if not exists petrichor_notification")
        expect(sql).toContain("payload_json text")
        expect(sql).toContain("create table if not exists petrichor_kb_article_share")
        expect(sql).toContain("is_repost boolean not null default false")
        expect(sql).toContain("original_url text")
        expect(sql).toContain("original_author_name text")
        expect(sql).toContain("add column if not exists is_repost boolean not null default false")
        expect(sql).toContain("add column if not exists original_url text")
        expect(sql).toContain("add column if not exists original_author_name text")
        expect(sql).toContain("create table if not exists petrichor_kb_article_burn_link")
        expect(sql).toContain("max_views integer not null default 1")
        expect(sql).toContain("status text not null default 'ACTIVE'")
        expect(sql).toContain("petrichor_kb_burn_link_article_idx")
        expect(sql).toContain("public_excerpt text")
        expect(sql).toContain("reading_minutes integer")
        expect(sql).toContain("toc_json text")
        expect(sql).toContain("public_content_hash text")
        expect(sql).toContain("ai_summary text")
        expect(sql).toContain("add column if not exists ai_summary text")
        expect(sql).toContain("create extension if not exists pg_trgm")
        expect(sql).toContain("idx_petrichor_kb_article_title_trgm")
        expect(sql).toContain("idx_petrichor_kb_article_public_excerpt_trgm")
        expect(sql).toContain("idx_petrichor_kb_article_content_md_trgm")
        expect(sql).toContain("create index if not exists petrichor_kb_article_public_updated_idx")
        expect(sql).toContain("create index if not exists petrichor_kb_article_share_public_idx")
        expect(sql).toContain("create table if not exists petrichor_site_about_profile")
        expect(sql).toContain("insert into petrichor_site_about_profile")
        expect(sql).toContain("Code is just another medium for painting dreams.")
        expect(sql).toContain("create table if not exists petrichor_site_appearance")
        expect(sql).toContain("insert into petrichor_site_appearance")
        expect(sql).toContain("create table if not exists petrichor_ai_model_config")
        expect(sql).not.toContain("create table if not exists petrichor_list")
        expect(sql).not.toContain("create table if not exists petrichor_task")
        expect(sql).not.toContain("create table if not exists petrichor_tag")
        expect(sql).not.toContain("create table if not exists petrichor_task_tag")
        expect(sql).not.toContain("create table if not exists petrichor_ai_chat_session")
        expect(sql).not.toContain("create table if not exists petrichor_ai_chat_run")
        expect(sql).not.toContain("create table if not exists petrichor_ai_chat_message")
        expect(sql).not.toContain("create table if not exists petrichor_kb_embedding_chunk")
        expect(sql).not.toContain("embedding_dimensions")
        expect(sql).not.toContain("extensions.vector")
        expect(sql).not.toContain("create table if not exists petrichor_kb_article_share_invite")
        expect(sql).not.toContain("create table if not exists petrichor_kb_resource_grant")
        expect(sql).not.toContain("create table if not exists petrichor_ai_model_config_share_code")
        expect(sql).not.toContain("create table if not exists petrichor_ai_model_config_binding_application")
        expect(sql).not.toContain("create table if not exists petrichor_ai_model_config_binding")
        expect(sql).not.toContain("create table if not exists petrichor_sticky_note")
    })

    it("包含站内 Assistant 运行时五张表（chat-first roadmap 契约 4.5）", () => {
        const sql = buildInitialMigrationSql()

        expect(sql).toContain("create table if not exists petrichor_assistant_thread")
        expect(sql).toContain("focus_json text")
        expect(sql).toContain("context_summary_md text")
        expect(sql).toContain("context_summary_until_message_id bigint")
        expect(sql).toContain("deleted_at timestamptz")
        expect(sql).toContain("create table if not exists petrichor_assistant_message")
        expect(sql).toContain("create table if not exists petrichor_assistant_run")
        expect(sql).toContain("intent_domains_json text")
        expect(sql).toContain("error_code text")
        expect(sql).toContain("create table if not exists petrichor_assistant_step")
        expect(sql).toContain("step_index integer not null")
        expect(sql).toContain("duration_ms integer")
        expect(sql).toContain("create table if not exists petrichor_assistant_artifact")
        expect(sql).toContain("petrichor_assistant_thread_user_history_idx")
        expect(sql).toContain("create table if not exists petrichor_assistant_plan")
        expect(sql).toContain("ux_petrichor_assistant_plan_thread_key")
        expect(sql).toContain("create table if not exists petrichor_assistant_confirmation")
        expect(sql).toContain("ux_petrichor_assistant_confirmation_key")
        expect(sql).toContain("create table if not exists petrichor_assistant_message_embedding")
        expect(sql).toContain("idx_petrichor_assistant_message_embedding")
    })
})
