@panomc/sdk 1.0.0-dev.9 → 2.0.0-dev.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.
Files changed (166) hide show
  1. package/api/routes.core.json +3702 -0
  2. package/bin/api/__tests__/api-compat.test.mjs +97 -0
  3. package/bin/api/__tests__/check-paths.test.mjs +113 -0
  4. package/bin/api/__tests__/cli.test.mjs +44 -0
  5. package/bin/api/__tests__/extract-routes.test.mjs +132 -0
  6. package/bin/api/__tests__/fixtures/core-new/Pano/api/paths.old-new.json +82 -0
  7. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
  8. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
  9. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
  10. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
  11. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
  12. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
  13. package/bin/api/__tests__/fixtures/core-new/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
  14. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/error/NotExists.kt +10 -0
  15. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/model/Bases.kt +21 -0
  16. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/GetSiteInfoAPI.kt +19 -0
  17. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/auth/HostSsoAPI.kt +10 -0
  18. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt +28 -0
  19. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/api/plugins/GetPluginUiZipAPI.kt +9 -0
  20. package/bin/api/__tests__/fixtures/core-old/Pano/src/main/kotlin/com/panomc/platform/route/template/IndexTemplate.kt +9 -0
  21. package/bin/api/__tests__/fixtures/plugin-new/api/paths.old-new.json +66 -0
  22. package/bin/api/__tests__/fixtures/plugin-new/build.gradle.kts +42 -0
  23. package/bin/api/__tests__/fixtures/plugin-new/gradle.properties +4 -0
  24. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
  25. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
  26. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
  27. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
  28. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
  29. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
  30. package/bin/api/__tests__/fixtures/plugin-new/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
  31. package/bin/api/__tests__/fixtures/plugin-new/src/panel/Things.js +4 -0
  32. package/bin/api/__tests__/fixtures/plugin-new/src/theme/Logo.svelte +9 -0
  33. package/bin/api/__tests__/fixtures/plugin-new/src/theme/api.js +29 -0
  34. package/bin/api/__tests__/fixtures/plugin-old/build.gradle.kts +22 -0
  35. package/bin/api/__tests__/fixtures/plugin-old/gradle.properties +4 -0
  36. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/base/DemoApis.kt +13 -0
  37. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Errors.kt +16 -0
  38. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/error/Plain.kt +4 -0
  39. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/GetThingsAPI.kt +16 -0
  40. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/PanelThingAPI.kt +11 -0
  41. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt +18 -0
  42. package/bin/api/__tests__/fixtures/plugin-old/src/main/kotlin/com/example/demo/routes/WebhookAPI.kt +23 -0
  43. package/bin/api/__tests__/fixtures/plugin-old/src/panel/Things.js +4 -0
  44. package/bin/api/__tests__/fixtures/plugin-old/src/theme/Logo.svelte +9 -0
  45. package/bin/api/__tests__/fixtures/plugin-old/src/theme/api.js +29 -0
  46. package/bin/api/__tests__/fixtures/refusals/gradle.properties +1 -0
  47. package/bin/api/__tests__/fixtures/refusals/src/main/kotlin/x/Bad.kt +39 -0
  48. package/bin/api/__tests__/fixtures/unresolved/src/main/kotlin/x/Orphan.kt +19 -0
  49. package/bin/api/__tests__/helpers.mjs +48 -0
  50. package/bin/api/__tests__/migrate-v1.test.mjs +377 -0
  51. package/bin/api/__tests__/openapi-snapshot.test.mjs +120 -0
  52. package/bin/api/api-compat.mjs +204 -0
  53. package/bin/api/check-paths.mjs +135 -0
  54. package/bin/api/cli.mjs +83 -0
  55. package/bin/api/extract-routes.mjs +129 -0
  56. package/bin/api/js-scan.mjs +337 -0
  57. package/bin/api/kotlin.mjs +366 -0
  58. package/bin/api/migrate-v1.mjs +330 -0
  59. package/bin/api/openapi-snapshot.mjs +112 -0
  60. package/bin/api/project.mjs +172 -0
  61. package/bin/api/rename-table.mjs +66 -0
  62. package/bin/api/util.mjs +194 -0
  63. package/bin/pano-api.js +5 -0
  64. package/build/plugin-api-rollup.js +46 -0
  65. package/core/css/pano-tokens.css +225 -0
  66. package/core/favicon/apple-touch-icon.png +0 -0
  67. package/core/favicon/favicon-96x96.png +0 -0
  68. package/core/favicon/favicon.ico +0 -0
  69. package/core/favicon/favicon.svg +1 -0
  70. package/core/favicon/site.webmanifest +21 -0
  71. package/core/favicon/web-app-manifest-192x192.png +0 -0
  72. package/core/favicon/web-app-manifest-512x512.png +0 -0
  73. package/core/favicon_old/android-icon-144x144.png +0 -0
  74. package/core/favicon_old/android-icon-192x192.png +0 -0
  75. package/core/favicon_old/android-icon-36x36.png +0 -0
  76. package/core/favicon_old/android-icon-48x48.png +0 -0
  77. package/core/favicon_old/android-icon-72x72.png +0 -0
  78. package/core/favicon_old/android-icon-96x96.png +0 -0
  79. package/core/favicon_old/apple-icon-114x114.png +0 -0
  80. package/core/favicon_old/apple-icon-120x120.png +0 -0
  81. package/core/favicon_old/apple-icon-144x144.png +0 -0
  82. package/core/favicon_old/apple-icon-152x152.png +0 -0
  83. package/core/favicon_old/apple-icon-180x180.png +0 -0
  84. package/core/favicon_old/apple-icon-57x57.png +0 -0
  85. package/core/favicon_old/apple-icon-60x60.png +0 -0
  86. package/core/favicon_old/apple-icon-72x72.png +0 -0
  87. package/core/favicon_old/apple-icon-76x76.png +0 -0
  88. package/core/favicon_old/apple-icon-precomposed.png +0 -0
  89. package/core/favicon_old/apple-icon.png +0 -0
  90. package/core/favicon_old/browserconfig.xml +11 -0
  91. package/core/favicon_old/favicon-16x16.png +0 -0
  92. package/core/favicon_old/favicon-32x32.png +0 -0
  93. package/core/favicon_old/favicon-96x96.png +0 -0
  94. package/core/favicon_old/favicon.ico +0 -0
  95. package/core/favicon_old/manifest.json +41 -0
  96. package/core/favicon_old/ms-icon-144x144.png +0 -0
  97. package/core/favicon_old/ms-icon-150x150.png +0 -0
  98. package/core/favicon_old/ms-icon-310x310.png +0 -0
  99. package/core/favicon_old/ms-icon-70x70.png +0 -0
  100. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.eot +0 -0
  101. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.svg +483 -0
  102. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.ttf +0 -0
  103. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff +0 -0
  104. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-300.woff2 +0 -0
  105. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.eot +0 -0
  106. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.svg +486 -0
  107. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.ttf +0 -0
  108. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff +0 -0
  109. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-500.woff2 +0 -0
  110. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.eot +0 -0
  111. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.svg +484 -0
  112. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.ttf +0 -0
  113. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff +0 -0
  114. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-700.woff2 +0 -0
  115. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.eot +0 -0
  116. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.svg +486 -0
  117. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.ttf +0 -0
  118. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff +0 -0
  119. package/core/fonts/quicksand-v8-latin-ext_latin_vietnamese-regular.woff2 +0 -0
  120. package/core/js/ControllerRegistry.js +421 -0
  121. package/core/js/NotificationManager.js +62 -0
  122. package/core/js/PluginAPI.js +39 -0
  123. package/core/js/PluginManager.js +851 -0
  124. package/core/js/RouteMatcher.js +142 -0
  125. package/core/js/__tests__/ControllerRegistry.test.js +378 -0
  126. package/core/js/__tests__/NotificationManager.test.js +57 -0
  127. package/core/js/__tests__/PluginAPI.test.js +25 -0
  128. package/core/js/__tests__/RouteMatcher.test.js +33 -0
  129. package/core/js/__tests__/accessClient.test.js +582 -0
  130. package/core/js/__tests__/api.util.v1.test.js +336 -0
  131. package/core/js/__tests__/appStores.test.js +47 -0
  132. package/core/js/__tests__/devBounce.test.js +28 -0
  133. package/core/js/__tests__/html.util.test.js +18 -0
  134. package/core/js/__tests__/pluginFolder.util.test.js +106 -0
  135. package/core/js/__tests__/pluginPackage.test.js +134 -0
  136. package/core/js/__tests__/pluginUiZip.test.js +47 -0
  137. package/core/js/__tests__/sdkLevel.test.js +34 -0
  138. package/core/js/api.util.js +433 -0
  139. package/core/js/appStores.js +93 -0
  140. package/core/js/basePath.js +10 -0
  141. package/core/js/html.util.js +26 -0
  142. package/core/js/pluginFolder.util.js +95 -0
  143. package/core/js/tooltip.util.js +59 -0
  144. package/core/js/variables.js +95 -0
  145. package/core/js/version.util.js +100 -0
  146. package/core/scss/_pano-tokens.scss +51 -0
  147. package/core/scss/custom-bootstrap.scss +210 -0
  148. package/core/scss/fonts.scss +85 -0
  149. package/core/scss/main.scss +389 -0
  150. package/core/scss/themes.scss +122 -0
  151. package/package.json +63 -11
  152. package/src/__tests__/controllers.test.js +220 -0
  153. package/src/__tests__/views.spike.test.js +472 -0
  154. package/src/components/{index.js → panel.js} +9 -7
  155. package/src/components/theme.js +18 -0
  156. package/src/controllers.js +127 -0
  157. package/src/index.js +1 -1
  158. package/src/runtime-specifiers.js +88 -0
  159. package/src/svelte.js +3 -2
  160. package/src/types.js +5 -3
  161. package/src/utils/api.js +42 -1
  162. package/src/utils/auth.js +8 -0
  163. package/src/utils/component.js +4 -2
  164. package/src/utils/route.js +16 -0
  165. package/src/utils/text.js +8 -0
  166. package/src/views.js +29 -0
@@ -0,0 +1,377 @@
1
+ import { afterAll, describe, expect, test } from 'bun:test';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { migrateV1, errorCodeOf, newDeclared, addApiLevelHelper } from '../migrate-v1.mjs';
5
+ import { rewriteResults, rewritePathLiterals, mapClientPath } from '../js-scan.mjs';
6
+ import { FIXTURES, cleanup, copyFixture, runCli, snapshot } from './helpers.mjs';
7
+
8
+ afterAll(cleanup);
9
+
10
+ const quiet = async (fn) => {
11
+ const e = console.error;
12
+ console.error = () => {};
13
+ try {
14
+ return await fn();
15
+ } finally {
16
+ console.error = e;
17
+ }
18
+ };
19
+
20
+ describe('plugin migration against the golden fixture', () => {
21
+ test('old fixture -> plugin-new, every step', async () => {
22
+ const dir = copyFixture('plugin-old');
23
+ const r = await quiet(() => migrateV1({ root: dir, strip: 'demo' }));
24
+ expect(r.errors).toEqual([]);
25
+ expect(r.leftovers).toEqual([]);
26
+ expect(snapshot(dir)).toEqual(snapshot(path.join(FIXTURES, 'plugin-new')));
27
+ });
28
+
29
+ test('a second run changes nothing and --check agrees', async () => {
30
+ const dir = copyFixture('plugin-new');
31
+ const before = snapshot(dir);
32
+ const again = await quiet(() => migrateV1({ root: dir, strip: 'demo' }));
33
+ expect(again.changes.size).toBe(0);
34
+ expect(snapshot(dir)).toEqual(before);
35
+ const check = await quiet(() => migrateV1({ root: dir, strip: 'demo', check: true }));
36
+ expect(check.changes.size).toBe(0);
37
+ expect(check.leftovers).toEqual([]);
38
+ });
39
+
40
+ test('--check on the old tree writes nothing and reports what would change', async () => {
41
+ const dir = copyFixture('plugin-old');
42
+ const before = snapshot(dir);
43
+ const r = await quiet(() => migrateV1({ root: dir, strip: 'demo', check: true }));
44
+ expect(snapshot(dir)).toEqual(before);
45
+ expect([...r.changes.keys()]).toContain('src/main/kotlin/com/example/demo/error/Errors.kt');
46
+ expect(runCli(['migrate-v1', '--check', '--strip', 'demo', dir]).code).toBe(1);
47
+ });
48
+
49
+ test('acceptance: --check on the migrated fixture exits 0', () => {
50
+ const r = runCli(['migrate-v1', '--check', path.join(FIXTURES, 'plugin-new')]);
51
+ expect(r.code).toBe(0);
52
+ });
53
+ });
54
+
55
+ describe('step a: Kotlin paths', () => {
56
+ const run = async (strip) => {
57
+ const dir = copyFixture('plugin-old');
58
+ await quiet(() => migrateV1({ root: dir, strip, only: ['a'] }));
59
+ return dir;
60
+ };
61
+ const kt = (dir, file) => fs.readFileSync(path.join(dir, 'src/main/kotlin/com/example/demo/routes', file), 'utf8');
62
+
63
+ test('--strip removes the plugin segment, /panel goes for a PanelApi, other segments stay', async () => {
64
+ const dir = await run('demo');
65
+ expect(kt(dir, 'GetThingsAPI.kt')).toContain('Path("/things", RouteType.GET)');
66
+ expect(kt(dir, 'GetThingsAPI.kt')).toContain('Path("/other/list", RouteType.GET)');
67
+ expect(kt(dir, 'PanelThingAPI.kt')).toContain('Path("/things/:id", RouteType.PUT)');
68
+ });
69
+
70
+ test('without --strip only /api and /panel go', async () => {
71
+ const dir = await run(undefined);
72
+ expect(kt(dir, 'GetThingsAPI.kt')).toContain('Path("/demo/things", RouteType.GET)');
73
+ expect(kt(dir, 'PanelThingAPI.kt')).toContain('Path("/demo/things/:id", RouteType.PUT)');
74
+ });
75
+
76
+ test('const val paths are rewritten where they are defined, templates and concatenation keep working', async () => {
77
+ const dir = await run('demo');
78
+ const src = kt(dir, 'WebhookAPI.kt');
79
+ expect(src).toContain('const val WEBHOOK_PATH = "/webhook/:id"');
80
+ expect(src).toContain('const val RETURN_PATH = "/payments/:providerId/return"');
81
+ expect(src).toContain('Path("$WEBHOOK_PATH/:channel", RouteType.ROUTE)');
82
+ expect(src).toContain('Path(Inbound.RETURN_PATH + "/:name", RouteType.GET)');
83
+ });
84
+
85
+ test('strings that are not Path declarations are untouched', async () => {
86
+ const dir = await run('demo');
87
+ expect(kt(dir, 'ValidatedAPI.kt')).toContain('val doc = "see /api/demo/validated"');
88
+ });
89
+
90
+ test('writes api/paths.old-new.json with full old and new paths, merged on later runs', async () => {
91
+ const dir = await run('demo');
92
+ const pairs = JSON.parse(fs.readFileSync(path.join(dir, 'api/paths.old-new.json'), 'utf8'));
93
+ const panel = pairs.find((p) => p.old === '/api/panel/demo/things/:id');
94
+ expect(panel).toMatchObject({ method: 'PUT', new: '/api/plugins/pano-plugin-demo/panel/things/:id', relative: '/plugins/pano-plugin-demo/panel/things/:id' });
95
+ const site = pairs.find((p) => p.old === '/api/demo/things');
96
+ expect(site.new).toBe('/api/plugins/pano-plugin-demo/things');
97
+ await quiet(() => migrateV1({ root: dir, strip: 'demo', only: ['a'] }));
98
+ expect(JSON.parse(fs.readFileSync(path.join(dir, 'api/paths.old-new.json'), 'utf8'))).toEqual(pairs);
99
+ });
100
+
101
+ test('newDeclared: core renames, panel prefix, strip', () => {
102
+ const core = { isCore: true, namespace: 'SITE' };
103
+ expect(newDeclared('/api/siteInfo', core)).toBe('/site-info');
104
+ expect(newDeclared('/api/auth/verifyLinkCode', core)).toBe('/auth/verify-link-code');
105
+ expect(newDeclared('/api/ticket/categories', core)).toBe('/ticket-categories');
106
+ expect(newDeclared('/api/post/thumbnail/:filename', core)).toBe('/posts/thumbnails/:filename');
107
+ expect(newDeclared('/api/notifications/quick/markAsRead', core)).toBe('/notifications/quick/mark-as-read');
108
+ expect(newDeclared('/api/panel/plugins/:pluginId/license', { isCore: true, namespace: 'PANEL' })).toBe('/addons/:pluginId/license');
109
+ expect(newDeclared('/api/panel/market/products', { isCore: false, strip: 'market', namespace: 'PANEL' })).toBe('/products');
110
+ expect(newDeclared('/api/market', { isCore: false, strip: 'market', namespace: 'SITE' })).toBe('/');
111
+ expect(newDeclared('/api/marketing/x', { isCore: false, strip: 'market', namespace: 'SITE' })).toBe('/marketing/x');
112
+ });
113
+ });
114
+
115
+ describe('core migration', () => {
116
+ test('old core fixture -> core-new (renames, overrides, error codes, pairs under Pano/api)', async () => {
117
+ const dir = copyFixture('core-old');
118
+ const r = await quiet(() => migrateV1({ root: dir }));
119
+ expect(r.errors).toEqual([]);
120
+ expect(snapshot(dir)).toEqual(snapshot(path.join(FIXTURES, 'core-new')));
121
+ expect(r.warnings.join('\n')).toContain('Banned extends the error class NotExists');
122
+ });
123
+
124
+ test('a PanelApi serving a path outside /panel and a site class under /api/panel are reported when no override exists', async () => {
125
+ const dir = copyFixture('core-old');
126
+ const f = path.join(dir, 'Pano/src/main/kotlin/com/panomc/platform/route/api/panel/PanelAddonsAPI.kt');
127
+ fs.writeFileSync(f, fs.readFileSync(f, 'utf8').replace(/ override val namespace = Namespace\.\w+\n/g, ''));
128
+ const r = await quiet(() => migrateV1({ root: dir, only: ['a'], check: true }));
129
+ const w = r.warnings.join('\n');
130
+ expect(w).toContain('PanelGetDefaultServerIconAPI is a PanelApi serving /api/server/icon/default');
131
+ expect(w).toContain('PanelIsUserExistsAPI extends Api but serves /api/panel/players/:username/exists');
132
+ });
133
+ });
134
+
135
+ describe('step b: error codes', () => {
136
+ test('errorCodeOf matches TextUtil.convertToSnakeCase().uppercase()', () => {
137
+ expect(errorCodeOf('EmptyCart')).toBe('EMPTY_CART');
138
+ expect(errorCodeOf('TwoFactorInvalidCode')).toBe('TWO_FACTOR_INVALID_CODE');
139
+ expect(errorCodeOf('NotExists')).toBe('NOT_EXISTS');
140
+ expect(errorCodeOf('HTTPError')).toBe('HTTPERROR');
141
+ expect(errorCodeOf('Oauth2Failed')).toBe('OAUTH2FAILED');
142
+ });
143
+
144
+ test('rewrites every supertype call to Error( to a literal UPPER_SNAKE code, whatever the arguments', async () => {
145
+ const dir = copyFixture('plugin-old');
146
+ await quiet(() => migrateV1({ root: dir, only: ['b'] }));
147
+ const src = fs.readFileSync(path.join(dir, 'src/main/kotlin/com/example/demo/error/Errors.kt'), 'utf8');
148
+ expect(src).toContain('class EmptyCart : Error("EMPTY_CART", 400)');
149
+ expect(src).toContain('class NotFoundThing(extras: Map<String, Any?> = mapOf()) : Error("NOT_FOUND_THING", 404, extras = extras)');
150
+ expect(src).toContain('class TwoFactorRequired : Error("TWO_FACTOR_REQUIRED")');
151
+ expect(src).toContain('class MultiLine : Error("MULTI_LINE",\n 403,');
152
+ expect(src).toContain('class AlreadyMigrated : Error("ALREADY_MIGRATED", 409)');
153
+ });
154
+
155
+ test('a class that does not import the platform Error is left alone', async () => {
156
+ const dir = copyFixture('plugin-old');
157
+ await quiet(() => migrateV1({ root: dir, only: ['b'] }));
158
+ expect(fs.readFileSync(path.join(dir, 'src/main/kotlin/com/example/demo/error/Plain.kt'), 'utf8')).toContain('Error("just a message")');
159
+ });
160
+ });
161
+
162
+ describe('step c: validation import prefix', () => {
163
+ test('class and member imports move to the schema dsl, other vertx imports stay', async () => {
164
+ const dir = copyFixture('plugin-old');
165
+ await quiet(() => migrateV1({ root: dir, only: ['c'] }));
166
+ const src = fs.readFileSync(path.join(dir, 'src/main/kotlin/com/example/demo/routes/ValidatedAPI.kt'), 'utf8');
167
+ expect(src).toContain('import com.panomc.platform.schema.dsl.Bodies.json');
168
+ expect(src).toContain('import com.panomc.platform.schema.dsl.Parameters.param');
169
+ expect(src).toContain('import com.panomc.platform.schema.dsl.ValidationHandlerBuilder');
170
+ expect(src).toContain('import io.vertx.ext.web.validation.ValidationHandler\n');
171
+ expect(src).not.toContain('validation.builder');
172
+ });
173
+ });
174
+
175
+ describe('step d: api-level helper', () => {
176
+ test('adds the helper and the manifest attribute once', () => {
177
+ const src = fs.readFileSync(path.join(FIXTURES, 'plugin-old/build.gradle.kts'), 'utf8');
178
+ const r = addApiLevelHelper(src);
179
+ expect(r.changed).toBe(true);
180
+ expect(r.text).toContain('val panoApiLevel: String? by lazy');
181
+ expect(r.text).toContain('rootProject.findProperty("panoApiLevel")');
182
+ expect(r.text).toContain('pano-api-level.properties');
183
+ expect(r.text).toContain(' panoApiLevel?.let { attributes["api-level"] = it }'.replace(' ', ' '));
184
+ expect(addApiLevelHelper(r.text).changed).toBe(false);
185
+ });
186
+
187
+ test('the helper uses short names with the two imports at the top (inside a Gradle script java. is the java {} extension)', () => {
188
+ const src = fs.readFileSync(path.join(FIXTURES, 'plugin-old/build.gradle.kts'), 'utf8');
189
+ const { text } = addApiLevelHelper(src);
190
+ expect(text.startsWith('import java.util.Properties\nimport java.util.zip.ZipFile\n')).toBe(true);
191
+ expect(text).toContain('else ZipFile(jar).use');
192
+ expect(text).toContain('Properties().apply');
193
+ expect(text).not.toContain('java.util.zip.ZipFile(');
194
+ expect(text).not.toContain('java.util.Properties()');
195
+ });
196
+
197
+ test('imports go below a leading comment and are not doubled when the script already has them', () => {
198
+ const body = 'plugins { kotlin("jvm") }\ntasks {\n shadowJar {\n manifest {\n attributes["id"] = "x"\n }\n }\n}\n';
199
+ const { text } = addApiLevelHelper('// header\nimport java.util.Properties\n\n' + body);
200
+ expect(text.match(/import java\.util\.Properties/g)).toHaveLength(1);
201
+ expect(text.match(/import java\.util\.zip\.ZipFile/g)).toHaveLength(1);
202
+ expect(text.indexOf('import java.util.zip.ZipFile')).toBeLessThan(text.indexOf('plugins {'));
203
+ expect(text.startsWith('// header\n')).toBe(true);
204
+ expect(addApiLevelHelper(text).changed).toBe(false);
205
+ const both = addApiLevelHelper('// header\nimport java.util.Properties\nimport java.util.zip.ZipFile\n\n' + body).text;
206
+ expect(both.match(/^import /gm)).toHaveLength(2);
207
+ expect(both).toContain('panoApiLevel?.let');
208
+ });
209
+
210
+ test('a helper written by the earlier version (fully qualified names) is repaired once', () => {
211
+ const old = [
212
+ 'plugins { kotlin("jvm") }',
213
+ '// api-level',
214
+ 'val panoApiLevel: String? by lazy {',
215
+ ' java.util.zip.ZipFile(jar).use { z -> java.util.Properties().apply { } }',
216
+ '}',
217
+ '',
218
+ ].join('\n');
219
+ const r = addApiLevelHelper(old);
220
+ expect(r.changed).toBe(true);
221
+ expect(r.text).toContain('ZipFile(jar).use');
222
+ expect(r.text).not.toContain('java.util.zip.ZipFile(');
223
+ expect(r.text.startsWith('import java.util.Properties\nimport java.util.zip.ZipFile\n')).toBe(true);
224
+ expect(addApiLevelHelper(r.text).changed).toBe(false);
225
+ });
226
+
227
+ test('a build file without a manifest block is left alone', () => {
228
+ expect(addApiLevelHelper('plugins { kotlin("jvm") }\n').changed).toBe(false);
229
+ });
230
+ });
231
+
232
+ describe('step e: client path literals', () => {
233
+ const ctx = {
234
+ strip: 'demo', pluginId: 'pano-plugin-demo',
235
+ pairs: [
236
+ { method: 'GET', old: '/api/demo/things/:id', new: '/api/plugins/pano-plugin-demo/things/:id', relative: '/plugins/pano-plugin-demo/things/:id' },
237
+ { method: 'GET', old: '/api/demo/things/featured', new: '/api/plugins/pano-plugin-demo/featured', relative: '/plugins/pano-plugin-demo/featured' },
238
+ ],
239
+ };
240
+
241
+ test('path: relative, src/href/fetch/WebSocket absolute under /api/v1', () => {
242
+ const src = [
243
+ "ApiUtil.get({ path: '/api/siteInfo' });",
244
+ 'fetch(`/api/panel/plugins/${id}?x=1`);',
245
+ '<img src="/api/websiteLogo" /><a href={`${base}/api/demo/things/${id}`}>',
246
+ 'new WebSocket("wss://host/api/ws");',
247
+ "const route = { path: '/api/not-through-this-rule' }.other; const view = { slot: '/api/x' };",
248
+ ].join('\n');
249
+ const { text } = rewritePathLiterals(src, ctx);
250
+ expect(text).toContain("ApiUtil.get({ path: '/site-info' });");
251
+ expect(text).toContain('fetch(`/api/v1/panel/addons/${id}?x=1`);');
252
+ expect(text).toContain('src="/api/v1/website-logo"');
253
+ expect(text).toContain('href={`${base}/api/plugins/pano-plugin-demo/things/${id}`}');
254
+ expect(text).toContain('new WebSocket("wss://host/api/v1/ws");');
255
+ expect(text).toContain("const view = { slot: '/api/x' };");
256
+ });
257
+
258
+ test('the most specific pair wins, then the core table, then --strip, then the plain /api drop', () => {
259
+ expect(mapClientPath('/demo/things/featured', ctx).path).toBe('/plugins/pano-plugin-demo/featured');
260
+ expect(mapClientPath('/demo/things/${id}', ctx).path).toBe('/plugins/pano-plugin-demo/things/${id}');
261
+ expect(mapClientPath('/ticket/categories', ctx)).toEqual({ path: '/ticket-categories', how: 'core' });
262
+ expect(mapClientPath('/demo/other', ctx)).toEqual({ path: '/plugins/pano-plugin-demo/other', how: 'strip' });
263
+ expect(mapClientPath('/panel/demo/other', ctx).path).toBe('/plugins/pano-plugin-demo/panel/other');
264
+ expect(mapClientPath('/posts', ctx)).toEqual({ path: '/posts', how: 'generic' });
265
+ });
266
+
267
+ test('text after the matched path stays: ${queryParams} survives', () => {
268
+ const pairs = [
269
+ { method: 'GET', old: '/api/staff/config', new: 'x', relative: '/staff/config' },
270
+ { method: 'GET', old: '/api/staff/:id', new: 'x', relative: '/staff/:id' },
271
+ ];
272
+ const text = (src) => rewritePathLiterals(src, { pairs }).text;
273
+ expect(text('ApiUtil.get({ path: `/panel/api/staff/${id}${queryParams}` });')).toBe('ApiUtil.get({ path: `/staff/${id}${queryParams}` });');
274
+ expect(text('ApiUtil.get({ path: `/api/staff/config${queryParams}` });')).toBe('ApiUtil.get({ path: `/staff/config${queryParams}` });');
275
+ expect(text('fetch(`/api/siteInfo${suffix}`);')).toBe('fetch(`/api/v1/site-info${suffix}`);');
276
+ expect(text('ApiUtil.get({ path: `/api/staff/config?a=${a}` });')).toBe('ApiUtil.get({ path: `/staff/config?a=${a}` });');
277
+ });
278
+
279
+ test('a path with a ${} hole maps only onto a route with a parameter at that position', () => {
280
+ const onlyFixed = { pairs: [{ method: 'GET', old: '/api/staff/config', new: 'x', relative: '/staff/config' }] };
281
+ expect(mapClientPath('/staff/${id}', onlyFixed)).toEqual({ path: '/staff/${id}', how: 'generic' });
282
+ expect(rewritePathLiterals('ApiUtil.get({ path: `/panel/api/staff/${id}` });', onlyFixed).text).toBe('ApiUtil.get({ path: `/staff/${id}` });');
283
+ const both = { pairs: [...onlyFixed.pairs, { method: 'GET', old: '/api/staff/:id', new: 'x', relative: '/staff/:id' }] };
284
+ expect(mapClientPath('/staff/${id}', both)).toEqual({ path: '/staff/${id}', how: 'pair' });
285
+ expect(mapClientPath('/staff/config', both).path).toBe('/staff/config');
286
+ // a hole never names a fixed core rename either
287
+ expect(mapClientPath('/auth/${action}', {})).toEqual({ path: '/auth/${action}', how: 'generic' });
288
+ });
289
+
290
+ test('already migrated literals and /panel/api aliases', () => {
291
+ expect(rewritePathLiterals("fetch('/api/v1/posts')", ctx).count).toBe(0);
292
+ expect(rewritePathLiterals("fetch('/panel/api/panel/basicData')", ctx).text).toBe("fetch('/api/v1/panel/basicData')");
293
+ });
294
+ });
295
+
296
+ describe('step f: result reads only on API values', () => {
297
+ test('rewrites reads on variables that receive client call results', () => {
298
+ const src = `
299
+ const a = await ApiUtil.get({ path: '/x' });
300
+ if (a.error === 'NOT_FOUND') {}
301
+ if (a.error !== "BANNED") {}
302
+ if (a.result === 'ok') {}
303
+ if (a.result !== 'ok') {}
304
+ if (a.result === 'error' || a.result == "errors") {}
305
+ const f = a.errors;
306
+ if (a?.error === 'X') {}
307
+ `;
308
+ const { text, count } = rewriteResults(src);
309
+ expect(count).toBe(8);
310
+ expect(text).toContain("if (a.error?.code === 'NOT_FOUND') {}");
311
+ expect(text).toContain('if (a.error?.code !== "BANNED") {}');
312
+ expect(text).toContain('if (!a.error) {}');
313
+ expect(text).toContain('if (!!a.error) {}');
314
+ expect(text).toContain('if (!!a.error || !!a.error) {}');
315
+ expect(text).toContain('const f = a.error?.fields;');
316
+ expect(text).toContain("if (a?.error?.code === 'X') {}");
317
+ });
318
+
319
+ test('leaves other values alone and tracks .then parameters and plugin-api clients', () => {
320
+ const src = `
321
+ import { api } from '@panomc/sdk/plugin-api';
322
+ const other = { error: 'X', result: 'ok', errors: {} };
323
+ if (other.error === 'X' && other.result === 'ok') console.log(other.errors);
324
+ const body = await api.panel.post({ path: '/save' });
325
+ if (body.result === 'ok') {}
326
+ api.get({ path: '/list' }).then((res) => res.error === 'LATE');
327
+ function plain(data) { return data.result === 'ok' || data.error === 'Z'; }
328
+ const mine = createPluginApi('p');
329
+ `;
330
+ const { text } = rewriteResults(src);
331
+ expect(text).toContain("other.error === 'X' && other.result === 'ok'");
332
+ expect(text).toContain('console.log(other.errors)');
333
+ expect(text).toContain('if (!body.error) {}');
334
+ expect(text).toContain("res.error?.code === 'LATE'");
335
+ expect(text).toContain("data.result === 'ok' || data.error === 'Z'");
336
+ });
337
+
338
+ test('is idempotent', () => {
339
+ const once = rewriteResults("const a = await ApiUtil.get({ path: '/x' }); if (a.error === 'E') {}").text;
340
+ expect(rewriteResults(once)).toEqual({ text: once, count: 0 });
341
+ });
342
+ });
343
+
344
+ describe('leftovers in --check', () => {
345
+ test('/api/ literals in Kotlin, .result comparisons and totalPage are reported', async () => {
346
+ const dir = copyFixture('plugin-new');
347
+ fs.writeFileSync(path.join(dir, 'src/main/kotlin/com/example/demo/Left.kt'), 'package x\nval u = "/api/old"\n// "/api/in-comment"\n');
348
+ fs.writeFileSync(path.join(dir, 'src/theme/left.js'), "if (x.result === 'ok') {}\nconst n = res.totalPage;\nfetch('/api/old');\nconst hook = origin + '/api/';\n");
349
+ const r = await quiet(() => migrateV1({ root: dir, check: true }));
350
+ const whats = r.leftovers.map((l) => `${l.file}:${l.line} ${l.what}`).sort();
351
+ expect(whats).toEqual([
352
+ 'src/main/kotlin/com/example/demo/Left.kt:2 "/api/ literal',
353
+ 'src/theme/left.js:1 .result comparison',
354
+ 'src/theme/left.js:2 totalPage',
355
+ ]);
356
+ expect([...r.changes.get('src/theme/left.js')]).toContain('e'); // the path literal is a change, not a leftover
357
+ expect(runCliExit(dir)).toBe(1);
358
+ });
359
+ });
360
+
361
+ function runCliExit(dir) {
362
+ return runCli(['migrate-v1', '--check', dir]).code;
363
+ }
364
+
365
+ describe('--only', () => {
366
+ test('runs just the named steps', async () => {
367
+ const dir = copyFixture('plugin-old');
368
+ const r = await quiet(() => migrateV1({ root: dir, strip: 'demo', only: ['b', 'c'] }));
369
+ const steps = new Set([...r.changes.values()].flatMap((s) => [...s]));
370
+ expect([...steps].sort()).toEqual(['b', 'c']);
371
+ expect(fs.existsSync(path.join(dir, 'api/paths.old-new.json'))).toBe(false);
372
+ });
373
+
374
+ test('the CLI refuses a bad letter', () => {
375
+ expect(runCli(['migrate-v1', '--only', 'z', FIXTURES]).code).toBe(2);
376
+ });
377
+ });
@@ -0,0 +1,120 @@
1
+ import { afterAll, afterEach, beforeEach, describe, expect, test } from 'bun:test';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ import { runOpenapiSnapshot, findPluginRepos } from '../openapi-snapshot.mjs';
5
+ import { stableJson } from '../util.mjs';
6
+ import { cleanup, runCli, tempDir } from './helpers.mjs';
7
+
8
+ afterAll(cleanup);
9
+
10
+ const doc = (paths) => ({ openapi: '3.1.0', info: { title: 'Pano', version: '1' }, paths });
11
+ const getOp = (props) => ({
12
+ get: { responses: { 200: { description: 'ok', content: { 'application/json': { schema: { type: 'object', properties: props } } } } } },
13
+ });
14
+
15
+ /** @type {Record<string, any>} */
16
+ let served;
17
+ let server;
18
+ let base;
19
+
20
+ beforeEach(() => {
21
+ served = {
22
+ '/api/v1/openapi.json': doc({ '/posts': getOp({ items: { type: 'array' } }) }),
23
+ '/api/v1/panel/openapi.json': doc({ '/panel/x': getOp({}) }),
24
+ '/api/v1/plugins/pano-plugin-demo/_/openapi.json': doc({ '/things': getOp({ id: { type: 'integer' } }) }),
25
+ };
26
+ server = Bun.serve({
27
+ port: 0,
28
+ fetch(req) {
29
+ const body = served[new URL(req.url).pathname];
30
+ return body ? Response.json(body) : new Response('{}', { status: 404 });
31
+ },
32
+ });
33
+ base = `http://127.0.0.1:${server.port}`;
34
+ });
35
+ afterEach(() => server.stop(true));
36
+
37
+ /** A workspace: Pano/api for core, plugins/<id> with a gradle.properties. */
38
+ function workspace(withPlugin = true) {
39
+ const root = tempDir();
40
+ fs.mkdirSync(path.join(root, 'Pano', 'api'), { recursive: true });
41
+ if (withPlugin) {
42
+ fs.mkdirSync(path.join(root, 'plugins', 'pano-plugin-demo'), { recursive: true });
43
+ fs.writeFileSync(path.join(root, 'plugins', 'pano-plugin-demo', 'gradle.properties'), 'pluginId=pano-plugin-demo\n');
44
+ }
45
+ return { root, coreDir: path.join(root, 'Pano', 'api'), pluginsDir: path.join(root, 'plugins') };
46
+ }
47
+ const quiet = async (fn) => {
48
+ const e = console.error;
49
+ console.error = () => {};
50
+ try {
51
+ return await fn();
52
+ } finally {
53
+ console.error = e;
54
+ }
55
+ };
56
+ const run = (ws, extra) => quiet(() => runOpenapiSnapshot({ url: base, coreDir: ws.coreDir, pluginsDir: ws.pluginsDir, ...extra }));
57
+
58
+ describe('openapi-snapshot', () => {
59
+ test('finds plugin repositories by their gradle.properties', () => {
60
+ const ws = workspace();
61
+ expect(findPluginRepos(ws.pluginsDir).map((r) => r.id)).toEqual(['pano-plugin-demo']);
62
+ });
63
+
64
+ test('--check fails while snapshots are missing, --write creates the three files, --check then passes', async () => {
65
+ const ws = workspace();
66
+ expect(await run(ws, { check: true })).toBe(1);
67
+ expect(await run(ws, { write: true })).toBe(0);
68
+ expect(fs.existsSync(path.join(ws.coreDir, 'openapi-core.json'))).toBe(true);
69
+ expect(fs.existsSync(path.join(ws.coreDir, 'openapi-internal.json'))).toBe(true);
70
+ expect(fs.readFileSync(path.join(ws.pluginsDir, 'pano-plugin-demo', 'api', 'openapi.json'), 'utf8')).toBe(stableJson(served['/api/v1/plugins/pano-plugin-demo/_/openapi.json']));
71
+ expect(await run(ws, { check: true })).toBe(0);
72
+ expect(await run(ws, { write: true })).toBe(0);
73
+ });
74
+
75
+ test('an additive change makes --check fail (stale snapshot) without being a break', async () => {
76
+ const ws = workspace();
77
+ await run(ws, { write: true });
78
+ served['/api/v1/openapi.json'].paths['/tags'] = getOp({});
79
+ expect(await run(ws, { check: true })).toBe(1);
80
+ expect(await run(ws, { write: true })).toBe(0);
81
+ expect(await run(ws, { check: true })).toBe(0);
82
+ });
83
+
84
+ test('a removed operation is refused on --write unless --allow-breaks, and fails --check', async () => {
85
+ const ws = workspace();
86
+ await run(ws, { write: true });
87
+ delete served['/api/v1/openapi.json'].paths['/posts'];
88
+ expect(await run(ws, { check: true })).toBe(1);
89
+ expect(await run(ws, { write: true })).toBe(1);
90
+ expect(JSON.parse(fs.readFileSync(path.join(ws.coreDir, 'openapi-core.json'), 'utf8')).paths['/posts']).toBeDefined();
91
+ expect(await run(ws, { write: true, allowBreaks: true })).toBe(0);
92
+ });
93
+
94
+ test('a deprecated operation whose removal date has passed may disappear', async () => {
95
+ const ws = workspace();
96
+ served['/api/v1/openapi.json'].paths['/old'] = { get: { ...getOp({}).get, deprecated: true, 'x-pano-removal': '2026-01-01', 'x-pano-deprecated-on': '2025-01-01' } };
97
+ await run(ws, { write: true });
98
+ delete served['/api/v1/openapi.json'].paths['/old'];
99
+ expect(await run(ws, { write: true, today: '2026-10-08' })).toBe(0);
100
+ });
101
+
102
+ test('a plugin the instance does not serve is skipped, a missing core document is an error', async () => {
103
+ const ws = workspace();
104
+ delete served['/api/v1/plugins/pano-plugin-demo/_/openapi.json'];
105
+ expect(await run(ws, { write: true })).toBe(0);
106
+ expect(fs.existsSync(path.join(ws.pluginsDir, 'pano-plugin-demo', 'api', 'openapi.json'))).toBe(false);
107
+ delete served['/api/v1/openapi.json'];
108
+ expect(await run(ws, { write: true })).toBe(2);
109
+ });
110
+
111
+ test('an unreachable instance exits 2', async () => {
112
+ const ws = workspace();
113
+ expect(await quiet(() => runOpenapiSnapshot({ url: 'http://127.0.0.1:1', coreDir: ws.coreDir, write: true }))).toBe(2);
114
+ });
115
+
116
+ test('the CLI needs --url and --write or --check', () => {
117
+ expect(runCli(['openapi-snapshot', '--write']).code).toBe(2);
118
+ expect(runCli(['openapi-snapshot', '--url', 'http://x']).code).toBe(2);
119
+ });
120
+ });