Skip to content

Colota - Android GPS Location tracker

4.0 KB
Raw Blame
/**
 * Copyright (C) 2026 Max Dietrich
 * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
 */

jest.mock("../../services/NativeLocationService", () => ({
  __esModule: true,
  default: { getBuildConfig: jest.fn() }
}))

import i18next from "i18next"
import NativeLocationService from "../../services/NativeLocationService"
import { initI18n, SUPPORTED_LANGUAGES, t } from "../index"
import en from "../locales/en.json"
import type { TranslationKey } from "../options"

const mockGetBuildConfig = NativeLocationService.getBuildConfig as jest.Mock

describe("initI18n", () => {
  beforeEach(() => {
    mockGetBuildConfig.mockReset()
  })

  it("uses the device language when a catalog exists for it", () => {
    mockGetBuildConfig.mockReturnValue({ APP_LANGUAGE: "en-GB" })

    // The device tag carries a region and the catalogs do not, so only the base tag can match.
    expect(initI18n()).toBe("en")
  })

  it("falls back to English for a language with no catalog", () => {
    mockGetBuildConfig.mockReturnValue({ APP_LANGUAGE: "pl-PL" })

    expect(initI18n()).toBe("en")
  })

  it("falls back to English when the native module is unavailable", () => {
    // getBuildConfig returns null when the bridge module is missing, which must not throw during
    // module-scope init or the app fails to start rather than starting in English.
    mockGetBuildConfig.mockReturnValue(null)

    expect(initI18n()).toBe("en")
  })

  it("resolves keys synchronously, so a first paint never shows raw keys", () => {
    mockGetBuildConfig.mockReturnValue({ APP_LANGUAGE: "en-US" })

    initI18n()

    expect(t("appearance.theme")).toBe("Theme")
  })

  it("returns the key itself when it is missing, rather than empty text", () => {
    mockGetBuildConfig.mockReturnValue({ APP_LANGUAGE: "en" })
    initI18n()

    expect(t("appearance.doesNotExist" as TranslationKey)).toBe("appearance.doesNotExist")
  })
})

describe("catalog", () => {
  it("every supported language is a real i18next resource after init", () => {
    mockGetBuildConfig.mockReturnValue({ APP_LANGUAGE: "en" })
    initI18n()

    for (const lang of SUPPORTED_LANGUAGES) {
      expect(i18next.getResourceBundle(lang, "translation")).toBeTruthy()
    }
  })

  it("has no empty values, which would render as blank UI", () => {
    const empty = Object.entries(en).filter(([, value]) => String(value).trim() === "")

    expect(empty).toEqual([])
  })
})

describe("plurals without Intl.PluralRules", () => {
  const original = Intl.PluralRules

  afterEach(() => {
    Object.defineProperty(Intl, "PluralRules", { value: original, configurable: true, writable: true })
  })

  it("still picks the English one and other forms, because Hermes ships no PluralRules and Node does", async () => {
    Object.defineProperty(Intl, "PluralRules", { value: undefined, configurable: true, writable: true })
    const hermesLike = i18next.createInstance()
    await hermesLike.init({ lng: "en", resources: { en: { translation: en } }, keySeparator: false })

    expect(hermesLike.t("settings.offlineMaps.areas", { count: 1, n: "1" })).toBe("1 area")
    expect(hermesLike.t("settings.offlineMaps.areas", { count: 2, n: "2" })).toBe("2 areas")
  })
})

describe("prominent disclosure wording", () => {
  // Google Play reviewed this text for the background location declaration; an edit has to be deliberate.
  it("keeps the English location disclosure exactly as submitted", () => {
    expect([en["disclosure.location.p1"], en["disclosure.location.p2"], en["disclosure.location.p3"]]).toEqual([
      "Colota collects location data to enable GPS tracking and sending your position to your configured server, even when the app is closed or not in use.",
      "This data is sent only to the server you set up. No data is shared with third parties.",
      "While tracking runs, a persistent notification shows its status. Battery optimization should be disabled for reliable background operation."
    ])
    expect(en["disclosure.location.confirm"]).toBe("Agree")
  })
})