import * as path from 'node:path';
import * as fs from 'node:fs';
import { resolveExtractorScript } from '../../../../../src/modules/netaclaw/runtime/weixin_db/key_extractor.js';

describe('resolveExtractorScript', () => {
  const originalEnv = process.env.NETA_WEIXIN_KEY_SCRIPT;
  afterEach(() => {
    if (originalEnv === undefined) delete process.env.NETA_WEIXIN_KEY_SCRIPT;
    else process.env.NETA_WEIXIN_KEY_SCRIPT = originalEnv;
  });

  it('honors NETA_WEIXIN_KEY_SCRIPT env when path exists', () => {
    // 找一个肯定存在的文件做指示 (这里用本测试文件自身)
    const knownExisting = __filename;
    process.env.NETA_WEIXIN_KEY_SCRIPT = knownExisting;
    expect(resolveExtractorScript()).toBe(knownExisting);
  });

  it('falls back to module-relative path (tools/win32/extract-weixin-key.ps1)', () => {
    delete process.env.NETA_WEIXIN_KEY_SCRIPT;
    const resolved = resolveExtractorScript();
    expect(resolved).toMatch(/extract-weixin-key\.ps1$/);
    expect(fs.existsSync(resolved)).toBe(true);
  });
});
