@digitalpaws/starr 0.0.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 (290) hide show
  1. package/README.rst +75 -0
  2. package/data/.nfs.811bbb0d.43c34.4 +0 -0
  3. package/data/.nfs.81ba4cd0.ffff87804.4 +0 -0
  4. package/data/.nfs.82d127f7.fffff5704.4 +0 -0
  5. package/data/.nfs.82d127fe.fffff5704.4 +0 -0
  6. package/data/.nfs.82d159a5.fffff5aa4.4 +0 -0
  7. package/data/.nfs.82d159aa.fffff5aa4.4 +0 -0
  8. package/data/.nfs.82d15a9c.fffff5bb4.4 +0 -0
  9. package/data/.nfs.82d15aa1.fffff5bb4.4 +0 -0
  10. package/data/.nfs.82d15c65.fffff5c24.4 +0 -0
  11. package/data/.nfs.82d1852c.fffff5e24.4 +0 -0
  12. package/data/.nfs.82d18536.fffff5e24.4 +0 -0
  13. package/data/.nfs.82d18dbe.fffff5e44.4 +0 -0
  14. package/data/.nfs.82d18dc6.fffff5e44.4 +0 -0
  15. package/data/.nfs.8309a3f5.fffff82f4.4 +0 -0
  16. package/data/.nfs.8309a3fe.fffff82f4.4 +0 -0
  17. package/data/.nfs.8314122e.fffffb044.4 +0 -0
  18. package/data/.nfs.83141236.fffffb044.4 +0 -0
  19. package/data/.nfs.833c4fe9.fffffbc34.4 +0 -0
  20. package/data/.nfs.833c4ff3.fffffbc34.4 +0 -0
  21. package/data/logs/starr.log +66244 -0
  22. package/data/starr.db +0 -0
  23. package/data/starr.db-shm +0 -0
  24. package/data/starr.db-wal +0 -0
  25. package/data/starr.jwt +1 -0
  26. package/data/starr.key +1 -0
  27. package/dist/auth/auth-service.d.ts +42 -0
  28. package/dist/auth/auth-service.d.ts.map +1 -0
  29. package/dist/auth/auth-service.js +226 -0
  30. package/dist/auth/auth-service.js.map +1 -0
  31. package/dist/auth/http-server.d.ts +16 -0
  32. package/dist/auth/http-server.d.ts.map +1 -0
  33. package/dist/auth/http-server.js +207 -0
  34. package/dist/auth/http-server.js.map +1 -0
  35. package/dist/auth/jwt-secret.d.ts +6 -0
  36. package/dist/auth/jwt-secret.d.ts.map +1 -0
  37. package/dist/auth/jwt-secret.js +51 -0
  38. package/dist/auth/jwt-secret.js.map +1 -0
  39. package/dist/auth/oidc/oidc-flow.d.ts +60 -0
  40. package/dist/auth/oidc/oidc-flow.d.ts.map +1 -0
  41. package/dist/auth/oidc/oidc-flow.js +201 -0
  42. package/dist/auth/oidc/oidc-flow.js.map +1 -0
  43. package/dist/auth/oidc/oidc-provider-service.d.ts +59 -0
  44. package/dist/auth/oidc/oidc-provider-service.d.ts.map +1 -0
  45. package/dist/auth/oidc/oidc-provider-service.js +124 -0
  46. package/dist/auth/oidc/oidc-provider-service.js.map +1 -0
  47. package/dist/auth/oidc/user-service.d.ts +67 -0
  48. package/dist/auth/oidc/user-service.d.ts.map +1 -0
  49. package/dist/auth/oidc/user-service.js +177 -0
  50. package/dist/auth/oidc/user-service.js.map +1 -0
  51. package/dist/auth/rate-limiter.d.ts +31 -0
  52. package/dist/auth/rate-limiter.d.ts.map +1 -0
  53. package/dist/auth/rate-limiter.js +70 -0
  54. package/dist/auth/rate-limiter.js.map +1 -0
  55. package/dist/config/config-service.d.ts +83 -0
  56. package/dist/config/config-service.d.ts.map +1 -0
  57. package/dist/config/config-service.js +309 -0
  58. package/dist/config/config-service.js.map +1 -0
  59. package/dist/config/validate.d.ts +12 -0
  60. package/dist/config/validate.d.ts.map +1 -0
  61. package/dist/config/validate.js +140 -0
  62. package/dist/config/validate.js.map +1 -0
  63. package/dist/content-types/registry.d.ts +75 -0
  64. package/dist/content-types/registry.d.ts.map +1 -0
  65. package/dist/content-types/registry.js +171 -0
  66. package/dist/content-types/registry.js.map +1 -0
  67. package/dist/datastore/db.d.ts +9 -0
  68. package/dist/datastore/db.d.ts.map +1 -0
  69. package/dist/datastore/db.js +27 -0
  70. package/dist/datastore/db.js.map +1 -0
  71. package/dist/datastore/encrypt.d.ts +9 -0
  72. package/dist/datastore/encrypt.d.ts.map +1 -0
  73. package/dist/datastore/encrypt.js +84 -0
  74. package/dist/datastore/encrypt.js.map +1 -0
  75. package/dist/datastore/migrations.d.ts +10 -0
  76. package/dist/datastore/migrations.d.ts.map +1 -0
  77. package/dist/datastore/migrations.js +340 -0
  78. package/dist/datastore/migrations.js.map +1 -0
  79. package/dist/datastore/storage.d.ts +4 -0
  80. package/dist/datastore/storage.d.ts.map +1 -0
  81. package/dist/datastore/storage.js +60 -0
  82. package/dist/datastore/storage.js.map +1 -0
  83. package/dist/events/event-bus.d.ts +15 -0
  84. package/dist/events/event-bus.d.ts.map +1 -0
  85. package/dist/events/event-bus.js +140 -0
  86. package/dist/events/event-bus.js.map +1 -0
  87. package/dist/events/spine.d.ts +31 -0
  88. package/dist/events/spine.d.ts.map +1 -0
  89. package/dist/events/spine.js +68 -0
  90. package/dist/events/spine.js.map +1 -0
  91. package/dist/events/trace.d.ts +24 -0
  92. package/dist/events/trace.d.ts.map +1 -0
  93. package/dist/events/trace.js +33 -0
  94. package/dist/events/trace.js.map +1 -0
  95. package/dist/host.d.ts +65 -0
  96. package/dist/host.d.ts.map +1 -0
  97. package/dist/host.js +94 -0
  98. package/dist/host.js.map +1 -0
  99. package/dist/library/importer.d.ts +29 -0
  100. package/dist/library/importer.d.ts.map +1 -0
  101. package/dist/library/importer.js +145 -0
  102. package/dist/library/importer.js.map +1 -0
  103. package/dist/library/library-cache.d.ts +46 -0
  104. package/dist/library/library-cache.d.ts.map +1 -0
  105. package/dist/library/library-cache.js +152 -0
  106. package/dist/library/library-cache.js.map +1 -0
  107. package/dist/library/library-config.d.ts +12 -0
  108. package/dist/library/library-config.d.ts.map +1 -0
  109. package/dist/library/library-config.js +10 -0
  110. package/dist/library/library-config.js.map +1 -0
  111. package/dist/library/library-scanner.d.ts +62 -0
  112. package/dist/library/library-scanner.d.ts.map +1 -0
  113. package/dist/library/library-scanner.js +336 -0
  114. package/dist/library/library-scanner.js.map +1 -0
  115. package/dist/library/library-service.d.ts +65 -0
  116. package/dist/library/library-service.d.ts.map +1 -0
  117. package/dist/library/library-service.js +197 -0
  118. package/dist/library/library-service.js.map +1 -0
  119. package/dist/library/metadata-matcher.d.ts +56 -0
  120. package/dist/library/metadata-matcher.d.ts.map +1 -0
  121. package/dist/library/metadata-matcher.js +207 -0
  122. package/dist/library/metadata-matcher.js.map +1 -0
  123. package/dist/library/naming.d.ts +8 -0
  124. package/dist/library/naming.d.ts.map +1 -0
  125. package/dist/library/naming.js +47 -0
  126. package/dist/library/naming.js.map +1 -0
  127. package/dist/library/scan-controller.d.ts +95 -0
  128. package/dist/library/scan-controller.d.ts.map +1 -0
  129. package/dist/library/scan-controller.js +379 -0
  130. package/dist/library/scan-controller.js.map +1 -0
  131. package/dist/library/source-resolver.d.ts +17 -0
  132. package/dist/library/source-resolver.d.ts.map +1 -0
  133. package/dist/library/source-resolver.js +75 -0
  134. package/dist/library/source-resolver.js.map +1 -0
  135. package/dist/logging/logger.d.ts +3 -0
  136. package/dist/logging/logger.d.ts.map +1 -0
  137. package/dist/logging/logger.js +107 -0
  138. package/dist/logging/logger.js.map +1 -0
  139. package/dist/main.d.ts +36 -0
  140. package/dist/main.d.ts.map +1 -0
  141. package/dist/main.js +229 -0
  142. package/dist/main.js.map +1 -0
  143. package/dist/plugins/context.d.ts +10 -0
  144. package/dist/plugins/context.d.ts.map +1 -0
  145. package/dist/plugins/context.js +110 -0
  146. package/dist/plugins/context.js.map +1 -0
  147. package/dist/plugins/discovery.d.ts +2 -0
  148. package/dist/plugins/discovery.d.ts.map +1 -0
  149. package/dist/plugins/discovery.js +32 -0
  150. package/dist/plugins/discovery.js.map +1 -0
  151. package/dist/plugins/loader.d.ts +9 -0
  152. package/dist/plugins/loader.d.ts.map +1 -0
  153. package/dist/plugins/loader.js +245 -0
  154. package/dist/plugins/loader.js.map +1 -0
  155. package/dist/state/request-intake.d.ts +26 -0
  156. package/dist/state/request-intake.d.ts.map +1 -0
  157. package/dist/state/request-intake.js +74 -0
  158. package/dist/state/request-intake.js.map +1 -0
  159. package/dist/state/state-store.d.ts +146 -0
  160. package/dist/state/state-store.d.ts.map +1 -0
  161. package/dist/state/state-store.js +465 -0
  162. package/dist/state/state-store.js.map +1 -0
  163. package/dist/state/trace-store.d.ts +12 -0
  164. package/dist/state/trace-store.d.ts.map +1 -0
  165. package/dist/state/trace-store.js +24 -0
  166. package/dist/state/trace-store.js.map +1 -0
  167. package/dist/types/plugin.d.ts +93 -0
  168. package/dist/types/plugin.d.ts.map +1 -0
  169. package/dist/types/plugin.js +4 -0
  170. package/dist/types/plugin.js.map +1 -0
  171. package/dist/web/index.d.ts +10 -0
  172. package/dist/web/index.d.ts.map +1 -0
  173. package/dist/web/index.js +153 -0
  174. package/dist/web/index.js.map +1 -0
  175. package/dist/web/routes/activity.d.ts +6 -0
  176. package/dist/web/routes/activity.d.ts.map +1 -0
  177. package/dist/web/routes/activity.js +31 -0
  178. package/dist/web/routes/activity.js.map +1 -0
  179. package/dist/web/routes/auth.d.ts +6 -0
  180. package/dist/web/routes/auth.d.ts.map +1 -0
  181. package/dist/web/routes/auth.js +142 -0
  182. package/dist/web/routes/auth.js.map +1 -0
  183. package/dist/web/routes/config.d.ts +7 -0
  184. package/dist/web/routes/config.d.ts.map +1 -0
  185. package/dist/web/routes/config.js +204 -0
  186. package/dist/web/routes/config.js.map +1 -0
  187. package/dist/web/routes/content-types.d.ts +6 -0
  188. package/dist/web/routes/content-types.d.ts.map +1 -0
  189. package/dist/web/routes/content-types.js +29 -0
  190. package/dist/web/routes/content-types.js.map +1 -0
  191. package/dist/web/routes/discovery.d.ts +6 -0
  192. package/dist/web/routes/discovery.d.ts.map +1 -0
  193. package/dist/web/routes/discovery.js +111 -0
  194. package/dist/web/routes/discovery.js.map +1 -0
  195. package/dist/web/routes/library-import.d.ts +6 -0
  196. package/dist/web/routes/library-import.d.ts.map +1 -0
  197. package/dist/web/routes/library-import.js +120 -0
  198. package/dist/web/routes/library-import.js.map +1 -0
  199. package/dist/web/routes/library-management.d.ts +6 -0
  200. package/dist/web/routes/library-management.d.ts.map +1 -0
  201. package/dist/web/routes/library-management.js +120 -0
  202. package/dist/web/routes/library-management.js.map +1 -0
  203. package/dist/web/routes/library.d.ts +6 -0
  204. package/dist/web/routes/library.d.ts.map +1 -0
  205. package/dist/web/routes/library.js +28 -0
  206. package/dist/web/routes/library.js.map +1 -0
  207. package/dist/web/routes/oidc.d.ts +9 -0
  208. package/dist/web/routes/oidc.d.ts.map +1 -0
  209. package/dist/web/routes/oidc.js +203 -0
  210. package/dist/web/routes/oidc.js.map +1 -0
  211. package/dist/web/routes/requests.d.ts +6 -0
  212. package/dist/web/routes/requests.d.ts.map +1 -0
  213. package/dist/web/routes/requests.js +183 -0
  214. package/dist/web/routes/requests.js.map +1 -0
  215. package/dist/web/routes/sse.d.ts +6 -0
  216. package/dist/web/routes/sse.d.ts.map +1 -0
  217. package/dist/web/routes/sse.js +54 -0
  218. package/dist/web/routes/sse.js.map +1 -0
  219. package/package.json +33 -0
  220. package/package.json.license +2 -0
  221. package/src/auth/auth-service.ts +333 -0
  222. package/src/auth/http-server.ts +249 -0
  223. package/src/auth/jwt-secret.ts +65 -0
  224. package/src/auth/oidc/oidc-flow.ts +266 -0
  225. package/src/auth/oidc/oidc-provider-service.ts +174 -0
  226. package/src/auth/oidc/user-service.ts +227 -0
  227. package/src/auth/rate-limiter.ts +85 -0
  228. package/src/config/config-service.ts +434 -0
  229. package/src/config/validate.ts +153 -0
  230. package/src/content-types/registry.ts +194 -0
  231. package/src/datastore/db.ts +38 -0
  232. package/src/datastore/encrypt.ts +107 -0
  233. package/src/datastore/migrations.ts +367 -0
  234. package/src/datastore/storage.ts +83 -0
  235. package/src/events/event-bus.ts +180 -0
  236. package/src/events/spine.ts +87 -0
  237. package/src/events/trace.ts +38 -0
  238. package/src/host.ts +130 -0
  239. package/src/library/importer.ts +174 -0
  240. package/src/library/library-cache.ts +212 -0
  241. package/src/library/library-config.ts +22 -0
  242. package/src/library/library-scanner.ts +404 -0
  243. package/src/library/library-service.ts +282 -0
  244. package/src/library/metadata-matcher.ts +285 -0
  245. package/src/library/naming.ts +59 -0
  246. package/src/library/scan-controller.ts +490 -0
  247. package/src/library/source-resolver.ts +92 -0
  248. package/src/logging/logger.ts +115 -0
  249. package/src/main.ts +364 -0
  250. package/src/plugins/context.ts +138 -0
  251. package/src/plugins/discovery.ts +29 -0
  252. package/src/plugins/loader.ts +309 -0
  253. package/src/state/request-intake.ts +101 -0
  254. package/src/state/state-store.ts +621 -0
  255. package/src/state/trace-store.ts +27 -0
  256. package/src/types/plugin.ts +110 -0
  257. package/src/web/index.ts +182 -0
  258. package/src/web/routes/activity.ts +41 -0
  259. package/src/web/routes/auth.ts +170 -0
  260. package/src/web/routes/config.ts +282 -0
  261. package/src/web/routes/content-types.ts +42 -0
  262. package/src/web/routes/discovery.ts +156 -0
  263. package/src/web/routes/library-import.ts +196 -0
  264. package/src/web/routes/library-management.ts +140 -0
  265. package/src/web/routes/library.ts +46 -0
  266. package/src/web/routes/oidc.ts +260 -0
  267. package/src/web/routes/requests.ts +250 -0
  268. package/src/web/routes/sse.ts +92 -0
  269. package/tests/auth.test.ts +510 -0
  270. package/tests/config-route.test.ts +345 -0
  271. package/tests/config.test.ts +612 -0
  272. package/tests/content-types.test.ts +376 -0
  273. package/tests/datastore.test.ts +345 -0
  274. package/tests/e2e.test.ts +430 -0
  275. package/tests/event-bus.test.ts +925 -0
  276. package/tests/host.test.ts +449 -0
  277. package/tests/library-cache.test.ts +171 -0
  278. package/tests/library-scanner.test.ts +177 -0
  279. package/tests/library.test.ts +210 -0
  280. package/tests/loader-context.test.ts +644 -0
  281. package/tests/logger-jsonl.test.ts +154 -0
  282. package/tests/metadata-matcher.test.ts +198 -0
  283. package/tests/oidc.test.ts +218 -0
  284. package/tests/scan-controller.test.ts +328 -0
  285. package/tests/source-resolver.test.ts +100 -0
  286. package/tests/starr-video-integration.test.ts +153 -0
  287. package/tests/state-store.test.ts +700 -0
  288. package/tests/trace-history.test.ts +356 -0
  289. package/tsconfig.json +8 -0
  290. package/tsconfig.json.license +2 -0
@@ -0,0 +1,345 @@
1
+ // SPDX-License-Identifier: 0BSD
2
+ // SPDX-FileCopyrightText: 2025 Digi <digitalpaws@proton.me>
3
+
4
+ import { describe, it, expect, beforeEach, afterEach } from "vitest";
5
+ import Database from "better-sqlite3";
6
+ import Fastify from "fastify";
7
+ import { mkdtempSync, existsSync, unlinkSync, rmdirSync } from "node:fs";
8
+ import { tmpdir } from "node:os";
9
+ import path from "node:path";
10
+ import type { FastifyInstance } from "fastify";
11
+
12
+ import { runMigrations, CORE_MIGRATIONS } from "../src/datastore/migrations.js";
13
+ import { loadEncryptionKey, resetEncryptionKey } from "../src/datastore/encrypt.js";
14
+ import { ConfigService, SECRET_UNCHANGED_SENTINEL } from "../src/config/config-service.js";
15
+ import { initConfigRoutes, default as configRoutes } from "../src/web/routes/config.js";
16
+ import { createLogger } from "../src/logging/logger.js";
17
+ import type { ConfigSchema } from "../src/types/plugin.js";
18
+ import type { PluginRuntime } from "../src/host.js";
19
+
20
+ // ── Helpers ────────────────────────────────────────────────────────────
21
+
22
+ const testLogger = createLogger("test", "silent");
23
+
24
+ function createTestDb(): Database.Database {
25
+ const db = new Database(":memory:");
26
+ db.pragma("journal_mode = WAL");
27
+ db.pragma("foreign_keys = ON");
28
+ return db;
29
+ }
30
+
31
+ interface MockRuntimeOpts {
32
+ hasTestConnection?: boolean;
33
+ }
34
+
35
+ function createMockRuntime(
36
+ pluginId: string,
37
+ opts: MockRuntimeOpts = {},
38
+ ): PluginRuntime {
39
+ return {
40
+ meta: { id: pluginId, version: "1.0.0", starrApiVersion: "^1.0.0" },
41
+ lifecycle: {
42
+ testConnection: opts.hasTestConnection
43
+ ? async (values: Record<string, unknown>) => {
44
+ const endpoint = String(values.endpoint ?? "");
45
+ const apiKey = String(values.apiKey ?? "");
46
+ if (!endpoint || !apiKey) {
47
+ return { ok: false, message: "Missing fields" };
48
+ }
49
+ return { ok: true, message: "Test passed" };
50
+ }
51
+ : undefined,
52
+ },
53
+ startOrder: 0,
54
+ };
55
+ }
56
+
57
+ // ── Test helper: inject an admin user for routes that require auth ──
58
+
59
+ function injectAdminUser(app: FastifyInstance): void {
60
+ app.addHook("onRequest", async (request) => {
61
+ (request as any).user = {
62
+ id: "admin",
63
+ username: "admin",
64
+ role: "admin",
65
+ };
66
+ });
67
+ }
68
+
69
+ // ── Tests ──────────────────────────────────────────────────────────────
70
+
71
+ describe("Config Route — GET /api/config/:pluginId (supportsTest)", () => {
72
+ let db: Database.Database;
73
+ let dataDir: string;
74
+ let cs: ConfigService;
75
+ let app: FastifyInstance;
76
+
77
+ beforeEach(async () => {
78
+ db = createTestDb();
79
+ runMigrations(db, CORE_MIGRATIONS, testLogger);
80
+ dataDir = mkdtempSync(path.join(tmpdir(), "starr-config-route-"));
81
+ resetEncryptionKey();
82
+ loadEncryptionKey(dataDir, testLogger);
83
+
84
+ cs = new ConfigService(db, testLogger);
85
+
86
+ // Register a plugin schema
87
+ const schema: ConfigSchema<{ endpoint: string; apiKey: string }> = {
88
+ endpoint: { type: "url", label: "Endpoint", required: true },
89
+ apiKey: { type: "secret", label: "API Key", required: true },
90
+ };
91
+ cs.register("test-plugin", schema);
92
+
93
+ app = Fastify({ logger: false });
94
+ injectAdminUser(app);
95
+ });
96
+
97
+ afterEach(async () => {
98
+ await app.close();
99
+ db.close();
100
+ try {
101
+ const keyPath = path.join(dataDir, "starr.key");
102
+ if (existsSync(keyPath)) unlinkSync(keyPath);
103
+ rmdirSync(dataDir);
104
+ } catch {
105
+ // ignore
106
+ }
107
+ resetEncryptionKey();
108
+ });
109
+
110
+ it("returns supportsTest: true when runtime has testConnection", async () => {
111
+ const runtimes = [createMockRuntime("test-plugin", { hasTestConnection: true })];
112
+ initConfigRoutes(cs, () => runtimes);
113
+ await app.register(configRoutes);
114
+
115
+ const res = await app.inject({
116
+ method: "GET",
117
+ url: "/api/config/test-plugin",
118
+ });
119
+
120
+ expect(res.statusCode).toBe(200);
121
+ const body = JSON.parse(res.body);
122
+ expect(body.supportsTest).toBe(true);
123
+ });
124
+
125
+ it("returns supportsTest: false when runtime lacks testConnection", async () => {
126
+ const runtimes = [createMockRuntime("test-plugin", { hasTestConnection: false })];
127
+ initConfigRoutes(cs, () => runtimes);
128
+ await app.register(configRoutes);
129
+
130
+ const res = await app.inject({
131
+ method: "GET",
132
+ url: "/api/config/test-plugin",
133
+ });
134
+
135
+ expect(res.statusCode).toBe(200);
136
+ const body = JSON.parse(res.body);
137
+ expect(body.supportsTest).toBe(false);
138
+ });
139
+
140
+ it("returns supportsTest: false when no runtime found", async () => {
141
+ const runtimes: PluginRuntime[] = [];
142
+ initConfigRoutes(cs, () => runtimes);
143
+ await app.register(configRoutes);
144
+
145
+ const res = await app.inject({
146
+ method: "GET",
147
+ url: "/api/config/test-plugin",
148
+ });
149
+
150
+ expect(res.statusCode).toBe(200);
151
+ const body = JSON.parse(res.body);
152
+ expect(body.supportsTest).toBe(false);
153
+ });
154
+ });
155
+
156
+ describe("Config Route — POST /api/config/:pluginId/test", () => {
157
+ let db: Database.Database;
158
+ let dataDir: string;
159
+ let cs: ConfigService;
160
+ let app: FastifyInstance;
161
+ let runtimes: PluginRuntime[];
162
+
163
+ beforeEach(async () => {
164
+ db = createTestDb();
165
+ runMigrations(db, CORE_MIGRATIONS, testLogger);
166
+ dataDir = mkdtempSync(path.join(tmpdir(), "starr-config-route-"));
167
+ resetEncryptionKey();
168
+ loadEncryptionKey(dataDir, testLogger);
169
+
170
+ cs = new ConfigService(db, testLogger);
171
+
172
+ // Register a plugin schema
173
+ const schema: ConfigSchema<{ endpoint: string; apiKey: string; requestTimeoutMs: number }> = {
174
+ endpoint: { type: "url", label: "Endpoint", required: true },
175
+ apiKey: { type: "secret", label: "API Key", required: true },
176
+ requestTimeoutMs: { type: "number", label: "Timeout", default: 15000 },
177
+ };
178
+ cs.register("test-plugin", schema);
179
+
180
+ runtimes = [];
181
+
182
+ app = Fastify({ logger: false });
183
+ injectAdminUser(app);
184
+ });
185
+
186
+ afterEach(async () => {
187
+ await app.close();
188
+ db.close();
189
+ try {
190
+ const keyPath = path.join(dataDir, "starr.key");
191
+ if (existsSync(keyPath)) unlinkSync(keyPath);
192
+ rmdirSync(dataDir);
193
+ } catch {
194
+ // ignore
195
+ }
196
+ resetEncryptionKey();
197
+ });
198
+
199
+ it("returns 400 when plugin does not support testConnection", async () => {
200
+ runtimes.push(createMockRuntime("test-plugin", { hasTestConnection: false }));
201
+ initConfigRoutes(cs, () => runtimes);
202
+ await app.register(configRoutes);
203
+
204
+ const res = await app.inject({
205
+ method: "POST",
206
+ url: "/api/config/test-plugin/test",
207
+ body: { endpoint: "http://jackett:9117/api", apiKey: "test-key" },
208
+ });
209
+
210
+ expect(res.statusCode).toBe(400);
211
+ const body = JSON.parse(res.body);
212
+ expect(body.error).toContain("does not support");
213
+ });
214
+
215
+ it("returns 404 for unknown plugin", async () => {
216
+ initConfigRoutes(cs, () => runtimes);
217
+ await app.register(configRoutes);
218
+
219
+ const res = await app.inject({
220
+ method: "POST",
221
+ url: "/api/config/unknown-plugin/test",
222
+ body: {},
223
+ });
224
+
225
+ expect(res.statusCode).toBe(404);
226
+ });
227
+
228
+ it("calls testConnection with raw values and returns result", async () => {
229
+ let calledWith: Record<string, unknown> | undefined;
230
+ runtimes.push({
231
+ meta: { id: "test-plugin", version: "1.0.0", starrApiVersion: "^1.0.0" },
232
+ lifecycle: {
233
+ testConnection: async (values: Record<string, unknown>) => {
234
+ calledWith = values;
235
+ return { ok: true, message: "Test passed" };
236
+ },
237
+ },
238
+ startOrder: 0,
239
+ });
240
+ initConfigRoutes(cs, () => runtimes);
241
+ await app.register(configRoutes);
242
+
243
+ const res = await app.inject({
244
+ method: "POST",
245
+ url: "/api/config/test-plugin/test",
246
+ body: { endpoint: "http://jackett:9117/api", apiKey: "test-key" },
247
+ });
248
+
249
+ expect(res.statusCode).toBe(200);
250
+ const body = JSON.parse(res.body);
251
+ expect(body.ok).toBe(true);
252
+ expect(body.message).toBe("Test passed");
253
+ expect(calledWith).toBeDefined();
254
+ expect(calledWith!.endpoint).toBe("http://jackett:9117/api");
255
+ expect(calledWith!.apiKey).toBe("test-key");
256
+ });
257
+
258
+ it("resolves sentinel secret from stored decrypted value", async () => {
259
+ // First, save a config with a real secret
260
+ cs.update("test-plugin", {
261
+ endpoint: { type: "url", label: "Endpoint", required: true },
262
+ apiKey: { type: "secret", label: "API Key", required: true },
263
+ requestTimeoutMs: { type: "number", label: "Timeout", default: 15000 },
264
+ }, {
265
+ endpoint: "http://jackett:9117/api",
266
+ apiKey: "stored-secret-value",
267
+ });
268
+
269
+ let calledWith: Record<string, unknown> | undefined;
270
+ runtimes.push({
271
+ meta: { id: "test-plugin", version: "1.0.0", starrApiVersion: "^1.0.0" },
272
+ lifecycle: {
273
+ testConnection: async (values: Record<string, unknown>) => {
274
+ calledWith = values;
275
+ return { ok: true, message: "OK" };
276
+ },
277
+ },
278
+ startOrder: 0,
279
+ });
280
+ initConfigRoutes(cs, () => runtimes);
281
+ await app.register(configRoutes);
282
+
283
+ // Now test with the sentinel for apiKey
284
+ const res = await app.inject({
285
+ method: "POST",
286
+ url: "/api/config/test-plugin/test",
287
+ body: {
288
+ endpoint: "http://jackett:9117/api",
289
+ apiKey: SECRET_UNCHANGED_SENTINEL,
290
+ },
291
+ });
292
+
293
+ expect(res.statusCode).toBe(200);
294
+ expect(calledWith).toBeDefined();
295
+ // The sentinel should have been resolved to the stored decrypted value
296
+ expect(calledWith!.apiKey).toBe("stored-secret-value");
297
+ });
298
+
299
+ it("returns 400 when sentinel is used but no stored secret exists", async () => {
300
+ // Don't save any config - just register (which creates defaults with no apiKey set)
301
+ runtimes.push(createMockRuntime("test-plugin", { hasTestConnection: true }));
302
+ initConfigRoutes(cs, () => runtimes);
303
+ await app.register(configRoutes);
304
+
305
+ const res = await app.inject({
306
+ method: "POST",
307
+ url: "/api/config/test-plugin/test",
308
+ body: {
309
+ endpoint: "http://jackett:9117/api",
310
+ apiKey: SECRET_UNCHANGED_SENTINEL,
311
+ },
312
+ });
313
+
314
+ expect(res.statusCode).toBe(400);
315
+ const body = JSON.parse(res.body);
316
+ expect(body.error).toContain("apiKey");
317
+ });
318
+
319
+ it("catches unexpected errors and returns 500 with sanitized message", async () => {
320
+ runtimes.push({
321
+ meta: { id: "test-plugin", version: "1.0.0", starrApiVersion: "^1.0.0" },
322
+ lifecycle: {
323
+ testConnection: async () => {
324
+ throw new Error("Connection refused to test-key");
325
+ },
326
+ },
327
+ startOrder: 0,
328
+ });
329
+ initConfigRoutes(cs, () => runtimes);
330
+ await app.register(configRoutes);
331
+
332
+ const res = await app.inject({
333
+ method: "POST",
334
+ url: "/api/config/test-plugin/test",
335
+ body: { endpoint: "http://jackett:9117/api", apiKey: "test-key" },
336
+ });
337
+
338
+ expect(res.statusCode).toBe(500);
339
+ const body = JSON.parse(res.body);
340
+ expect(body.ok).toBe(false);
341
+ // The secret value "test-key" should be sanitized (redacted with ***)
342
+ expect(body.message).not.toContain("test-key");
343
+ expect(body.message).toContain("***");
344
+ });
345
+ });