|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import { beforeEach, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +const { getSystemLocaleMock, getUserDefaultMock } = vi.hoisted(() => ({ |
| 7 | + getSystemLocaleMock: vi.fn(() => "en-GB"), |
| 8 | + getUserDefaultMock: vi.fn((_key: string) => false), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("electron", () => ({ |
| 12 | + app: { |
| 13 | + getSystemLocale: getSystemLocaleMock, |
| 14 | + }, |
| 15 | + systemPreferences: { |
| 16 | + getUserDefault: getUserDefaultMock, |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +import * as ElectronApp from "./ElectronApp.ts"; |
| 21 | + |
| 22 | +describe("ElectronApp", () => { |
| 23 | + beforeEach(() => { |
| 24 | + getSystemLocaleMock.mockReset(); |
| 25 | + getUserDefaultMock.mockReset(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("keeps the macOS region override and hour cycle in the locale tag", () => { |
| 29 | + assert.strictEqual(ElectronApp.toSystemLocaleTag("en_GB"), "en-GB"); |
| 30 | + assert.strictEqual(ElectronApp.toSystemLocaleTag("en-US@rg=nlzzzz", "h12"), "en-NL-u-hc-h12"); |
| 31 | + assert.strictEqual(ElectronApp.toSystemLocaleTag("en_US@rg=nlzzzz"), "en-NL"); |
| 32 | + assert.strictEqual(ElectronApp.toSystemLocaleTag("en-GB", "h23"), "en-GB-u-hc-h23"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("drops ICU keywords Intl cannot read instead of losing the whole locale", () => { |
| 36 | + assert.strictEqual(ElectronApp.toSystemLocaleTag("de_DE@calendar=buddhist"), "de-DE"); |
| 37 | + }); |
| 38 | + |
| 39 | + it.effect("follows LC_TIME on Linux when it differs from the language", () => |
| 40 | + Effect.gen(function* () { |
| 41 | + const electronApp = yield* ElectronApp.ElectronApp; |
| 42 | + const systemLocale = yield* electronApp.systemLocale.pipe( |
| 43 | + Effect.provideService(HostProcess.Platform, "linux"), |
| 44 | + Effect.provideService(HostProcess.Environment, { |
| 45 | + LANG: "en_US.UTF-8", |
| 46 | + LC_TIME: "de_DE.UTF-8", |
| 47 | + }), |
| 48 | + ); |
| 49 | + |
| 50 | + assert.strictEqual(systemLocale, "de-DE"); |
| 51 | + }).pipe(Effect.provide(ElectronApp.layer)), |
| 52 | + ); |
| 53 | + |
| 54 | + it.effect("falls back to Chromium's locale on Linux when the time locale is C", () => |
| 55 | + Effect.gen(function* () { |
| 56 | + getSystemLocaleMock.mockImplementationOnce(() => "fr-FR"); |
| 57 | + const electronApp = yield* ElectronApp.ElectronApp; |
| 58 | + const systemLocale = yield* electronApp.systemLocale.pipe( |
| 59 | + Effect.provideService(HostProcess.Platform, "linux"), |
| 60 | + Effect.provideService(HostProcess.Environment, { LANG: "C.UTF-8" }), |
| 61 | + ); |
| 62 | + |
| 63 | + assert.strictEqual(systemLocale, "fr-FR"); |
| 64 | + }).pipe(Effect.provide(ElectronApp.layer)), |
| 65 | + ); |
| 66 | +}); |
| 67 | + |
| 68 | +it.effect("reads both macOS hour preferences without overriding explicit formats", () => |
| 69 | + Effect.gen(function* () { |
| 70 | + for (const [preference, expected] of [ |
| 71 | + ["AppleICUForce12HourTime", "en-NL-u-hc-h12"], |
| 72 | + ["AppleICUForce24HourTime", "en-NL-u-hc-h23"], |
| 73 | + ["", "en-NL"], |
| 74 | + ]) { |
| 75 | + getSystemLocaleMock.mockReturnValue("en_US@rg=nlzzzz"); |
| 76 | + getUserDefaultMock.mockImplementation((key) => key === preference); |
| 77 | + const locale = yield* ElectronApp.make.systemLocale.pipe( |
| 78 | + Effect.provideService(HostProcess.Platform, "darwin"), |
| 79 | + ); |
| 80 | + assert.strictEqual(locale, expected); |
| 81 | + for (const hour12 of [true, false]) { |
| 82 | + assert.strictEqual( |
| 83 | + new Intl.DateTimeFormat(locale, { hour: "numeric", hour12 }).resolvedOptions().hour12, |
| 84 | + hour12, |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + }), |
| 89 | +); |
0 commit comments