Skip to content

Commit 448991d

Browse files
ckerrdsanders11
andauthored
refactor: use more production code, fewer mocks in github tests (#1943)
* refactor: extract GIST_MAX_FILE_SIZE / GIST_MAX_FILE_COUNT constants Hoist the gist size/count limits out of `src/main/github.ts` into `src/constants.ts` so tests can reference them by name instead of duplicating the magic numbers. * refactor: STATIC_DIR in main/constants.ts now works in both production and tests * test: add electron app.setPath() mock * refactor: reduce scope of MOCK_GIST_FILES, MOCK_GIST_DATA * refactor: remove magic string from github.spec.ts * test: use more production code, fewer mocks in github.spec.ts * test: stop mocking getTemplate() in github.spec.ts * refactor: minor copyediting * Apply suggestion from @dsanders11 Co-authored-by: David Sanders <dsanders11@ucsbalum.com> * test: safeguard tests against the credentials file location changing * fixup! Apply suggestion from @dsanders11 * fix: unused variable warning * fixup! test: safeguard tests against the credentials file location changing fix: oops, removed a line accidentally --------- Co-authored-by: David Sanders <dsanders11@ucsbalum.com>
1 parent 2061482 commit 448991d

8 files changed

Lines changed: 133 additions & 145 deletions

File tree

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ export const SENTRY_DSN =
22
'https://966a5b01ac8d4941b81e4ebd0ab4c991@sentry.io/1882540';
33

44
export const ELECTRON_DTS = 'electron.d.ts';
5+
6+
// These are the limits GitHub enforces for gist sizes.
7+
// We use these to fail fast locally when creating/updating a new gist.
8+
export const GIST_MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB per file
9+
export const GIST_MAX_FILE_COUNT = 300;

src/main/constants.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import * as path from 'node:path';
22

33
import { app } from 'electron';
44

5-
export const STATIC_DIR = path.resolve(__dirname, '../static');
5+
// Find the root dir for static assets (eg `show-me/`, `electron-quick-start/`).
6+
// In production, the bundled main script lives in `.webpack/main/` and webpack
7+
// copies static assets to `.webpack/static/`.
8+
// In tests (vitest loads the source TypeScript directly), `__dirname` is
9+
// `src/main/` and the static folder lives at the repository root.
10+
export const STATIC_DIR = path.resolve(
11+
__dirname,
12+
process.env.VITEST ? '../../static' : '../static',
13+
);
614

715
export const ELECTRON_DOWNLOAD_PATH = path.join(
816
app.getPath('userData'),

src/main/github.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IpcMainInvokeEvent, app, safeStorage } from 'electron';
66

77
import { getTemplate } from './content';
88
import { ipcMainManager } from './ipc';
9+
import { GIST_MAX_FILE_COUNT, GIST_MAX_FILE_SIZE } from '../constants';
910
import { EditorValues, GistLoadResult, GistRevision } from '../interfaces';
1011
import { IpcEvents } from '../ipc-events';
1112
import { isSupportedFile } from '../utils/editor-utils';
@@ -25,10 +26,6 @@ const SHA_PATTERN = /^[0-9a-f]{40}$/;
2526

2627
const MAX_DESCRIPTION_LENGTH = 256;
2728

28-
const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB per file — GitHub's gist limit
29-
30-
const MAX_FILE_COUNT = 300; // GitHub's gist file limit
31-
3229
function isValidToken(token: unknown): token is string {
3330
return typeof token === 'string' && TOKEN_PATTERN.test(token);
3431
}
@@ -62,7 +59,8 @@ function areValidGistFiles(
6259

6360
const entries = Object.entries(files as Record<string, unknown>);
6461

65-
if (entries.length === 0 || entries.length > MAX_FILE_COUNT) return false;
62+
if (entries.length === 0 || entries.length > GIST_MAX_FILE_COUNT)
63+
return false;
6664

6765
for (const [key, value] of entries) {
6866
// null entries are used to delete files during update
@@ -75,7 +73,7 @@ function areValidGistFiles(
7573
if (filename.length === 0) return false;
7674
if (filename !== key) return false;
7775
if (typeof content !== 'string') return false;
78-
if (content.length > MAX_FILE_SIZE) return false;
76+
if (content.length > GIST_MAX_FILE_SIZE) return false;
7977
}
8078

8179
return true;
@@ -437,6 +435,7 @@ export function setupGitHub() {
437435
// Exported for testing
438436
export const testing = {
439437
fetchExample,
438+
getCredentialsPath,
440439
handleGistCreate,
441440
handleGistDelete,
442441
handleGistListCommits,
@@ -445,4 +444,6 @@ export const testing = {
445444
handleTokenCheckAuth,
446445
handleTokenSignIn,
447446
handleTokenSignOut,
447+
loadToken,
448+
saveToken,
448449
};

tests/main/content.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import { getTemplate, getTestTemplate } from '../../src/main/content';
2020
import { isReleasedMajor } from '../../src/main/versions';
2121

2222
vi.unmock('fs-extra');
23-
vi.mock('../../src/main/constants', () => ({
24-
STATIC_DIR: path.join(__dirname, '../../static'),
25-
}));
2623
vi.mock('../../src/main/versions', () => ({
2724
isReleasedMajor: vi.fn(),
2825
}));

0 commit comments

Comments
 (0)