Skip to content

Commit 3363c4d

Browse files
Copilotunixfox
andcommitted
Fix TypeScript type errors with proper error handling and complete mock configs
Co-authored-by: unixfox <[email protected]>
1 parent 783ab03 commit 3363c4d

File tree

3 files changed

+76
-25
lines changed

3 files changed

+76
-25
lines changed

src/lib/helpers/cacheDirectoryCheck.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ export function checkCacheDirectoryPermissions(config: Config): void {
4141
try {
4242
Deno.mkdirSync(youtubeiJsDir, { recursive: true });
4343
} catch (err) {
44+
const errorMessage = err instanceof Error
45+
? err.message
46+
: String(err);
4447
throw new Error(
4548
`Cannot create youtubei.js cache directory '${youtubeiJsDir}'. ` +
46-
`Check directory permissions. Original error: ${err.message}`,
49+
`Check directory permissions. Original error: ${errorMessage}`,
4750
);
4851
}
4952
}
@@ -54,20 +57,24 @@ export function checkCacheDirectoryPermissions(config: Config): void {
5457
Deno.writeTextFileSync(testFile, "test");
5558
Deno.removeSync(testFile);
5659
} catch (err) {
60+
const errorMessage = err instanceof Error
61+
? err.message
62+
: String(err);
5763
throw new Error(
5864
`Cannot write to youtubei.js cache directory '${youtubeiJsDir}'. ` +
5965
`This usually indicates a permission issue. ` +
6066
`Ensure the directory is writable by the application user. ` +
6167
`For Docker containers, check volume mount permissions. ` +
62-
`Original error: ${err.message}`,
68+
`Original error: ${errorMessage}`,
6369
);
6470
}
6571

6672
console.log(
6773
`[INFO] Cache directory '${youtubeiJsDir}' is accessible and writable`,
6874
);
6975
} catch (err) {
70-
console.log(`[ERROR] Cache directory check failed: ${err.message}`);
76+
const errorMessage = err instanceof Error ? err.message : String(err);
77+
console.log(`[ERROR] Cache directory check failed: ${errorMessage}`);
7178
console.log(`[ERROR] Common solutions:`);
7279
console.log(
7380
`[ERROR] - For Docker: Ensure the mounted volume has correct permissions (chmod 777 or appropriate ownership)`,

src/main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ if (args._version_date && args._version_commit) {
2424
try {
2525
checkCacheDirectoryPermissions(config);
2626
} catch (err) {
27-
console.log(`[FATAL] ${err.message}`);
27+
const errorMessage = err instanceof Error ? err.message : String(err);
28+
console.log(`[FATAL] ${errorMessage}`);
2829
Deno.exit(1);
2930
}
3031

src/tests/cacheDirectoryCheck_test.ts

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,58 @@
11
import { checkCacheDirectoryPermissions } from "../lib/helpers/cacheDirectoryCheck.ts";
2+
import type { Config } from "../lib/helpers/config.ts";
3+
4+
function createMockConfig(
5+
cacheConfig: { enabled: boolean; directory: string },
6+
): Config {
7+
return {
8+
server: {
9+
port: 8282,
10+
host: "127.0.0.1",
11+
use_unix_socket: false,
12+
unix_socket_path: "/tmp/invidious-companion.sock",
13+
secret_key: "1234567890123456",
14+
verify_requests: false,
15+
encrypt_query_params: false,
16+
enable_metrics: false,
17+
},
18+
cache: cacheConfig,
19+
networking: {
20+
proxy: null,
21+
fetch: {
22+
timeout_ms: 30000,
23+
retry: {
24+
enabled: false,
25+
times: 1,
26+
initial_debounce: 0,
27+
max_debounce: 0,
28+
backoff_multiplier: 0,
29+
},
30+
},
31+
videoplayback: {
32+
ump: false,
33+
video_fetch_chunk_size_mb: 5,
34+
},
35+
},
36+
jobs: {
37+
youtube_session: {
38+
po_token_enabled: false,
39+
frequency: "*/5 * * * *",
40+
},
41+
},
42+
youtube_session: {
43+
oauth_enabled: false,
44+
cookies: "",
45+
},
46+
};
47+
}
248

349
Deno.test({
450
name: "Cache directory permissions check - disabled cache",
551
fn() {
6-
const config = {
7-
cache: {
8-
enabled: false,
9-
directory: "/nonexistent/path",
10-
},
11-
};
52+
const config = createMockConfig({
53+
enabled: false,
54+
directory: "/nonexistent/path",
55+
});
1256

1357
// Should not throw when cache is disabled
1458
checkCacheDirectoryPermissions(config);
@@ -18,12 +62,10 @@ Deno.test({
1862
Deno.test({
1963
name: "Cache directory permissions check - valid directory",
2064
fn() {
21-
const config = {
22-
cache: {
23-
enabled: true,
24-
directory: "/tmp",
25-
},
26-
};
65+
const config = createMockConfig({
66+
enabled: true,
67+
directory: "/tmp",
68+
});
2769

2870
// Should not throw for /tmp which should be writable
2971
checkCacheDirectoryPermissions(config);
@@ -33,25 +75,26 @@ Deno.test({
3375
Deno.test({
3476
name: "Cache directory permissions check - nonexistent parent directory",
3577
fn() {
36-
const config = {
37-
cache: {
38-
enabled: true,
39-
directory: "/nonexistent/path",
40-
},
41-
};
78+
const config = createMockConfig({
79+
enabled: true,
80+
directory: "/nonexistent/path",
81+
});
4282

4383
// Should throw for nonexistent parent directory
4484
try {
4585
checkCacheDirectoryPermissions(config);
4686
throw new Error("Expected function to throw");
4787
} catch (err) {
48-
if (err.message.includes("Expected function to throw")) {
88+
const errorMessage = err instanceof Error
89+
? err.message
90+
: String(err);
91+
if (errorMessage.includes("Expected function to throw")) {
4992
throw err;
5093
}
5194
// Expected error - check it contains helpful information
52-
if (!err.message.includes("does not exist")) {
95+
if (!errorMessage.includes("does not exist")) {
5396
throw new Error(
54-
`Expected error message to mention directory doesn't exist, got: ${err.message}`,
97+
`Expected error message to mention directory doesn't exist, got: ${errorMessage}`,
5598
);
5699
}
57100
}

0 commit comments

Comments
 (0)