@maverick006/vibeguard 1.0.11 → 1.0.12

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,96 @@
1
+ import { saveCredentials, loadCredentials, clearCredentials, getCredentialsPath } from '../src/credentials';
2
+ import fs from 'fs';
3
+
4
+ describe('VibeGuard CLI Auth & Credentials Management', () => {
5
+ const originalEnv = process.env;
6
+ let originalFileContent: string | null = null;
7
+ const credsPath = getCredentialsPath();
8
+
9
+ beforeAll(() => {
10
+ // Backup existing credentials if any
11
+ if (fs.existsSync(credsPath)) {
12
+ originalFileContent = fs.readFileSync(credsPath, 'utf-8');
13
+ }
14
+ });
15
+
16
+ afterAll(() => {
17
+ // Restore original credentials
18
+ if (originalFileContent !== null) {
19
+ fs.writeFileSync(credsPath, originalFileContent, 'utf-8');
20
+ } else {
21
+ clearCredentials();
22
+ }
23
+ process.env = originalEnv;
24
+ });
25
+
26
+ beforeEach(() => {
27
+ clearCredentials();
28
+ });
29
+
30
+ test('1. loadCredentials returns null when no credentials file exists', () => {
31
+ expect(loadCredentials()).toBeNull();
32
+ });
33
+
34
+ test('2. saveCredentials persists user session and loadCredentials retrieves it', () => {
35
+ const mockUser = {
36
+ id: 'test-user-uuid-123',
37
+ email: 'engineer@vibeguard.io',
38
+ name: 'Test Engineer'
39
+ };
40
+ const mockToken = 'mock-jwt-token-header.payload.signature';
41
+ const apiUrl = 'http://localhost:3001';
42
+
43
+ saveCredentials({
44
+ token: mockToken,
45
+ user: mockUser,
46
+ apiUrl
47
+ });
48
+
49
+ const loaded = loadCredentials();
50
+ expect(loaded).not.toBeNull();
51
+ expect(loaded?.token).toBe(mockToken);
52
+ expect(loaded?.user.id).toBe(mockUser.id);
53
+ expect(loaded?.user.email).toBe(mockUser.email);
54
+ expect(loaded?.user.name).toBe(mockUser.name);
55
+ expect(loaded?.apiUrl).toBe(apiUrl);
56
+ expect(loaded?.savedAt).toBeDefined();
57
+ });
58
+
59
+ test('3. clearCredentials removes stored credentials cleanly', () => {
60
+ saveCredentials({
61
+ token: 'jwt-to-delete',
62
+ user: { id: 'u1', email: 'delete-me@vibeguard.io' }
63
+ });
64
+ expect(loadCredentials()).not.toBeNull();
65
+
66
+ const cleared = clearCredentials();
67
+ expect(cleared).toBe(true);
68
+ expect(loadCredentials()).toBeNull();
69
+ expect(fs.existsSync(credsPath)).toBe(false);
70
+ });
71
+
72
+ test('4. loadCredentials handles corrupt json gracefully without crashing', () => {
73
+ const dir = require('path').dirname(credsPath);
74
+ if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
75
+ fs.writeFileSync(credsPath, '{ invalid json corrupted string }', 'utf-8');
76
+
77
+ expect(loadCredentials()).toBeNull();
78
+ });
79
+
80
+ test('5. CLI sync status contract verification', () => {
81
+ // Unauthenticated: sync attempt should be rejected
82
+ const noCreds = loadCredentials();
83
+ expect(noCreds).toBeNull();
84
+
85
+ // When creds exist, authorization header format is standard Bearer
86
+ saveCredentials({
87
+ token: 'valid-test-token',
88
+ user: { id: 'u-abc', email: 'user@vibeguard.io' }
89
+ });
90
+
91
+ const activeCreds = loadCredentials();
92
+ expect(activeCreds).not.toBeNull();
93
+ const authHeader = `Bearer ${activeCreds!.token}`;
94
+ expect(authHeader).toBe('Bearer valid-test-token');
95
+ });
96
+ });