Download src/lib/native/__tests__/native.test.ts from NAP1320/RotNot: direct link, hf CLI and curl.
- Browser
- Download file 6.25 kB
-
https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/native/__tests__/native.test.ts
- Command line
-
hf download hf://spaces/NAP1320/RotNot/src/lib/native/__tests__/native.test.ts
-
curl -L -o native.test.ts https://huggingface.co/spaces/NAP1320/RotNot/resolve/main/src/lib/native/__tests__/native.test.ts
6.25 kB
| /** | |
| * Tests for the RotNot native bridge (src/lib/native/index.ts) | |
| * | |
| * The bridge wraps Capacitor plugins behind feature-detection. | |
| * All tests run in a plain Node environment β Capacitor plugins are NOT | |
| * available, so all exported functions must be graceful no-ops. | |
| * | |
| * isNative() returns false in Node (no window.Capacitor), so we test only | |
| * the no-op / web-fallback behaviour of all exported APIs. | |
| */ | |
| import { describe, it, expect } from "vitest"; | |
| import { isNative, nativeHaptics, nativePush, nativeNotifications, nativeStatusBar } from "../index"; | |
| // βββ isNative βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("isNative", () => { | |
| it("returns false in a Node (non-browser) environment", () => { | |
| // window is undefined in Node β isNative() guards against this | |
| expect(isNative()).toBe(false); | |
| }); | |
| it("returns a boolean", () => { | |
| expect(typeof isNative()).toBe("boolean"); | |
| }); | |
| }); | |
| // βββ nativeHaptics ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("nativeHaptics", () => { | |
| it("tap() resolves without error in non-native context", async () => { | |
| await expect(nativeHaptics.tap()).resolves.toBeUndefined(); | |
| }); | |
| it("xpGain() resolves without error in non-native context", async () => { | |
| await expect(nativeHaptics.xpGain()).resolves.toBeUndefined(); | |
| }); | |
| it("levelUp() resolves without error in non-native context", async () => { | |
| await expect(nativeHaptics.levelUp()).resolves.toBeUndefined(); | |
| }); | |
| it("correct() resolves without error in non-native context", async () => { | |
| await expect(nativeHaptics.correct()).resolves.toBeUndefined(); | |
| }); | |
| it("error() resolves without error in non-native context", async () => { | |
| await expect(nativeHaptics.error()).resolves.toBeUndefined(); | |
| }); | |
| it("all haptic functions are safe to call multiple times", async () => { | |
| for (let i = 0; i < 5; i++) { | |
| await nativeHaptics.xpGain(); | |
| } | |
| // no throw = pass | |
| expect(true).toBe(true); | |
| }); | |
| it("all haptic methods exist on the exported object", () => { | |
| expect(typeof nativeHaptics.tap).toBe("function"); | |
| expect(typeof nativeHaptics.xpGain).toBe("function"); | |
| expect(typeof nativeHaptics.levelUp).toBe("function"); | |
| expect(typeof nativeHaptics.correct).toBe("function"); | |
| expect(typeof nativeHaptics.error).toBe("function"); | |
| }); | |
| }); | |
| // βββ nativePush βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("nativePush", () => { | |
| it("requestPermission() returns { granted: false } in non-native context", async () => { | |
| const result = await nativePush.requestPermission(); | |
| expect(result).toEqual({ granted: false }); | |
| }); | |
| it("requestPermission() never throws", async () => { | |
| await expect(nativePush.requestPermission()).resolves.not.toThrow(); | |
| }); | |
| it("requestPermission() returns a PushPermissionResult shape", async () => { | |
| const result = await nativePush.requestPermission(); | |
| expect(typeof result.granted).toBe("boolean"); | |
| // token is optional β undefined is fine | |
| expect("token" in result || result.token === undefined).toBe(true); | |
| }); | |
| it("token is undefined when not granted", async () => { | |
| const result = await nativePush.requestPermission(); | |
| expect(result.token).toBeUndefined(); | |
| }); | |
| }); | |
| // βββ nativeNotifications ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("nativeNotifications", () => { | |
| it("scheduleStreakReminder() resolves without error (non-native)", async () => { | |
| await expect( | |
| nativeNotifications.scheduleStreakReminder({ streakDays: 5 }) | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("scheduleStreakReminder() accepts zero streak days", async () => { | |
| await expect( | |
| nativeNotifications.scheduleStreakReminder({ streakDays: 0 }) | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("scheduleStreakReminder() accepts delayMinutes override", async () => { | |
| await expect( | |
| nativeNotifications.scheduleStreakReminder({ streakDays: 3, delayMinutes: 30 }) | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("scheduleReviewReminder() resolves without error (non-native)", async () => { | |
| const dueAt = new Date(Date.now() + 86_400_000); | |
| await expect( | |
| nativeNotifications.scheduleReviewReminder(3, dueAt) | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("scheduleReviewReminder() handles singular count", async () => { | |
| const dueAt = new Date(Date.now() + 3_600_000); | |
| await expect( | |
| nativeNotifications.scheduleReviewReminder(1, dueAt) | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("scheduleReviewReminder() handles zero count", async () => { | |
| const dueAt = new Date(Date.now() + 60_000); | |
| await expect( | |
| nativeNotifications.scheduleReviewReminder(0, dueAt) | |
| ).resolves.toBeUndefined(); | |
| }); | |
| it("cancelAll() resolves without error (non-native)", async () => { | |
| await expect(nativeNotifications.cancelAll()).resolves.toBeUndefined(); | |
| }); | |
| it("all notification functions are exported", () => { | |
| expect(typeof nativeNotifications.scheduleStreakReminder).toBe("function"); | |
| expect(typeof nativeNotifications.scheduleReviewReminder).toBe("function"); | |
| expect(typeof nativeNotifications.cancelAll).toBe("function"); | |
| }); | |
| }); | |
| // βββ nativeStatusBar ββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| describe("nativeStatusBar", () => { | |
| it("setDark() resolves without error (non-native)", async () => { | |
| await expect(nativeStatusBar.setDark()).resolves.toBeUndefined(); | |
| }); | |
| it("setDark() can be called multiple times safely", async () => { | |
| await nativeStatusBar.setDark(); | |
| await nativeStatusBar.setDark(); | |
| expect(true).toBe(true); | |
| }); | |
| }); | |