fix: enable initial mockIPC

This commit is contained in:
ndom91 2024-05-04 19:28:29 +02:00
parent 01e8a4d28d
commit 4c2d3a137b
No known key found for this signature in database
4 changed files with 194 additions and 48 deletions

View File

@ -3,9 +3,11 @@ import { error as logErrorToFile } from 'tauri-plugin-log-api';
import type { NavigationEvent } from '@sveltejs/kit';
function myErrorHandler({ error, event }: { error: any; event: NavigationEvent }) {
console.error(error.message + '\n' + error.stack);
logErrorToFile(error.toString());
console.error('An error occurred on the client side:', error, event);
if (typeof window.__TAURI_IPC__ === 'function') {
console.error(error.message + '\n' + error.stack);
logErrorToFile(error.toString());
}
console.error('An error occurred on the client side:', error, event);
}
export const handleError = handleErrorWithSentry(myErrorHandler);
@ -19,7 +21,9 @@ export const handleError = handleErrorWithSentry(myErrorHandler);
*/
const originalUnhandledHandler = window.onunhandledrejection;
window.onunhandledrejection = (event: PromiseRejectionEvent) => {
logErrorToFile('Unhandled exception: ' + event?.reason + ' ' + event?.reason?.sourceURL);
console.log('Unhandled exception', event.reason);
originalUnhandledHandler?.bind(window)(event);
if (typeof window.__TAURI_IPC__ === 'function') {
logErrorToFile('Unhandled exception: ' + event?.reason + ' ' + event?.reason?.sourceURL);
console.log('Unhandled exception', event.reason);
originalUnhandledHandler?.bind(window)(event);
}
};

View File

@ -0,0 +1,82 @@
import type { Project } from '$lib/backend/projects'
import type { BaseBranch, RemoteCommit, Author } from '$lib/vbranches/types'
import type { User } from '$lib/stores/user'
export type Constructor<T = any> = new (...args: any[]) => T;
export type Class<T = any> = InstanceType<Constructor<T>>;
export const project: Class<Project> = {
id: "abc123",
title: 'My Project',
description: "A project description",
path: "/Users/user/project",
api: {
name: 'github',
description: 'description',
repository_id: 'abc123',
git_url: 'https://github.com/user/project',
created_at: '2021-07-01T00:00:00Z',
updated_at: '2021-07-01T00:01:00Z',
sync: false
},
preferred_key: {
local: { private_key_path: '/Users/.ssh/id_rsa' },
},
ok_with_force_push: true,
omit_certificate_check: true,
use_diff_context: true,
vscodePath: '/Users/user/project'
}
export const author: Class<Author> = {
name: 'John Snow',
email: 'user@company.com',
gravatarUrl: new URL('https://gravatar.com/abc123'),
isBot: false
}
// @ts-expect-error
export const remoteCommit: Class<RemoteCommit> = {
id: 'abc123',
author,
description: 'A commit message',
createdAt: new Date(),
isLocal: false,
}
// @ts-expect-error
export const baseBranch: Class<BaseBranch> = {
branchName: 'main',
remoteName: 'origin',
remoteUrl: 'ssh://github.com/user/project.git',
baseSha: '90c225edcc74b31718a9cd8963c1bc89c17d8864',
currentSha: '90c225edcc74b31718a9cd8963c1bc89c17d8864',
behind: 0,
upstreamCommits: [],
recentCommits: [remoteCommit],
lastFetchedMs: 1714843209991,
lastFetched: new Date(),
repoBaseUrl: 'https://github.com/user/project',
shortName: 'main',
branchUrl: () => 'https://github.com/user/project',
commitUrl: () => 'https://github.com/user/project',
isGitlab: false,
isBitBucket: false
}
export const user: Class<User> = {
id: 123,
name: "John Snow",
given_name: "John",
family_name: "Snow",
email: "user@comapny.com",
picture: "",
locale: "en_EN",
created_at: "2021-07-01T00:00:00Z",
updated_at: "2021-07-01T00:01:00Z",
access_token: "abc123",
role: "ADMIN",
supporter: true,
github_access_token: undefined,
github_username: undefined
}

View File

@ -0,0 +1,53 @@
import { mockIPC } from "@tauri-apps/api/mocks";
import { mockWindows } from '@tauri-apps/api/mocks';
import { baseBranch, project, user } from "./fixtures"
export const mockTauri = () => {
// TODO: Set localSTorage like 'lastProject'
mockIPC((cmd, args) => {
console.log("MOCKIPC.CMD", cmd, args)
// console.log(JSON.stringify(args, null, 2))
// console.groupEnd()
// Open Project Dialog
if (cmd === "tauri" && args.__tauriModule === "Dialog" && args.message?.cmd === "openDialog") {
return "/Users/user/project"
}
// List Projects
if (cmd === "list_projects") {
console.log('mock.projects', [project])
return [project]
}
// List Project
if (cmd === "get_project" && args.id === "abc123") {
return project
}
// Get HEAD
if (cmd === "git_head") {
return "refs/heads/abc123"
}
if (cmd === "menu_item_set_enabled") {
return true
}
if (cmd === "fetch_from_target") {
return true
}
if (cmd === "get_base_branch_data") {
console.log('mock.baseBranch', baseBranch)
return baseBranch
}
if (cmd === "get_user") {
return user
}
});
mockWindows('main');
}

View File

@ -12,6 +12,8 @@ import { GitHubService } from '$lib/github/service';
import { UserService } from '$lib/stores/user';
import lscache from 'lscache';
import { BehaviorSubject, config } from 'rxjs';
import { mockTauri } from '$lib/testing/index';
import { env } from "$env/dynamic/public";
// call on startup so we don't accumulate old items
lscache.flushExpired();
@ -24,51 +26,56 @@ export const prerender = false;
export const csr = true;
export async function load() {
appErrorReportingEnabled()
.onDisk()
.then((enabled) => {
if (enabled) initSentry();
});
appMetricsEnabled()
.onDisk()
.then((enabled) => {
if (enabled) initPostHog();
});
// Mock Tauri API during E2E tests
if (env.PUBLIC_TESTING) {
mockTauri()
}
// TODO: Find a workaround to avoid this dynamic import
// https://github.com/sveltejs/kit/issues/905
const defaultPath = await (await import('@tauri-apps/api/path')).homeDir();
appErrorReportingEnabled()
.onDisk()
.then((enabled) => {
if (enabled) initSentry();
});
appMetricsEnabled()
.onDisk()
.then((enabled) => {
if (enabled) initPostHog();
});
const httpClient = new HttpClient();
const authService = new AuthService();
const projectService = new ProjectService(defaultPath, httpClient);
const updaterService = new UpdaterService();
const promptService = new PromptService();
const userService = new UserService(httpClient);
// TODO: Find a workaround to avoid this dynamic import
// https://github.com/sveltejs/kit/issues/905
const defaultPath = await (await import('@tauri-apps/api/path')).homeDir();
// We're declaring a remoteUrl$ observable here that is written to by `BaseBranchService`. This
// is a bit awkard, but `GitHubService` needs to be available at the root scoped layout.ts, such
// that we can perform actions related to GitHub that do not depend on repo information.
// We should evaluate whether or not to split this service into two separate services. That
// way we would not need `remoteUrl$` for the non-repo service, and therefore the other one
// could easily get an observable of the remote url from `BaseBranchService`.
const remoteUrl$ = new BehaviorSubject<string | undefined>(undefined);
const githubService = new GitHubService(userService.accessToken$, remoteUrl$);
const httpClient = new HttpClient();
const authService = new AuthService();
const projectService = new ProjectService(defaultPath, httpClient);
const updaterService = new UpdaterService();
const promptService = new PromptService();
const userService = new UserService(httpClient);
const gitConfig = new GitConfigService();
const aiService = new AIService(gitConfig, httpClient);
// We're declaring a remoteUrl$ observable here that is written to by `BaseBranchService`. This
// is a bit awkard, but `GitHubService` needs to be available at the root scoped layout.ts, such
// that we can perform actions related to GitHub that do not depend on repo information.
// We should evaluate whether or not to split this service into two separate services. That
// way we would not need `remoteUrl$` for the non-repo service, and therefore the other one
// could easily get an observable of the remote url from `BaseBranchService`.
const remoteUrl$ = new BehaviorSubject<string | undefined>(undefined);
const githubService = new GitHubService(userService.accessToken$, remoteUrl$);
return {
authService,
cloud: httpClient,
githubService,
projectService,
updaterService,
promptService,
userService,
// These observables are provided for convenience
remoteUrl$,
gitConfig,
aiService
};
const gitConfig = new GitConfigService();
const aiService = new AIService(gitConfig, httpClient);
return {
authService,
cloud: httpClient,
githubService,
projectService,
updaterService,
promptService,
userService,
// These observables are provided for convenience
remoteUrl$,
gitConfig,
aiService
};
}