test: test if tauri env is recognized correctly

Part-of: <https://dev.funkwhale.audio/funkwhale/funkwhale/-/merge_requests/2701>
environments/review-docs-v2-ov-8q6uyo/deployments/19325
Kasper Seweryn 2024-02-05 13:51:42 +01:00
rodzic cb8725a838
commit f3a7394461
7 zmienionych plików z 90 dodań i 1588 usunięć

Wyświetl plik

@ -102,7 +102,6 @@
"unplugin-vue-macros": "2.4.6",
"utility-types": "3.10.0",
"vite": "5.1.3",
"vite-plugin-pwa": "0.14.4",
"vitest": "1.3.1",
"vue-tsc": "1.6.5",
"workbox-core": "6.5.4",

Wyświetl plik

@ -17,6 +17,7 @@ import Logo from '~/components/Logo.vue'
import useThemeList from '~/composables/useThemeList'
import useTheme from '~/composables/useTheme'
import { isTauri as checkTauri } from '~/composables/tauri'
interface Props {
width: number
@ -103,7 +104,7 @@ watch(locale, (locale) => {
})
const isProduction = import.meta.env.PROD
const isTauri = 'TAURI_ENV_PLATFORM' in import.meta.env
const isTauri = checkTauri()
const showUserModal = ref(false)
const showThemeModal = ref(false)

Wyświetl plik

@ -0,0 +1,3 @@
export const isTauri = () => {
return 'TAURI_ENV_PLATFORM' in import.meta.env
}

Wyświetl plik

@ -5,6 +5,7 @@ import axios from 'axios'
import { merge } from 'lodash-es'
import useLogger from '~/composables/useLogger'
import { useQueue } from '~/composables/audio/queue'
import { isTauri } from '~/composables/tauri'
export interface State {
frontSettings: FrontendSettings
@ -123,15 +124,15 @@ interface Settings {
const logger = useLogger()
// Use some arbitrary url that will trigger the instance chooser, this needs to be a valid url
export const TAURI_DEFAULT_INSTANCE_URL = 'tauri://force-instance-chooser/'
export const TAURI_DEFAULT_INSTANCE_URL = 'http://localhost/force-instance-chooser/'
// We have several way to guess the API server url. By order of precedence:
// 0. use the url provided in settings.json, if any. That's a lazy operation done by already initialized store.
// 1. force instance chooser, if in tauri app
// 2. use the url provided in settings.json, if any
// 3. use the url specified when building via VUE_APP_INSTANCE_URL
// 4. use the current url
const DEFAULT_INSTANCE_URL = (() => {
if ('TAURI_ENV_PLATFORM' in import.meta.env) {
// 2. use the url specified when building via VUE_APP_INSTANCE_URL
// 3. use the current url
export const findDefaultInstanceUrl = () => {
if (isTauri()) {
return TAURI_DEFAULT_INSTANCE_URL
}
@ -142,7 +143,9 @@ const DEFAULT_INSTANCE_URL = (() => {
}
return `${location.origin}/`
})()
}
const DEFAULT_INSTANCE_URL = findDefaultInstanceUrl()
const store: Module<State, RootState> = {
namespaced: true,

Wyświetl plik

@ -0,0 +1,17 @@
import { vi } from 'vitest'
import { isTauri } from '~/composables/tauri'
afterEach(() => {
vi.unstubAllEnvs()
})
test('Correctly detects Tauri environment', () => {
// Stub the Tauri environment variable
vi.stubEnv('TAURI_ENV_PLATFORM', 'tauri')
expect(isTauri()).toBe(true)
})
test('Correctly detects browser environment', () => {
expect(isTauri()).toBe(false)
})

Wyświetl plik

@ -0,0 +1,28 @@
import { vi } from 'vitest'
// HACK: First we import the global store (and instance store indirectly) so that we don't fall into error pitfall
import _store from '~/store'
import { findDefaultInstanceUrl, TAURI_DEFAULT_INSTANCE_URL } from '~/store/instance'
afterEach(() => {
vi.unstubAllEnvs()
vi.unstubAllGlobals()
})
describe('findDefaultInstanceUrl', () => {
test('tauri', () => {
vi.stubEnv('TAURI_ENV_PLATFORM', 'tauri')
expect(findDefaultInstanceUrl()).toBe(TAURI_DEFAULT_INSTANCE_URL)
})
test('environment variable', () => {
vi.stubEnv('VUE_APP_INSTANCE_URL', 'https://example.com')
expect(findDefaultInstanceUrl()).toBe('https://example.com/')
})
test('location origin', () => {
vi.stubGlobal('location', new URL('https://example.com'))
expect(findDefaultInstanceUrl()).toBe('https://example.com/')
})
})

Plik diff jest za duży Load Diff