@powersync/service-core 0.8.7 → 0.8.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,153 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+ import { container } from '@powersync/lib-services-framework';
3
+ import { startupCheck, livenessCheck, readinessCheck } from '../../../src/routes/endpoints/probes.js';
4
+
5
+ // Mock the container
6
+ vi.mock('@powersync/lib-services-framework', () => ({
7
+ container: {
8
+ probes: {
9
+ state: vi.fn()
10
+ }
11
+ },
12
+ router: {
13
+ HTTPMethod: {
14
+ GET: 'GET'
15
+ },
16
+ RouterResponse: class RouterResponse {
17
+ status: number;
18
+ data: any;
19
+ headers: Record<string, string>;
20
+ afterSend: () => Promise<void>;
21
+ __micro_router_response = true;
22
+
23
+ constructor({ status, data, headers, afterSend }: {
24
+ status?: number;
25
+ data: any;
26
+ headers?: Record<string, string>;
27
+ afterSend?: () => Promise<void>;
28
+ }) {
29
+ this.status = status || 200;
30
+ this.data = data;
31
+ this.headers = headers || { 'Content-Type': 'application/json' };
32
+ this.afterSend = afterSend ?? (() => Promise.resolve());
33
+ }
34
+ }
35
+ }
36
+ }));
37
+
38
+ describe('Probe Routes', () => {
39
+ beforeEach(() => {
40
+ vi.clearAllMocks();
41
+ });
42
+
43
+ describe('startupCheck', () => {
44
+ it('has the correct route definitions', () => {
45
+ expect(startupCheck.path).toBe('/probes/startup');
46
+ expect(startupCheck.method).toBe('GET');
47
+ });
48
+
49
+ it('returns 200 when started is true', async () => {
50
+ const mockState = {
51
+ started: true,
52
+ ready: true,
53
+ touched_at: new Date()
54
+ };
55
+
56
+ vi.mocked(container.probes.state).mockReturnValue(mockState);
57
+
58
+ const response = await startupCheck.handler();
59
+
60
+ expect(response.status).toEqual(200);
61
+ expect(response.data).toEqual(mockState);
62
+ });
63
+
64
+ it('returns 400 when started is false', async () => {
65
+ const mockState = {
66
+ started: false,
67
+ ready: false,
68
+ touched_at: new Date()
69
+ };
70
+
71
+ vi.mocked(container.probes.state).mockReturnValue(mockState);
72
+
73
+ const response = await startupCheck.handler();
74
+
75
+ expect(response.status).toBe(400);
76
+ expect(response.data).toEqual(mockState);
77
+ });
78
+ });
79
+
80
+ describe('livenessCheck', () => {
81
+ it('has the correct route definitions', () => {
82
+ expect(livenessCheck.path).toBe('/probes/liveness');
83
+ expect(livenessCheck.method).toBe('GET');
84
+ });
85
+
86
+ it('returns 200 when last touch was less than 10 seconds ago', async () => {
87
+ const mockState = {
88
+ started: true,
89
+ ready: true,
90
+ touched_at: new Date(Date.now() - 9000) // 11 seconds ago
91
+ };
92
+
93
+ vi.mocked(container.probes.state).mockReturnValue(mockState);
94
+
95
+ const response = await livenessCheck.handler();
96
+
97
+ expect(response.status).toBe(200);
98
+ expect(response.data).toEqual(mockState);
99
+ });
100
+
101
+ it('returns 400 when last touch was more than 10 seconds ago', async () => {
102
+ const mockState = {
103
+ started: true,
104
+ ready: true,
105
+ touched_at: new Date(Date.now() - 11000)
106
+ };
107
+
108
+ vi.mocked(container.probes.state).mockReturnValue(mockState);
109
+
110
+ const response = await livenessCheck.handler();
111
+
112
+ expect(response.status).toBe(400);
113
+ expect(response.data).toEqual(mockState);
114
+ });
115
+ });
116
+
117
+ describe('readinessCheck', () => {
118
+ it('has the correct route definitions', () => {
119
+ expect(readinessCheck.path).toBe('/probes/readiness');
120
+ expect(readinessCheck.method).toBe('GET');
121
+ });
122
+
123
+ it('returns 200 when ready is true', async () => {
124
+ const mockState = {
125
+ started: true,
126
+ ready: true,
127
+ touched_at: new Date()
128
+ };
129
+
130
+ vi.mocked(container.probes.state).mockReturnValue(mockState);
131
+
132
+ const response = await readinessCheck.handler();
133
+
134
+ expect(response.status).toBe(200);
135
+ expect(response.data).toEqual(mockState);
136
+ });
137
+
138
+ it('returns 400 when ready is false', async () => {
139
+ const mockState = {
140
+ started: true,
141
+ ready: false,
142
+ touched_at: new Date()
143
+ };
144
+
145
+ vi.mocked(container.probes.state).mockReturnValue(mockState);
146
+
147
+ const response = await readinessCheck.handler();
148
+
149
+ expect(response.status).toBe(400);
150
+ expect(response.data).toEqual(mockState);
151
+ });
152
+ });
153
+ });