File size: 6,251 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
 * 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);
  });
});