@lark-apaas/client-toolkit 1.2.81 → 1.2.82-alpha.20261104054541

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 @@
1
+ export {};
@@ -0,0 +1,106 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { getAppInfo } from "../getAppInfo.js";
3
+ import { isSparkRuntime } from "../../utils/utils.js";
4
+ const { getInitialInfoMock } = vi.hoisted(()=>({
5
+ getInitialInfoMock: vi.fn()
6
+ }));
7
+ vi.mock('../../utils/getInitialInfo', ()=>({
8
+ getInitialInfo: getInitialInfoMock
9
+ }));
10
+ vi.mock('../../utils/utils', async (importOriginal)=>{
11
+ const actual = await importOriginal();
12
+ return {
13
+ ...actual,
14
+ isSparkRuntime: vi.fn()
15
+ };
16
+ });
17
+ beforeEach(()=>{
18
+ getInitialInfoMock.mockReset();
19
+ vi.mocked(isSparkRuntime).mockReset();
20
+ delete window._appInfo;
21
+ });
22
+ afterEach(()=>{
23
+ delete window._appInfo;
24
+ });
25
+ describe('getAppInfo', ()=>{
26
+ it('returns the cached value on window without fetching', async ()=>{
27
+ window._appInfo = {
28
+ name: 'cached',
29
+ avatar: 'a',
30
+ description: 'd'
31
+ };
32
+ const result = await getAppInfo();
33
+ expect(result.name).toBe('cached');
34
+ expect(getInitialInfoMock).not.toHaveBeenCalled();
35
+ });
36
+ it('returns {} when not in spark runtime and no cache', async ()=>{
37
+ vi.mocked(isSparkRuntime).mockReturnValue(false);
38
+ expect(await getAppInfo()).toEqual({});
39
+ expect(getInitialInfoMock).not.toHaveBeenCalled();
40
+ });
41
+ it('fetches initial info when in spark runtime and writes back to window', async ()=>{
42
+ vi.mocked(isSparkRuntime).mockReturnValue(true);
43
+ getInitialInfoMock.mockResolvedValue({
44
+ app_info: {
45
+ app_name: ' My App ',
46
+ app_avatar: 'http://a',
47
+ app_description: 'd'
48
+ }
49
+ });
50
+ const result = await getAppInfo();
51
+ expect(result.name).toBe('My App');
52
+ expect(result.avatar).toBe('http://a');
53
+ expect(result.description).toBe('d');
54
+ expect(window._appInfo).toEqual(result);
55
+ });
56
+ it('refreshes when refresh=true even with cached value', async ()=>{
57
+ window._appInfo = {
58
+ name: 'old'
59
+ };
60
+ vi.mocked(isSparkRuntime).mockReturnValue(true);
61
+ getInitialInfoMock.mockResolvedValue({
62
+ app_info: {
63
+ app_name: 'new',
64
+ app_avatar: '',
65
+ app_description: ''
66
+ }
67
+ });
68
+ const result = await getAppInfo(true);
69
+ expect(result.name).toBe('new');
70
+ expect(getInitialInfoMock).toHaveBeenCalledWith(true);
71
+ });
72
+ it('handles undefined app_info gracefully', async ()=>{
73
+ vi.mocked(isSparkRuntime).mockReturnValue(true);
74
+ getInitialInfoMock.mockResolvedValue(void 0);
75
+ const result = await getAppInfo();
76
+ expect(result).toEqual({
77
+ name: '',
78
+ avatar: '',
79
+ description: ''
80
+ });
81
+ });
82
+ it('extracts url when app_avatar is a JSON string', async ()=>{
83
+ vi.mocked(isSparkRuntime).mockReturnValue(true);
84
+ getInitialInfoMock.mockResolvedValue({
85
+ app_info: {
86
+ app_name: 'App',
87
+ app_avatar: '{"url":"https://a/logo.png"}',
88
+ app_description: ''
89
+ }
90
+ });
91
+ const result = await getAppInfo();
92
+ expect(result.avatar).toBe('https://a/logo.png');
93
+ });
94
+ it('returns empty avatar when app_avatar is malformed JSON', async ()=>{
95
+ vi.mocked(isSparkRuntime).mockReturnValue(true);
96
+ getInitialInfoMock.mockResolvedValue({
97
+ app_info: {
98
+ app_name: 'App',
99
+ app_avatar: '{"url":',
100
+ app_description: ''
101
+ }
102
+ });
103
+ const result = await getAppInfo();
104
+ expect(result.avatar).toBe('');
105
+ });
106
+ });
@@ -6,7 +6,7 @@ async function getAppInfo(refresh = false) {
6
6
  const info = (await getInitialInfo(refresh))?.app_info;
7
7
  appInfo = {
8
8
  name: info?.app_name || '',
9
- avatar: info?.app_avatar || '',
9
+ avatar: normalizeAvatar(info?.app_avatar),
10
10
  description: info?.app_description || ''
11
11
  };
12
12
  if (appInfo.name) appInfo.name = appInfo.name.trim();
@@ -14,4 +14,14 @@ async function getAppInfo(refresh = false) {
14
14
  }
15
15
  return appInfo ?? {};
16
16
  }
17
+ function normalizeAvatar(raw) {
18
+ if (!raw) return '';
19
+ if (raw.trim().startsWith('{')) try {
20
+ const url = JSON.parse(raw)?.url;
21
+ return 'string' == typeof url ? url : '';
22
+ } catch {
23
+ return '';
24
+ }
25
+ return raw;
26
+ }
17
27
  export { getAppInfo };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.81",
3
+ "version": "1.2.82-alpha.20261104054541",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [