fancive_obsidian-parallel-r.../tests/test-setup.js
wujunchen 33371cadd6 chore: integrate obsidianmd eslint plugin and fix recommended rules
Add eslint v9 + @typescript-eslint/parser + eslint-plugin-obsidianmd as
a dedicated `npm run lint:obsidian` track, separate from the existing
biome lint. Apply autofixes from the recommended config and resolve the
follow-on type/test issues.

- prefer-create-el: createEl('div'/'span') -> createDiv/createSpan
- prefer-active-window-timers: setTimeout/clearTimeout -> activeWindow.*
  + retype timer holders from ReturnType<typeof setTimeout> to number
- prefer-active-doc: drop globalThis.fetch in streaming
- no-useless-catch: drop no-op rethrow wrapper
- no-restricted-globals: streaming.ts is exempted because requestUrl
  does not support streaming responses
- @typescript-eslint/no-unsafe-*: disabled (out of scope for an Obsidian
  plugin lint pass; tsc + biome already cover type safety)
- tests: polyfill globalThis.activeWindow so unit tests still run in Node

Change-Id: I43cc652e29a66d490e2834e940843c39b211b80b
2026-05-03 10:45:10 +08:00

75 lines
1.9 KiB
JavaScript

const assert = require('assert');
const esbuild = require('esbuild');
const fs = require('fs');
const os = require('os');
const path = require('path');
const { EventEmitter } = require('events');
// Polyfill Obsidian's `activeWindow` global for Node test runtime.
if (typeof globalThis.activeWindow === 'undefined') {
globalThis.activeWindow = globalThis;
}
// Load obsidian mock first — hooks Module._load so that require('obsidian')
// returns the mock for both us and the esbuild-bundled test-exports module.
const { getRequestUrlMock, setRequestUrlMock } = require('./obsidian-mock');
// Bundle test-exports.ts synchronously with obsidian marked as external.
// The bundled code will require('obsidian') at runtime, hitting our mock.
const repoRoot = path.join(__dirname, '..');
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'parallel-reader-test-setup-'));
const outfile = path.join(tempDir, 'test-exports.cjs');
esbuild.buildSync({
entryPoints: [path.join(repoRoot, 'src/test-exports.ts')],
bundle: true,
platform: 'node',
format: 'cjs',
outfile,
external: ['obsidian'],
});
const t = require(outfile);
// Clean up temp bundle on process exit.
process.on('exit', () => {
try {
fs.rmSync(tempDir, { recursive: true, force: true });
} catch (_) {
// Best-effort cleanup.
}
});
function openAiCardsResponse(cards) {
const json = {
choices: [
{
message: {
content: JSON.stringify({ cards }),
},
},
],
};
return { status: 200, json, text: JSON.stringify(json) };
}
const baseSettings = {
backend: 'api',
apiProvider: 'openai',
apiFormat: 'openai-chat',
apiBaseUrl: 'https://api.openai.com/v1',
apiAuthType: 'bearer',
apiKey: 'test-key',
apiMaxTokens: 4096,
model: 'openai/gpt-5.1',
};
module.exports = {
assert,
EventEmitter,
t,
baseSettings,
openAiCardsResponse,
getRequestUrlMock,
setRequestUrlMock,
};