Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

2.3 KB
Raw Blame
import { vi } from 'vitest'

// `Promise.withResolvers` (Node 22+) is used by tests that resolve a promise
// from outside its executor. Polyfilled for the Node 20 runtime; a no-op where
// the runtime already ships it.
if (typeof (Promise as { withResolvers?: unknown }).withResolvers !== 'function') {
  (
    Promise as unknown as { withResolvers: <T>() => {
      promise: Promise<T>
      resolve: (value: T | PromiseLike<T>) => void
      reject: (reason?: unknown) => void
    } }
  ).withResolvers = function withResolvers<T>() {
    let resolve!: (value: T | PromiseLike<T>) => void
    let reject!: (reason?: unknown) => void
    const promise = new Promise<T>((res, rej) => {
      resolve = res
      reject = rej
    })
    return { promise, resolve, reject }
  }
}

// Mock localStorage
const localStorageMock = {
  getItem: vi.fn(),
  setItem: vi.fn(),
  removeItem: vi.fn(),
  clear: vi.fn(),
}

Object.defineProperty(globalThis, 'localStorage', {
  value: localStorageMock,
  writable: true,
})

// Mock the global window object for Tauri
Object.defineProperty(globalThis, 'window', {
  value: {
    localStorage: localStorageMock,
    core: {
      api: {
        // getSystemInfo: vi.fn(),
      },
    },
  },
})

vi.mock('@janhq/tauri-plugin-hardware-api', () => ({
  getSystemInfo: vi.fn(),
}));

// Mock Tauri invoke function
vi.mock('@tauri-apps/api/core', () => ({
  invoke: vi.fn(),
}))

// Mock @janhq/core
vi.mock('@janhq/core', () => ({
  ServiceKeys: {
    Downloads: { id: 'downloads' },
    Llamacpp: { id: 'llamacpp' },
  },
  logger: {
    debug: vi.fn(),
    info: vi.fn(),
    warn: vi.fn(),
    error: vi.fn(),
  },
  getJanDataFolderPath: vi.fn(),
  fs: {
    existsSync: vi.fn(),
    readdirSync: vi.fn(),
    fileStat: vi.fn(),
    mkdir: vi.fn(),
    rm: vi.fn(),
    readYaml: vi.fn(),
    writeYaml: vi.fn(),
  },
  joinPath: vi.fn(),
  modelInfo: {},
  SessionInfo: {},
  UnloadResult: {},
  chatCompletion: {},
  chatCompletionChunk: {},
  ImportOptions: {},
  chatCompletionRequest: {},
  events: {
    emit: vi.fn(),
  },
  AppEvent: {
    onModelImported: 'onModelImported',
    onBackendVerificationFailed: 'onBackendVerificationFailed',
  },
  DownloadEvent: {
    onFileDownloadUpdate: 'onFileDownloadUpdate',
    onFileDownloadStopped: 'onFileDownloadStopped',
    onModelValidationFailed: 'onModelValidationFailed',
  },
  AIEngine: class {},
}))