import { simulateReadableStream } from "ai";
import { MockLanguageModelV3 } from "ai/test";
import { vi } from "vitest";
import { AiServiceFeatureFlags } from "../features.js";
// A properly formatted mock JWT with user_id in payload for tests that need JWT parsing
export const mockJwt = `header.${Buffer.from(JSON.stringify({
    user_id: "test-user-123",
    org_id: "test-org-456",
})).toString("base64")}.signature`;
const createV3Usage = (inputTokens, outputTokens) => ({
    inputTokens: {
        total: inputTokens,
        noCache: inputTokens,
        cacheRead: 0,
        cacheWrite: 0,
    },
    outputTokens: {
        total: outputTokens,
        text: outputTokens,
        reasoning: 0,
    },
});
export const mockPeer = {
    call: {
        aiPushMessage: vi.fn(),
        aiSetGenerationState: vi.fn(),
        aiPushPromptInterpretation: vi.fn(),
    },
    close: vi.fn(),
};
export const mockClark = {
    state: "Idle",
    context: { peer: mockPeer },
    getMetadata: vi.fn(),
    resetContext: vi.fn(),
    updateContext: vi.fn(),
    updateMetadata: vi.fn(),
    resetAllMetadata: vi.fn(),
    send: vi.fn(),
};
export const mockHandlerParams = {
    draftInterface: {
        createDraftFile: vi.fn(),
        getDraftDiff: vi.fn(),
    },
    appRootDirPath: "/test/app",
    fsOperationQueue: {
        add: vi.fn(),
        enqueue: vi.fn().mockImplementation(async (fn) => fn()),
    },
    templateRenderer: {
        render: vi.fn(),
    },
    signals: {
        emit: vi.fn(),
        once: vi
            .fn()
            .mockImplementation((_event, callback) => {
            // Simulate checkpoint uploaded immediately (next microtask)
            void Promise.resolve().then(() => callback());
        }),
    },
    sourceTrackerInterface: {
        getElementToFilePathMap: vi.fn(),
        getElementToFilePath: vi.fn(),
        getElementToLocation: vi.fn(),
        getScopeDefinitionForPage: vi.fn(),
        isCustomComponent: vi.fn(),
    },
    appShell: {
        appRootDirPath: "/test/app",
        readFiles: vi.fn().mockReturnValue([
            {
                name: "test.ts",
                content: 'console.log("test");',
            },
        ]),
        runTypecheck: vi.fn(),
        runLinter: vi.fn(),
        warmLinterCache: vi.fn(),
        clearScratchDirectory: vi.fn(),
    },
    clarkProvider: vi.fn().mockReturnValue(new MockLanguageModelV3({
        doStream: async (_options) => ({
            stream: simulateReadableStream({
                chunks: [
                    { type: "text-delta", id: "1", delta: "Hello" },
                    { type: "text-delta", id: "2", delta: " world!" },
                    {
                        type: "finish",
                        finishReason: { unified: "stop", raw: "stop" },
                        usage: createV3Usage(13, 10),
                    },
                ],
            }),
            request: { body: null },
            response: { headers: {} },
        }),
    })),
    integrationStore: {
        getOpenApiSpec: vi.fn(),
        getIntegrationForPromptContext: vi.fn(),
    },
    knowledgeManager: {
        loadFactsPromptSections: vi.fn().mockResolvedValue({
            systemPromptSection: "",
            userMessageSection: "",
            onDemandSkillIndex: "",
            facts: {
                org: [],
                user: [],
                integrations: {},
            },
        }),
        getNetNewFacts: vi.fn().mockReturnValue({
            org: [],
            user: [],
            integrations: {},
        }),
        hasAnyFacts: vi.fn().mockReturnValue(false),
        getFactById: vi.fn().mockReturnValue(undefined),
    },
    appContextStore: {
        componentSemanticsById: {},
        apiSemanticsByName: {},
        getComponent: vi.fn().mockReturnValue({}),
    },
    chatSessionStore: {
        getMessages: vi.fn(),
        newChat: vi.fn(),
        recordUser: vi.fn(),
        recordUserAcceptance: vi.fn(),
        recordUserRejection: vi.fn(),
        recordAssistant: vi.fn(),
        recordAssistantChangeInfo: vi.fn(),
        getLastUserMessage: vi.fn(),
    },
    features: AiServiceFeatureFlags.create({ "ai.suggest-whats-next": true }),
    llmProvider: {
        name: "test-provider",
        modelForTask: vi.fn().mockReturnValue(new MockLanguageModelV3({
            doStream: async (_options) => ({
                stream: simulateReadableStream({
                    chunks: [
                        {
                            type: "finish",
                            finishReason: { unified: "stop", raw: "stop" },
                            usage: createV3Usage(1, 1),
                        },
                    ],
                }),
                request: { body: null },
                response: { headers: {} },
            }),
        })),
    },
    logger: {
        info: vi.fn(),
        error: vi.fn(),
        debug: vi.fn(),
        warn: vi.fn(),
    },
};
//# sourceMappingURL=mocks.js.map