cc-lark 0.1.1
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.
- package/.github/workflows/ci.yml +47 -0
- package/.github/workflows/release.yml +47 -0
- package/.github/workflows/sync-upstream.yml +127 -0
- package/.prettierrc.json +7 -0
- package/README.md +214 -0
- package/dist/core/api-error.d.ts +193 -0
- package/dist/core/api-error.d.ts.map +1 -0
- package/dist/core/api-error.js +263 -0
- package/dist/core/api-error.js.map +1 -0
- package/dist/core/auth-errors.d.ts +13 -0
- package/dist/core/auth-errors.d.ts.map +1 -0
- package/dist/core/auth-errors.js +14 -0
- package/dist/core/auth-errors.js.map +1 -0
- package/dist/core/config.d.ts +60 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +115 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/device-flow.d.ts +80 -0
- package/dist/core/device-flow.d.ts.map +1 -0
- package/dist/core/device-flow.js +231 -0
- package/dist/core/device-flow.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +16 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/lark-client.d.ts +136 -0
- package/dist/core/lark-client.d.ts.map +1 -0
- package/dist/core/lark-client.js +315 -0
- package/dist/core/lark-client.js.map +1 -0
- package/dist/core/token-store.d.ts +67 -0
- package/dist/core/token-store.d.ts.map +1 -0
- package/dist/core/token-store.js +215 -0
- package/dist/core/token-store.js.map +1 -0
- package/dist/core/types.d.ts +286 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +11 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/uat-client.d.ts +64 -0
- package/dist/core/uat-client.d.ts.map +1 -0
- package/dist/core/uat-client.js +227 -0
- package/dist/core/uat-client.js.map +1 -0
- package/dist/core/version.d.ts +26 -0
- package/dist/core/version.d.ts.map +1 -0
- package/dist/core/version.js +50 -0
- package/dist/core/version.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +116 -0
- package/dist/index.js.map +1 -0
- package/dist/tools/bitable/app.d.ts +20 -0
- package/dist/tools/bitable/app.d.ts.map +1 -0
- package/dist/tools/bitable/app.js +301 -0
- package/dist/tools/bitable/app.js.map +1 -0
- package/dist/tools/bitable/field.d.ts +19 -0
- package/dist/tools/bitable/field.d.ts.map +1 -0
- package/dist/tools/bitable/field.js +315 -0
- package/dist/tools/bitable/field.js.map +1 -0
- package/dist/tools/bitable/index.d.ts +21 -0
- package/dist/tools/bitable/index.d.ts.map +1 -0
- package/dist/tools/bitable/index.js +39 -0
- package/dist/tools/bitable/index.js.map +1 -0
- package/dist/tools/bitable/record.d.ts +22 -0
- package/dist/tools/bitable/record.d.ts.map +1 -0
- package/dist/tools/bitable/record.js +434 -0
- package/dist/tools/bitable/record.js.map +1 -0
- package/dist/tools/bitable/table.d.ts +21 -0
- package/dist/tools/bitable/table.d.ts.map +1 -0
- package/dist/tools/bitable/table.js +361 -0
- package/dist/tools/bitable/table.js.map +1 -0
- package/dist/tools/calendar/calendar.d.ts +18 -0
- package/dist/tools/calendar/calendar.d.ts.map +1 -0
- package/dist/tools/calendar/calendar.js +192 -0
- package/dist/tools/calendar/calendar.js.map +1 -0
- package/dist/tools/calendar/event.d.ts +20 -0
- package/dist/tools/calendar/event.d.ts.map +1 -0
- package/dist/tools/calendar/event.js +465 -0
- package/dist/tools/calendar/event.js.map +1 -0
- package/dist/tools/calendar/index.d.ts +19 -0
- package/dist/tools/calendar/index.d.ts.map +1 -0
- package/dist/tools/calendar/index.js +37 -0
- package/dist/tools/calendar/index.js.map +1 -0
- package/dist/tools/chat/chat.d.ts +11 -0
- package/dist/tools/chat/chat.d.ts.map +1 -0
- package/dist/tools/chat/chat.js +106 -0
- package/dist/tools/chat/chat.js.map +1 -0
- package/dist/tools/chat/index.d.ts +11 -0
- package/dist/tools/chat/index.d.ts.map +1 -0
- package/dist/tools/chat/index.js +20 -0
- package/dist/tools/chat/index.js.map +1 -0
- package/dist/tools/chat/members.d.ts +9 -0
- package/dist/tools/chat/members.d.ts.map +1 -0
- package/dist/tools/chat/members.js +80 -0
- package/dist/tools/chat/members.js.map +1 -0
- package/dist/tools/common/get-user.d.ts +11 -0
- package/dist/tools/common/get-user.d.ts.map +1 -0
- package/dist/tools/common/get-user.js +112 -0
- package/dist/tools/common/get-user.js.map +1 -0
- package/dist/tools/common/index.d.ts +11 -0
- package/dist/tools/common/index.d.ts.map +1 -0
- package/dist/tools/common/index.js +20 -0
- package/dist/tools/common/index.js.map +1 -0
- package/dist/tools/common/search-user.d.ts +9 -0
- package/dist/tools/common/search-user.d.ts.map +1 -0
- package/dist/tools/common/search-user.js +88 -0
- package/dist/tools/common/search-user.js.map +1 -0
- package/dist/tools/doc/create.d.ts +17 -0
- package/dist/tools/doc/create.d.ts.map +1 -0
- package/dist/tools/doc/create.js +159 -0
- package/dist/tools/doc/create.js.map +1 -0
- package/dist/tools/doc/fetch.d.ts +17 -0
- package/dist/tools/doc/fetch.d.ts.map +1 -0
- package/dist/tools/doc/fetch.js +123 -0
- package/dist/tools/doc/fetch.js.map +1 -0
- package/dist/tools/doc/index.d.ts +21 -0
- package/dist/tools/doc/index.d.ts.map +1 -0
- package/dist/tools/doc/index.js +33 -0
- package/dist/tools/doc/index.js.map +1 -0
- package/dist/tools/doc/shared.d.ts +69 -0
- package/dist/tools/doc/shared.d.ts.map +1 -0
- package/dist/tools/doc/shared.js +172 -0
- package/dist/tools/doc/shared.js.map +1 -0
- package/dist/tools/doc/update.d.ts +25 -0
- package/dist/tools/doc/update.d.ts.map +1 -0
- package/dist/tools/doc/update.js +208 -0
- package/dist/tools/doc/update.js.map +1 -0
- package/dist/tools/drive/file.d.ts +13 -0
- package/dist/tools/drive/file.d.ts.map +1 -0
- package/dist/tools/drive/file.js +212 -0
- package/dist/tools/drive/file.js.map +1 -0
- package/dist/tools/drive/index.d.ts +12 -0
- package/dist/tools/drive/index.d.ts.map +1 -0
- package/dist/tools/drive/index.js +25 -0
- package/dist/tools/drive/index.js.map +1 -0
- package/dist/tools/im/format-messages.d.ts +99 -0
- package/dist/tools/im/format-messages.d.ts.map +1 -0
- package/dist/tools/im/format-messages.js +277 -0
- package/dist/tools/im/format-messages.js.map +1 -0
- package/dist/tools/im/helpers.d.ts +53 -0
- package/dist/tools/im/helpers.d.ts.map +1 -0
- package/dist/tools/im/helpers.js +85 -0
- package/dist/tools/im/helpers.js.map +1 -0
- package/dist/tools/im/index.d.ts +25 -0
- package/dist/tools/im/index.d.ts.map +1 -0
- package/dist/tools/im/index.js +44 -0
- package/dist/tools/im/index.js.map +1 -0
- package/dist/tools/im/message-read.d.ts +19 -0
- package/dist/tools/im/message-read.d.ts.map +1 -0
- package/dist/tools/im/message-read.js +526 -0
- package/dist/tools/im/message-read.js.map +1 -0
- package/dist/tools/im/message.d.ts +22 -0
- package/dist/tools/im/message.d.ts.map +1 -0
- package/dist/tools/im/message.js +233 -0
- package/dist/tools/im/message.js.map +1 -0
- package/dist/tools/im/resource.d.ts +19 -0
- package/dist/tools/im/resource.d.ts.map +1 -0
- package/dist/tools/im/resource.js +185 -0
- package/dist/tools/im/resource.js.map +1 -0
- package/dist/tools/im/time-utils.d.ts +70 -0
- package/dist/tools/im/time-utils.d.ts.map +1 -0
- package/dist/tools/im/time-utils.js +277 -0
- package/dist/tools/im/time-utils.js.map +1 -0
- package/dist/tools/index.d.ts +85 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +135 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/oauth.d.ts +15 -0
- package/dist/tools/oauth.d.ts.map +1 -0
- package/dist/tools/oauth.js +379 -0
- package/dist/tools/oauth.js.map +1 -0
- package/dist/tools/search/doc-search.d.ts +9 -0
- package/dist/tools/search/doc-search.d.ts.map +1 -0
- package/dist/tools/search/doc-search.js +219 -0
- package/dist/tools/search/doc-search.js.map +1 -0
- package/dist/tools/search/index.d.ts +11 -0
- package/dist/tools/search/index.d.ts.map +1 -0
- package/dist/tools/search/index.js +18 -0
- package/dist/tools/search/index.js.map +1 -0
- package/dist/tools/sheets/index.d.ts +11 -0
- package/dist/tools/sheets/index.d.ts.map +1 -0
- package/dist/tools/sheets/index.js +18 -0
- package/dist/tools/sheets/index.js.map +1 -0
- package/dist/tools/sheets/sheet.d.ts +11 -0
- package/dist/tools/sheets/sheet.d.ts.map +1 -0
- package/dist/tools/sheets/sheet.js +332 -0
- package/dist/tools/sheets/sheet.js.map +1 -0
- package/dist/tools/task/index.d.ts +12 -0
- package/dist/tools/task/index.d.ts.map +1 -0
- package/dist/tools/task/index.js +30 -0
- package/dist/tools/task/index.js.map +1 -0
- package/dist/tools/task/task.d.ts +13 -0
- package/dist/tools/task/task.d.ts.map +1 -0
- package/dist/tools/task/task.js +225 -0
- package/dist/tools/task/task.js.map +1 -0
- package/dist/tools/task/tasklist.d.ts +13 -0
- package/dist/tools/task/tasklist.d.ts.map +1 -0
- package/dist/tools/task/tasklist.js +206 -0
- package/dist/tools/task/tasklist.js.map +1 -0
- package/dist/tools/wiki/index.d.ts +11 -0
- package/dist/tools/wiki/index.d.ts.map +1 -0
- package/dist/tools/wiki/index.js +20 -0
- package/dist/tools/wiki/index.js.map +1 -0
- package/dist/tools/wiki/node.d.ts +11 -0
- package/dist/tools/wiki/node.d.ts.map +1 -0
- package/dist/tools/wiki/node.js +112 -0
- package/dist/tools/wiki/node.js.map +1 -0
- package/dist/tools/wiki/space.d.ts +11 -0
- package/dist/tools/wiki/space.d.ts.map +1 -0
- package/dist/tools/wiki/space.js +125 -0
- package/dist/tools/wiki/space.js.map +1 -0
- package/dist/utils/index.d.ts +8 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +8 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/logger.d.ts +36 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +101 -0
- package/dist/utils/logger.js.map +1 -0
- package/eslint.config.js +13 -0
- package/package.json +54 -0
- package/skills/feishu-bitable/SKILL.md +248 -0
- package/skills/feishu-bitable/references/examples.md +813 -0
- package/skills/feishu-bitable/references/field-properties.md +763 -0
- package/skills/feishu-bitable/references/record-values.md +911 -0
- package/skills/feishu-calendar/SKILL.md +244 -0
- package/skills/feishu-channel-rules/SKILL.md +18 -0
- package/skills/feishu-channel-rules/references/markdown-syntax.md +138 -0
- package/skills/feishu-create-doc/SKILL.md +719 -0
- package/skills/feishu-fetch-doc/SKILL.md +93 -0
- package/skills/feishu-im-read/SKILL.md +163 -0
- package/skills/feishu-task/SKILL.md +293 -0
- package/skills/feishu-troubleshoot/SKILL.md +70 -0
- package/skills/feishu-update-doc/SKILL.md +285 -0
- package/src/core/api-error.ts +342 -0
- package/src/core/auth-errors.ts +27 -0
- package/src/core/config.ts +134 -0
- package/src/core/device-flow.ts +314 -0
- package/src/core/index.ts +16 -0
- package/src/core/lark-client.ts +391 -0
- package/src/core/token-store.ts +249 -0
- package/src/core/types.ts +302 -0
- package/src/core/uat-client.ts +298 -0
- package/src/core/version.ts +53 -0
- package/src/index.ts +138 -0
- package/src/tools/bitable/app.ts +390 -0
- package/src/tools/bitable/field.ts +406 -0
- package/src/tools/bitable/index.ts +43 -0
- package/src/tools/bitable/record.ts +559 -0
- package/src/tools/bitable/table.ts +472 -0
- package/src/tools/calendar/calendar.ts +254 -0
- package/src/tools/calendar/event.ts +606 -0
- package/src/tools/calendar/index.ts +41 -0
- package/src/tools/chat/chat.ts +127 -0
- package/src/tools/chat/index.ts +24 -0
- package/src/tools/chat/members.ts +93 -0
- package/src/tools/common/get-user.ts +127 -0
- package/src/tools/common/index.ts +24 -0
- package/src/tools/common/search-user.ts +99 -0
- package/src/tools/doc/create.ts +184 -0
- package/src/tools/doc/fetch.ts +149 -0
- package/src/tools/doc/index.ts +38 -0
- package/src/tools/doc/shared.ts +228 -0
- package/src/tools/doc/update.ts +240 -0
- package/src/tools/drive/file.ts +265 -0
- package/src/tools/drive/index.ts +29 -0
- package/src/tools/im/format-messages.ts +391 -0
- package/src/tools/im/helpers.ts +109 -0
- package/src/tools/im/index.ts +49 -0
- package/src/tools/im/message-read.ts +676 -0
- package/src/tools/im/message.ts +303 -0
- package/src/tools/im/resource.ts +225 -0
- package/src/tools/im/time-utils.ts +347 -0
- package/src/tools/index.ts +205 -0
- package/src/tools/oauth.ts +460 -0
- package/src/tools/search/doc-search.ts +250 -0
- package/src/tools/search/index.ts +22 -0
- package/src/tools/sheets/index.ts +22 -0
- package/src/tools/sheets/sheet.ts +382 -0
- package/src/tools/task/index.ts +34 -0
- package/src/tools/task/task.ts +265 -0
- package/src/tools/task/tasklist.ts +262 -0
- package/src/tools/wiki/index.ts +24 -0
- package/src/tools/wiki/node.ts +131 -0
- package/src/tools/wiki/space.ts +152 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/logger.ts +132 -0
- package/tests/core/config.test.ts +238 -0
- package/tests/core/device-flow.test.ts +490 -0
- package/tests/core/lark-client.test.ts +378 -0
- package/tests/core/token-store.test.ts +438 -0
- package/tests/index.test.ts +360 -0
- package/tests/tools/doc/create.test.ts +224 -0
- package/tests/tools/doc/fetch.test.ts +182 -0
- package/tests/tools/doc/shared.test.ts +183 -0
- package/tests/tools/doc/update.test.ts +330 -0
- package/tests/tools/im/format-messages.test.ts +184 -0
- package/tests/tools/im/time-utils.test.ts +178 -0
- package/tests/utils/logger.test.ts +140 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2026 ByteDance Ltd. and/or its affiliates
|
|
3
|
+
* SPDX-License-Identifier: MIT
|
|
4
|
+
*
|
|
5
|
+
* Tests for token-store.ts
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
9
|
+
import {
|
|
10
|
+
getStoredToken,
|
|
11
|
+
setStoredToken,
|
|
12
|
+
removeStoredToken,
|
|
13
|
+
listStoredTokens,
|
|
14
|
+
clearAllTokens,
|
|
15
|
+
tokenStatus,
|
|
16
|
+
maskToken,
|
|
17
|
+
getTokensFilePath,
|
|
18
|
+
tokensStorageExists,
|
|
19
|
+
type StoredUAToken,
|
|
20
|
+
} from '../../src/core/token-store.js';
|
|
21
|
+
import { mkdir, readFile, writeFile, unlink, stat, chmod } from 'node:fs/promises';
|
|
22
|
+
import { join } from 'node:path';
|
|
23
|
+
import { homedir } from 'node:os';
|
|
24
|
+
|
|
25
|
+
// Mock fs/promises
|
|
26
|
+
vi.mock('node:fs/promises', () => ({
|
|
27
|
+
mkdir: vi.fn(),
|
|
28
|
+
readFile: vi.fn(),
|
|
29
|
+
writeFile: vi.fn(),
|
|
30
|
+
unlink: vi.fn(),
|
|
31
|
+
chmod: vi.fn(),
|
|
32
|
+
stat: vi.fn(),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
// Mock logger
|
|
36
|
+
vi.mock('../../src/utils/logger.js', () => ({
|
|
37
|
+
logger: () => ({
|
|
38
|
+
info: vi.fn(),
|
|
39
|
+
debug: vi.fn(),
|
|
40
|
+
warn: vi.fn(),
|
|
41
|
+
error: vi.fn(),
|
|
42
|
+
}),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
describe('maskToken', () => {
|
|
46
|
+
it('should mask short tokens', () => {
|
|
47
|
+
expect(maskToken('abc')).toBe('****');
|
|
48
|
+
expect(maskToken('12345678')).toBe('****');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should show last 4 chars for longer tokens', () => {
|
|
52
|
+
expect(maskToken('123456789')).toBe('****6789');
|
|
53
|
+
expect(maskToken('abcdefghijklmnop')).toBe('****mnop');
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('tokenStatus', () => {
|
|
58
|
+
const now = Date.now();
|
|
59
|
+
|
|
60
|
+
it('should return "valid" for token not expiring soon', () => {
|
|
61
|
+
const token: StoredUAToken = {
|
|
62
|
+
userOpenId: 'user-1',
|
|
63
|
+
appId: 'app-1',
|
|
64
|
+
accessToken: 'at',
|
|
65
|
+
refreshToken: 'rt',
|
|
66
|
+
expiresAt: now + 60 * 60 * 1000, // 1 hour from now
|
|
67
|
+
refreshExpiresAt: now + 7 * 24 * 60 * 60 * 1000, // 7 days from now
|
|
68
|
+
scope: 'test',
|
|
69
|
+
grantedAt: now,
|
|
70
|
+
};
|
|
71
|
+
expect(tokenStatus(token)).toBe('valid');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should return "needs_refresh" for token expiring within 5 minutes', () => {
|
|
75
|
+
const token: StoredUAToken = {
|
|
76
|
+
userOpenId: 'user-1',
|
|
77
|
+
appId: 'app-1',
|
|
78
|
+
accessToken: 'at',
|
|
79
|
+
refreshToken: 'rt',
|
|
80
|
+
expiresAt: now + 4 * 60 * 1000, // 4 minutes from now (within 5 min refresh window)
|
|
81
|
+
refreshExpiresAt: now + 7 * 24 * 60 * 60 * 1000, // 7 days from now
|
|
82
|
+
scope: 'test',
|
|
83
|
+
grantedAt: now,
|
|
84
|
+
};
|
|
85
|
+
expect(tokenStatus(token)).toBe('needs_refresh');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should return "needs_refresh" for expired access token but valid refresh token', () => {
|
|
89
|
+
const token: StoredUAToken = {
|
|
90
|
+
userOpenId: 'user-1',
|
|
91
|
+
appId: 'app-1',
|
|
92
|
+
accessToken: 'at',
|
|
93
|
+
refreshToken: 'rt',
|
|
94
|
+
expiresAt: now - 1000, // Already expired
|
|
95
|
+
refreshExpiresAt: now + 7 * 24 * 60 * 60 * 1000, // 7 days from now
|
|
96
|
+
scope: 'test',
|
|
97
|
+
grantedAt: now,
|
|
98
|
+
};
|
|
99
|
+
expect(tokenStatus(token)).toBe('needs_refresh');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should return "expired" when both tokens are expired', () => {
|
|
103
|
+
const token: StoredUAToken = {
|
|
104
|
+
userOpenId: 'user-1',
|
|
105
|
+
appId: 'app-1',
|
|
106
|
+
accessToken: 'at',
|
|
107
|
+
refreshToken: 'rt',
|
|
108
|
+
expiresAt: now - 1000, // Already expired
|
|
109
|
+
refreshExpiresAt: now - 1000, // Already expired
|
|
110
|
+
scope: 'test',
|
|
111
|
+
grantedAt: now,
|
|
112
|
+
};
|
|
113
|
+
expect(tokenStatus(token)).toBe('expired');
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
describe('getTokensFilePath', () => {
|
|
118
|
+
it('should return path in user home directory', () => {
|
|
119
|
+
const path = getTokensFilePath();
|
|
120
|
+
expect(path).toBe(join(homedir(), '.cc-lark', 'tokens.json'));
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
describe('tokensStorageExists', () => {
|
|
125
|
+
beforeEach(() => {
|
|
126
|
+
vi.mocked(stat).mockReset();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should return true when file exists', async () => {
|
|
130
|
+
vi.mocked(stat).mockResolvedValueOnce({} as any);
|
|
131
|
+
const exists = await tokensStorageExists();
|
|
132
|
+
expect(exists).toBe(true);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should return false when file does not exist', async () => {
|
|
136
|
+
const error = new Error('ENOENT') as NodeJS.ErrnoException;
|
|
137
|
+
error.code = 'ENOENT';
|
|
138
|
+
vi.mocked(stat).mockRejectedValueOnce(error);
|
|
139
|
+
const exists = await tokensStorageExists();
|
|
140
|
+
expect(exists).toBe(false);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('getStoredToken', () => {
|
|
145
|
+
beforeEach(() => {
|
|
146
|
+
vi.mocked(readFile).mockReset();
|
|
147
|
+
vi.mocked(mkdir).mockReset();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('should return null when storage file does not exist', async () => {
|
|
151
|
+
const error = new Error('ENOENT') as NodeJS.ErrnoException;
|
|
152
|
+
error.code = 'ENOENT';
|
|
153
|
+
vi.mocked(readFile).mockRejectedValueOnce(error);
|
|
154
|
+
|
|
155
|
+
const token = await getStoredToken('app-1', 'user-1');
|
|
156
|
+
expect(token).toBeNull();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('should return stored token when exists', async () => {
|
|
160
|
+
const now = Date.now();
|
|
161
|
+
const storedToken: StoredUAToken = {
|
|
162
|
+
userOpenId: 'user-1',
|
|
163
|
+
appId: 'app-1',
|
|
164
|
+
accessToken: 'test-access-token',
|
|
165
|
+
refreshToken: 'test-refresh-token',
|
|
166
|
+
expiresAt: now + 3600000,
|
|
167
|
+
refreshExpiresAt: now + 604800000,
|
|
168
|
+
scope: 'test-scope',
|
|
169
|
+
grantedAt: now,
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
const storage = {
|
|
173
|
+
version: 1,
|
|
174
|
+
tokens: {
|
|
175
|
+
'app-1:user-1': storedToken,
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
180
|
+
|
|
181
|
+
const token = await getStoredToken('app-1', 'user-1');
|
|
182
|
+
expect(token).toEqual(storedToken);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should return null when token not found', async () => {
|
|
186
|
+
const storage = {
|
|
187
|
+
version: 1,
|
|
188
|
+
tokens: {},
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
192
|
+
|
|
193
|
+
const token = await getStoredToken('app-1', 'user-1');
|
|
194
|
+
expect(token).toBeNull();
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should return null for unsupported version', async () => {
|
|
198
|
+
const storage = {
|
|
199
|
+
version: 2,
|
|
200
|
+
tokens: {},
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
204
|
+
|
|
205
|
+
const token = await getStoredToken('app-1', 'user-1');
|
|
206
|
+
expect(token).toBeNull();
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
describe('setStoredToken', () => {
|
|
211
|
+
beforeEach(() => {
|
|
212
|
+
vi.mocked(readFile).mockReset();
|
|
213
|
+
vi.mocked(writeFile).mockReset();
|
|
214
|
+
vi.mocked(mkdir).mockReset();
|
|
215
|
+
vi.mocked(chmod).mockReset();
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should create new storage file if not exists', async () => {
|
|
219
|
+
const error = new Error('ENOENT') as NodeJS.ErrnoException;
|
|
220
|
+
error.code = 'ENOENT';
|
|
221
|
+
vi.mocked(readFile).mockRejectedValueOnce(error);
|
|
222
|
+
vi.mocked(mkdir).mockResolvedValueOnce(undefined);
|
|
223
|
+
vi.mocked(writeFile).mockResolvedValueOnce(undefined);
|
|
224
|
+
vi.mocked(chmod).mockResolvedValueOnce(undefined);
|
|
225
|
+
|
|
226
|
+
const now = Date.now();
|
|
227
|
+
const token: StoredUAToken = {
|
|
228
|
+
userOpenId: 'user-1',
|
|
229
|
+
appId: 'app-1',
|
|
230
|
+
accessToken: 'test-access-token',
|
|
231
|
+
refreshToken: 'test-refresh-token',
|
|
232
|
+
expiresAt: now + 3600000,
|
|
233
|
+
refreshExpiresAt: now + 604800000,
|
|
234
|
+
scope: 'test-scope',
|
|
235
|
+
grantedAt: now,
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
await setStoredToken(token);
|
|
239
|
+
|
|
240
|
+
expect(mkdir).toHaveBeenCalled();
|
|
241
|
+
expect(writeFile).toHaveBeenCalled();
|
|
242
|
+
const writtenData = vi.mocked(writeFile).mock.calls[0][1] as string;
|
|
243
|
+
const parsed = JSON.parse(writtenData);
|
|
244
|
+
expect(parsed.version).toBe(1);
|
|
245
|
+
expect(parsed.tokens['app-1:user-1']).toEqual(token);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should update existing storage', async () => {
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
const existingToken: StoredUAToken = {
|
|
251
|
+
userOpenId: 'user-1',
|
|
252
|
+
appId: 'app-1',
|
|
253
|
+
accessToken: 'old-access-token',
|
|
254
|
+
refreshToken: 'old-refresh-token',
|
|
255
|
+
expiresAt: now,
|
|
256
|
+
refreshExpiresAt: now,
|
|
257
|
+
scope: 'old-scope',
|
|
258
|
+
grantedAt: now,
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const storage = {
|
|
262
|
+
version: 1,
|
|
263
|
+
tokens: {
|
|
264
|
+
'app-1:user-1': existingToken,
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
269
|
+
vi.mocked(mkdir).mockResolvedValueOnce(undefined);
|
|
270
|
+
vi.mocked(writeFile).mockResolvedValueOnce(undefined);
|
|
271
|
+
vi.mocked(chmod).mockResolvedValueOnce(undefined);
|
|
272
|
+
|
|
273
|
+
const newToken: StoredUAToken = {
|
|
274
|
+
userOpenId: 'user-1',
|
|
275
|
+
appId: 'app-1',
|
|
276
|
+
accessToken: 'new-access-token',
|
|
277
|
+
refreshToken: 'new-refresh-token',
|
|
278
|
+
expiresAt: now + 3600000,
|
|
279
|
+
refreshExpiresAt: now + 604800000,
|
|
280
|
+
scope: 'new-scope',
|
|
281
|
+
grantedAt: now,
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
await setStoredToken(newToken);
|
|
285
|
+
|
|
286
|
+
const writtenData = vi.mocked(writeFile).mock.calls[0][1] as string;
|
|
287
|
+
const parsed = JSON.parse(writtenData);
|
|
288
|
+
expect(parsed.tokens['app-1:user-1']).toEqual(newToken);
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
describe('removeStoredToken', () => {
|
|
293
|
+
beforeEach(() => {
|
|
294
|
+
vi.mocked(readFile).mockReset();
|
|
295
|
+
vi.mocked(writeFile).mockReset();
|
|
296
|
+
vi.mocked(unlink).mockReset();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('should remove existing token', async () => {
|
|
300
|
+
const now = Date.now();
|
|
301
|
+
const token: StoredUAToken = {
|
|
302
|
+
userOpenId: 'user-1',
|
|
303
|
+
appId: 'app-1',
|
|
304
|
+
accessToken: 'test-access-token',
|
|
305
|
+
refreshToken: 'test-refresh-token',
|
|
306
|
+
expiresAt: now + 3600000,
|
|
307
|
+
refreshExpiresAt: now + 604800000,
|
|
308
|
+
scope: 'test-scope',
|
|
309
|
+
grantedAt: now,
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const storage = {
|
|
313
|
+
version: 1,
|
|
314
|
+
tokens: {
|
|
315
|
+
'app-1:user-1': token,
|
|
316
|
+
},
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
320
|
+
vi.mocked(writeFile).mockResolvedValueOnce(undefined);
|
|
321
|
+
|
|
322
|
+
await removeStoredToken('app-1', 'user-1');
|
|
323
|
+
|
|
324
|
+
const writtenData = vi.mocked(writeFile).mock.calls[0][1] as string;
|
|
325
|
+
const parsed = JSON.parse(writtenData);
|
|
326
|
+
expect(parsed.tokens['app-1:user-1']).toBeUndefined();
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it('should do nothing when storage does not exist', async () => {
|
|
330
|
+
const error = new Error('ENOENT') as NodeJS.ErrnoException;
|
|
331
|
+
error.code = 'ENOENT';
|
|
332
|
+
vi.mocked(readFile).mockRejectedValueOnce(error);
|
|
333
|
+
|
|
334
|
+
await removeStoredToken('app-1', 'user-1');
|
|
335
|
+
// Should not throw
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it('should do nothing when token does not exist', async () => {
|
|
339
|
+
const storage = {
|
|
340
|
+
version: 1,
|
|
341
|
+
tokens: {},
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
345
|
+
|
|
346
|
+
await removeStoredToken('app-1', 'user-1');
|
|
347
|
+
// Should not write since no token was removed
|
|
348
|
+
expect(writeFile).not.toHaveBeenCalled();
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe('listStoredTokens', () => {
|
|
353
|
+
beforeEach(() => {
|
|
354
|
+
vi.mocked(readFile).mockReset();
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it('should list all tokens for given appId', async () => {
|
|
358
|
+
const now = Date.now();
|
|
359
|
+
const token1: StoredUAToken = {
|
|
360
|
+
userOpenId: 'user-1',
|
|
361
|
+
appId: 'app-1',
|
|
362
|
+
accessToken: 'at1',
|
|
363
|
+
refreshToken: 'rt1',
|
|
364
|
+
expiresAt: now + 3600000,
|
|
365
|
+
refreshExpiresAt: now + 604800000,
|
|
366
|
+
scope: 'scope1',
|
|
367
|
+
grantedAt: now,
|
|
368
|
+
};
|
|
369
|
+
const token2: StoredUAToken = {
|
|
370
|
+
userOpenId: 'user-2',
|
|
371
|
+
appId: 'app-1',
|
|
372
|
+
accessToken: 'at2',
|
|
373
|
+
refreshToken: 'rt2',
|
|
374
|
+
expiresAt: now + 3600000,
|
|
375
|
+
refreshExpiresAt: now + 604800000,
|
|
376
|
+
scope: 'scope2',
|
|
377
|
+
grantedAt: now,
|
|
378
|
+
};
|
|
379
|
+
const token3: StoredUAToken = {
|
|
380
|
+
userOpenId: 'user-3',
|
|
381
|
+
appId: 'app-2', // Different app
|
|
382
|
+
accessToken: 'at3',
|
|
383
|
+
refreshToken: 'rt3',
|
|
384
|
+
expiresAt: now + 3600000,
|
|
385
|
+
refreshExpiresAt: now + 604800000,
|
|
386
|
+
scope: 'scope3',
|
|
387
|
+
grantedAt: now,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const storage = {
|
|
391
|
+
version: 1,
|
|
392
|
+
tokens: {
|
|
393
|
+
'app-1:user-1': token1,
|
|
394
|
+
'app-1:user-2': token2,
|
|
395
|
+
'app-2:user-3': token3,
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
vi.mocked(readFile).mockResolvedValueOnce(JSON.stringify(storage));
|
|
400
|
+
|
|
401
|
+
const tokens = await listStoredTokens('app-1');
|
|
402
|
+
expect(tokens).toHaveLength(2);
|
|
403
|
+
expect(tokens).toContainEqual(token1);
|
|
404
|
+
expect(tokens).toContainEqual(token2);
|
|
405
|
+
expect(tokens).not.toContainEqual(token3);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it('should return empty array when no tokens', async () => {
|
|
409
|
+
const error = new Error('ENOENT') as NodeJS.ErrnoException;
|
|
410
|
+
error.code = 'ENOENT';
|
|
411
|
+
vi.mocked(readFile).mockRejectedValueOnce(error);
|
|
412
|
+
|
|
413
|
+
const tokens = await listStoredTokens('app-1');
|
|
414
|
+
expect(tokens).toEqual([]);
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
describe('clearAllTokens', () => {
|
|
419
|
+
beforeEach(() => {
|
|
420
|
+
vi.mocked(unlink).mockReset();
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it('should delete tokens file', async () => {
|
|
424
|
+
vi.mocked(unlink).mockResolvedValueOnce(undefined);
|
|
425
|
+
|
|
426
|
+
await clearAllTokens();
|
|
427
|
+
|
|
428
|
+
expect(unlink).toHaveBeenCalled();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('should not throw when file does not exist', async () => {
|
|
432
|
+
const error = new Error('ENOENT') as NodeJS.ErrnoException;
|
|
433
|
+
error.code = 'ENOENT';
|
|
434
|
+
vi.mocked(unlink).mockRejectedValueOnce(error);
|
|
435
|
+
|
|
436
|
+
await clearAllTokens(); // Should not throw
|
|
437
|
+
});
|
|
438
|
+
});
|