GitNexus/gitnexus/test/unit/lbug-extension-loader.test.ts
Gergő Magyar 21a52af1d4
fix(lbug): ship FTS per-platform and recover in-place native aborts (#3274)
* fix(lbug): pin Ladybug core so Dependabot cannot ship a skewed FTS artifact

The extension version is a separate upstream constant. Ignore daily core bumps and fail the pairing gate when the committed manifest does not name the installed core.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): make doctor and CI FTS gates resolve the packaged artifact

Doctor and the REQUIRE_FTS file gates still treated an empty ~/.lbdb as
unavailable, which would turn three CI jobs red once analyze stops
installing into that tree.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): name native-abort and tuple-missing so analyze cannot mis-advise

The CLI summary's trailing else treated every unknown skip reason as a
missing extension. New crash and platform causes must get their own
remedies, not a network-install hint.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): delete the dead read-path FTS index create

ensureFTSIndex had no production callers and swallowed read-only
CREATE_FTS_INDEX failures, which hid the only signal that a reader
tried to write.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): vendor per-platform FTS artifacts so analyze needs no host install

Keyword search depended on a CDN fetch into ~/.lbdb. Shipping the five
published tuples inside the package makes air-gapped and ignore-scripts
installs load the same artifact the publish gate checksums.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): load the packaged FTS artifact before any network install

Analyze still required a CDN fetch into ~/.lbdb even when the package
already shipped the file. FTS now path-loads the vendored tuple first
and records source labels so a later truncated home copy cannot steal
the diagnosis.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): diagnose a core/extension version skew instead of a missing runtime

A structurally valid FTS artifact whose path version disagrees with the
packaged pin must name both versions, not prescribe VC++ or OpenSSL.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): stamp an FTS phase so repair stays usable after an in-place abort

A native CREATE_FTS_INDEX abort leaves no skip reason; the next run infers
it from the dirty flag, and --repair-fts must not treat that phase as a
half-written graph.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): park an in-place FTS crash WAL without wiping the graph

An FTS abort after a successful checkpoint must reopen the live index on
macOS, Windows, and Linux. Staging never parks the live WAL; readers keep
today's large-WAL refusal.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): refuse read-only opens of an FTS-poisoned WAL

MCP and serve cannot repair a leftover in-place abort. Fail before the
native open and name --repair-fts, on macOS, Windows, and Linux.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): name a vendor-neutral Windows OpenSSL prerequisite

OQ1 is unanswered here so GitNexus does not ship OpenSSL DLLs. Windows
FTS now asks for a system OpenSSL 3 runtime instead of Git Bash PATH.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(lbug): inject the FTS vendor root and redact it on HTTP and MCP

Path-loaded artifacts no longer vary with HOME. Tests pass an injected
vendor tree and assert search warnings never leak a filesystem path.

Co-authored-by: Cursor <cursoragent@cursor.com>

* docs(lbug): document load-only as the global FTS install default

Analyze still overrides to auto. Packaged per-platform artifacts load
before any network install on macOS, Windows, and Linux.

Co-authored-by: Cursor <cursoragent@cursor.com>

* docs(lbug): format the FTS install-policy README table

Prettier does not run on Markdown in pre-commit, so the U10 table wrap
needs its own formatting commit.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): skip FTS CREATE after a persisted native abort

A recovered analyze run was retrying CREATE_FTS_INDEX from skipReason
alone. Keep that skip until --repair-fts, fail closed on unsupported
tuples, and honor the checkpoint warrant for park/repair.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): honor checkpoint flushed warrant and align FTS tests with packaged vendor

A no-op CHECKPOINT must not satisfy the FTS park warrant, and CI still asserted HOME-only FTS isolation after analyze started path-LOADing the packaged artifact.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(lbug): accept a nonempty incremental write set in the #2790 recovery check

FTS-phase recovery can incremental-add files (changed=0, added=1). That is not the #2790 empty-diff wipe skip.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): compare FTS home versions to the core pin and tighten the publish filename gate

Ladybug's ~/.lbdb/extension directory is the runtime/core version; treating it as the artifact version false-diagnosed skew. The publish guard now rejects a path-escaping filename the same way the fetch script does.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(lbug): seed FTS e2e fixtures from the packaged vendor artifact

A machine with no ~/.lbdb copy should still run the vendor-survivorship cases; the seed no longer depends on HOME or a network install.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback (#3274)

Keep in-place FTS abort evidence after persist so a second CREATE abort
cannot fail-open readers, and close the CLI, loader, embed, and e2e gaps
the review called out.

Note: full npm test hit Ladybug worker-pool startup failures under memory
pressure; tsc and 180 targeted unit tests passed.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback (#3274)

Run the vendored-path symlink guard on the OS matrix, put e2e HOME
fixtures on Ladybug's real extension layout, pin the embed crash-WAL
gate before the writable open, and let analyze writers park through
missing-shadow recovery.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(lbug): keep --repair-fts CI green after vendored-first FTS

Never-installed warning fixtures must not inspect a packaged vendor binary, and a failed dirty restamp must not abort an otherwise successful --repair-fts run.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(cli): give the #1169 analyze e2e the same 90s Windows budget as its sibling

The first #1169 persist-meta case was still on a 60s spawn/it budget and was killed banner-only on windows-latest after the FTS warning fixture no longer failed the shard first.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(ci): reweight Windows shards after the FTS e2e grew

Vendored-first HOME fixtures pushed fts-extension-e2e to ~6 minutes on windows-latest, so the old 146s weight packed it with skills-e2e and blew the 20-minute watchdog.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-14 08:52:24 +01:00

690 lines
26 KiB
TypeScript

import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterAll, describe, expect, it, vi } from 'vitest';
import { escapeCypherString } from '../../src/core/lbug/cypher-escape.js';
import {
ExtensionManager,
getExtensionInstallChildProcessArgs,
getExtensionInstallPolicy,
getExtensionInstallTimeoutMs,
type ExtensionInstallResult,
} from '../../src/core/lbug/extension-loader.js';
import { diagnoseExtensionLoad } from '../../src/core/lbug/extension-load-error.js';
const emptyVendor = mkdtempSync(path.join(tmpdir(), 'gn-fts-empty-vendor-'));
const noVendored = { vendorRoot: emptyVendor };
afterAll(() => {
rmSync(emptyVendor, { recursive: true, force: true });
});
const okInstall: ExtensionInstallResult = {
success: true,
timedOut: false,
message: 'installed',
};
const failedInstall: ExtensionInstallResult = {
success: false,
timedOut: false,
message: 'install failed',
};
const timedOutInstall: ExtensionInstallResult = {
success: false,
timedOut: true,
message: 'INSTALL vector timed out after 10ms',
};
const noopWarn = (): void => {};
describe('ExtensionManager — LOAD-first behavior', () => {
it('uses LOAD only and never invokes INSTALL when the extension is already available', async () => {
const installExtension = vi.fn();
const manager = new ExtensionManager({ policy: 'auto', installExtension });
const query = vi.fn().mockResolvedValue({});
await expect(manager.ensure(query, 'fts', 'FTS', noVendored)).resolves.toBe(true);
expect(query.mock.calls.map(([sql]) => sql)).toEqual(['LOAD EXTENSION fts']);
expect(installExtension).not.toHaveBeenCalled();
expect(manager.getCapabilities()).toEqual([{ name: 'fts', loaded: true }]);
});
it('treats "already loaded" load errors as success', async () => {
const installExtension = vi.fn();
const manager = new ExtensionManager({ policy: 'auto', installExtension });
const query = vi.fn().mockRejectedValue(new Error('Extension fts is already loaded'));
await expect(manager.ensure(query, 'fts', 'FTS', noVendored)).resolves.toBe(true);
expect(installExtension).not.toHaveBeenCalled();
});
});
describe('ExtensionManager — install policies', () => {
it('runs bounded out-of-process INSTALL and retries LOAD when policy=auto', async () => {
const installExtension = vi.fn().mockResolvedValue(okInstall);
const manager = new ExtensionManager({ policy: 'auto', installExtension });
const query = vi
.fn()
.mockRejectedValueOnce(new Error('Extension "fts" not found'))
.mockResolvedValueOnce({});
await expect(
manager.ensure(query, 'fts', 'FTS', { ...noVendored, installTimeoutMs: 1234 }),
).resolves.toBe(true);
// The LOAD failure reason is threaded to the installer so it can pick
// INSTALL vs FORCE INSTALL from the error class (#2374, PR #2375).
expect(installExtension).toHaveBeenCalledWith('fts', 1234, 'Extension "fts" not found');
expect(query.mock.calls.map(([sql]) => sql)).toEqual([
'LOAD EXTENSION fts',
'LOAD EXTENSION fts',
]);
expect(query.mock.calls.some(([sql]) => String(sql).startsWith('INSTALL '))).toBe(false);
});
it('skips INSTALL and warns when policy=load-only', async () => {
const installExtension = vi.fn();
const warn = vi.fn();
const manager = new ExtensionManager({ policy: 'load-only', installExtension, warn });
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
await expect(manager.ensure(query, 'fts', 'FTS', noVendored)).resolves.toBe(false);
expect(installExtension).not.toHaveBeenCalled();
expect(warn).toHaveBeenCalledWith(expect.stringContaining('continuing without FTS features'));
expect(manager.getCapabilities()).toMatchObject([
{ name: 'fts', loaded: false, reason: expect.stringContaining('load-only') },
]);
});
it('short-circuits LOAD and INSTALL when policy=never', async () => {
const installExtension = vi.fn();
const warn = vi.fn();
const manager = new ExtensionManager({ policy: 'never', installExtension, warn });
const query = vi.fn();
await expect(manager.ensure(query, 'vector', 'VECTOR')).resolves.toBe(false);
expect(query).not.toHaveBeenCalled();
expect(installExtension).not.toHaveBeenCalled();
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('continuing without VECTOR features'),
);
});
it('per-call options override manager defaults', async () => {
const installExtension = vi.fn().mockResolvedValue(okInstall);
const manager = new ExtensionManager({
policy: 'auto',
installExtension,
warn: noopWarn,
});
const query = vi.fn().mockRejectedValue(new Error('Extension "vector" not found'));
await expect(manager.ensure(query, 'vector', 'VECTOR', { policy: 'load-only' })).resolves.toBe(
false,
);
expect(installExtension).not.toHaveBeenCalled();
});
it('returns false and warns when bounded install times out', async () => {
const installExtension = vi.fn().mockResolvedValue(timedOutInstall);
const warn = vi.fn();
const manager = new ExtensionManager({ policy: 'auto', installExtension, warn });
const query = vi.fn().mockRejectedValue(new Error('Extension "vector" not found'));
await expect(manager.ensure(query, 'vector', 'VECTOR', { installTimeoutMs: 10 })).resolves.toBe(
false,
);
expect(query.mock.calls.map(([sql]) => sql)).toEqual(['LOAD EXTENSION vector']);
expect(warn).toHaveBeenCalledWith(
expect.stringContaining('continuing without VECTOR features'),
);
});
});
describe('ExtensionManager — reason strings carry the real LOAD error (#2374)', () => {
it('load-only failure reason includes the underlying LadybugDB error, collapsed to one line', async () => {
const warn = vi.fn();
const manager = new ExtensionManager({ policy: 'load-only', warn });
const query = vi
.fn()
.mockRejectedValue(
new Error(
'IO exception: Failed to load library: /x/libfts.lbug_extension.\ninvalid ELF header',
),
);
await expect(manager.ensure(query, 'fts', 'FTS', noVendored)).resolves.toBe(false);
expect(manager.getCapabilities()).toMatchObject([
{
name: 'fts',
loaded: false,
reason: expect.stringContaining(
'LOAD fts failed: IO exception: Failed to load library: /x/libfts.lbug_extension. invalid ELF header',
),
},
]);
expect(warn).toHaveBeenCalledWith(expect.stringContaining('invalid ELF header'));
});
it('failed-install reason includes both the install message and the original LOAD error', async () => {
const installExtension = vi.fn().mockResolvedValue(failedInstall);
const manager = new ExtensionManager({ policy: 'auto', installExtension, warn: noopWarn });
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
await expect(manager.ensure(query, 'fts', 'FTS', noVendored)).resolves.toBe(false);
expect(manager.getCapabilities()).toMatchObject([
{
name: 'fts',
loaded: false,
reason: 'install failed; LOAD fts had failed: Extension "fts" not found',
},
]);
});
it('post-install LOAD failure reason includes the retry error', async () => {
const installExtension = vi.fn().mockResolvedValue(okInstall);
const manager = new ExtensionManager({ policy: 'auto', installExtension, warn: noopWarn });
const query = vi
.fn()
.mockRejectedValue(new Error('version mismatch: extension built for 0.17.0'));
await expect(manager.ensure(query, 'fts', 'FTS', noVendored)).resolves.toBe(false);
expect(manager.getCapabilities()).toMatchObject([
{
name: 'fts',
loaded: false,
reason:
'LOAD fts failed after successful INSTALL: version mismatch: extension built for 0.17.0',
},
]);
});
});
describe('ExtensionManager — caching', () => {
it('caches install attempt outcome to avoid retrying within the same process', async () => {
const installExtension = vi.fn().mockResolvedValue(timedOutInstall);
const manager = new ExtensionManager({
policy: 'auto',
installExtension,
warn: noopWarn,
});
const query = vi.fn().mockRejectedValue(new Error('Extension "vector" not found'));
await expect(manager.ensure(query, 'vector', 'VECTOR')).resolves.toBe(false);
await expect(manager.ensure(query, 'vector', 'VECTOR')).resolves.toBe(false);
expect(installExtension).toHaveBeenCalledOnce();
});
it('reset() clears capability and install state so install is retried', async () => {
const installExtension = vi.fn().mockResolvedValue(failedInstall);
const manager = new ExtensionManager({
policy: 'auto',
installExtension,
warn: noopWarn,
});
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
await manager.ensure(query, 'fts', 'FTS', noVendored);
expect(manager.getCapabilities()).toHaveLength(1);
manager.reset();
expect(manager.getCapabilities()).toEqual([]);
await manager.ensure(query, 'fts', 'FTS', noVendored);
expect(installExtension).toHaveBeenCalledTimes(2);
});
});
describe('ExtensionManager — observability', () => {
it('exposes per-extension capability snapshot', async () => {
const manager = new ExtensionManager({ policy: 'load-only', warn: noopWarn });
const okQuery = vi.fn().mockResolvedValue({});
const failQuery = vi.fn().mockRejectedValue(new Error('Extension "vector" not found'));
await manager.ensure(okQuery, 'fts', 'FTS', noVendored);
await manager.ensure(failQuery, 'vector', 'VECTOR');
expect(manager.getCapabilities()).toMatchObject([
{ name: 'fts', loaded: true },
{ name: 'vector', loaded: false, reason: expect.stringContaining('load-only') },
]);
});
it('warns at most once per (extension, reason) pair', async () => {
const installExtension = vi.fn();
const warn = vi.fn();
const manager = new ExtensionManager({ policy: 'load-only', installExtension, warn });
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
await manager.ensure(query, 'fts', 'FTS', noVendored);
await manager.ensure(query, 'fts', 'FTS', noVendored);
expect(warn).toHaveBeenCalledTimes(1);
});
// initLbug's writable FTS pre-load is a speculative probe — on a cold
// machine it misses, then analyze Phase 3 installs and every FTS index builds.
// The probe must not report a degradation that the same run repairs.
it('stays silent on a quiet probe that a later install-capable call repairs', async () => {
const installExtension = vi.fn().mockResolvedValue(okInstall);
const warn = vi.fn();
const manager = new ExtensionManager({ installExtension, warn });
const query = vi
.fn()
.mockRejectedValueOnce(new Error('Extension "fts" not found'))
.mockRejectedValueOnce(new Error('Extension "fts" not found'))
.mockResolvedValueOnce({});
await expect(
manager.ensure(query, 'fts', 'FTS', { ...noVendored, policy: 'load-only', quiet: true }),
).resolves.toBe(false);
expect(warn).not.toHaveBeenCalled();
await expect(
manager.ensure(query, 'fts', 'FTS', { ...noVendored, policy: 'auto' }),
).resolves.toBe(true);
expect(warn).not.toHaveBeenCalled();
expect(manager.getCapabilities()).toEqual([{ name: 'fts', loaded: true }]);
});
it('still warns on a genuine load-only miss that follows a quiet probe of the same reason', async () => {
const warn = vi.fn();
const manager = new ExtensionManager({ policy: 'load-only', warn });
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
await manager.ensure(query, 'fts', 'FTS', { ...noVendored, quiet: true });
await manager.ensure(query, 'fts', 'FTS', noVendored);
expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toHaveBeenCalledWith(expect.stringContaining('continuing without FTS features'));
});
});
describe('ExtensionManager — input validation', () => {
it('rejects extension names that are not bare identifiers', async () => {
const manager = new ExtensionManager({ policy: 'auto' });
const query = vi.fn();
await expect(manager.ensure(query, 'fts; DROP TABLE x', 'FTS')).rejects.toThrow(/Invalid/);
expect(query).not.toHaveBeenCalled();
});
});
describe('installDuckDbExtensionOutOfProcess child process', () => {
it('spawns the stable packaged installer script instead of inline -e code', () => {
const args = getExtensionInstallChildProcessArgs('fts');
expect(args).not.toContain('-e');
expect(args).not.toContain('--input-type=module');
expect(args[0]).toContain('scripts');
expect(args[0]).toContain('install-duckdb-extension.mjs');
expect(args[1]).toBe('fts');
expect(Number(args[2])).toBeGreaterThan(0);
});
it('passes the resolved LadybugDB max DB size to the installer child', () => {
expect(getExtensionInstallChildProcessArgs('fts', 1234).at(-1)).toBe('1234');
});
});
describe('getExtensionInstallPolicy', () => {
it('defaults to load-only when env var is unset', () => {
const original = process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
try {
expect(getExtensionInstallPolicy()).toBe('load-only');
} finally {
if (original === undefined) {
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
} else {
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = original;
}
}
});
it('returns auto when env var is set to auto', () => {
const original = process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = 'auto';
try {
expect(getExtensionInstallPolicy()).toBe('auto');
} finally {
if (original === undefined) {
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
} else {
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = original;
}
}
});
it('returns never when env var is set to never', () => {
const original = process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = 'never';
try {
expect(getExtensionInstallPolicy()).toBe('never');
} finally {
if (original === undefined) {
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
} else {
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = original;
}
}
});
it('falls back to load-only for invalid env var values', () => {
const original = process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = 'bogus';
try {
expect(getExtensionInstallPolicy()).toBe('load-only');
} finally {
if (original === undefined) {
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL;
} else {
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL = original;
}
}
});
});
describe('getExtensionInstallTimeoutMs', () => {
it('reads a positive override from the environment', () => {
const original = process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS;
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS = '42';
try {
expect(getExtensionInstallTimeoutMs()).toBe(42);
} finally {
if (original === undefined) {
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS;
} else {
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS = original;
}
}
});
it('falls back to the default when the env var is missing or invalid', () => {
const original = process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS;
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS;
try {
expect(getExtensionInstallTimeoutMs()).toBe(15_000);
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS = 'notanumber';
expect(getExtensionInstallTimeoutMs()).toBe(15_000);
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS = '0';
expect(getExtensionInstallTimeoutMs()).toBe(15_000);
} finally {
if (original === undefined) {
delete process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS;
} else {
process.env.GITNEXUS_LBUG_EXTENSION_INSTALL_TIMEOUT_MS = original;
}
}
});
});
const buildHostValidBinary = (): Buffer => {
const arm = process.arch === 'arm64';
if (process.platform === 'win32') {
const peOff = 0x80;
const b = Buffer.alloc(peOff + 8);
b[0] = 0x4d;
b[1] = 0x5a;
b.writeUInt32LE(peOff, 0x3c);
b[peOff] = 0x50;
b[peOff + 1] = 0x45;
b.writeUInt16LE(arm ? 0xaa64 : 0x8664, peOff + 4);
return b;
}
if (process.platform === 'darwin') {
const b = Buffer.alloc(32);
b.writeUInt32LE(0xfeedfacf, 0);
b.writeUInt32LE(arm ? 0x0100000c : 0x01000007, 4);
return b;
}
const b = Buffer.alloc(64);
b[0] = 0x7f;
b[1] = 0x45;
b[2] = 0x4c;
b[3] = 0x46;
b[4] = 2;
b[5] = 1;
b.writeUInt16LE(arm ? 0xb7 : 0x3e, 18);
return b;
};
const writeVendorArtifact = (
root: string,
tuple: string,
filename = 'libfts.lbug_extension',
): string => {
const dir = path.join(root, 'lbug-fts', 'prebuilds', tuple);
mkdirSync(dir, { recursive: true });
const artifact = path.join(dir, filename);
writeFileSync(artifact, 'placeholder');
writeFileSync(
path.join(root, 'lbug-fts', 'manifest.json'),
JSON.stringify({
filename,
unsupportedTuples: [{ tuple: 'win32-arm64', reason: 'none' }],
}),
);
return artifact;
};
describe('ExtensionManager — vendored-first FTS (U2)', () => {
const tmpRoots: string[] = [];
const makeRoot = (): string => {
const root = mkdtempSync(path.join(tmpdir(), 'gn-fts-vendor-'));
tmpRoots.push(root);
return root;
};
afterAll(() => {
for (const root of tmpRoots) rmSync(root, { recursive: true, force: true });
});
it('loads a present artifact without spawning an installer child', async () => {
const vendorRoot = makeRoot();
const artifact = writeVendorArtifact(vendorRoot, 'linux-x64');
const installExtension = vi.fn();
const manager = new ExtensionManager({ policy: 'auto', installExtension });
const query = vi.fn().mockResolvedValue({});
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(true);
expect(query).toHaveBeenCalledTimes(1);
expect(query.mock.calls[0][0]).toBe(
`LOAD EXTENSION '${escapeCypherString(realpathSync(artifact))}'`,
);
expect(installExtension).not.toHaveBeenCalled();
expect(JSON.stringify(manager.getCapabilities())).not.toContain(vendorRoot);
});
it('still loads under load-only when the artifact is present', async () => {
const vendorRoot = makeRoot();
writeVendorArtifact(vendorRoot, 'linux-x64');
const installExtension = vi.fn();
const manager = new ExtensionManager({ policy: 'load-only', installExtension });
const query = vi.fn().mockResolvedValue({});
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(true);
expect(installExtension).not.toHaveBeenCalled();
});
it('attempts nothing under never, even with a packaged artifact', async () => {
const vendorRoot = makeRoot();
writeVendorArtifact(vendorRoot, 'linux-x64');
const query = vi.fn();
const installExtension = vi.fn();
const manager = new ExtensionManager({ policy: 'never', installExtension, warn: noopWarn });
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(false);
expect(query).not.toHaveBeenCalled();
expect(installExtension).not.toHaveBeenCalled();
expect(manager.getCapabilities()[0]?.reason).toContain('never');
});
it('fails closed on an unsupported tuple without named LOAD or INSTALL', async () => {
const vendorRoot = makeRoot();
writeVendorArtifact(vendorRoot, 'linux-x64');
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
const installExtension = vi.fn();
const manager = new ExtensionManager({
policy: 'load-only',
installExtension,
warn: noopWarn,
});
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'win32-arm64' }),
).resolves.toBe(false);
expect(query).not.toHaveBeenCalled();
expect(installExtension).not.toHaveBeenCalled();
expect(manager.getCapabilities()[0]?.reason).toContain('win32-arm64');
expect(manager.getCapabilities()[0]?.reason).toContain('no packaged FTS artifact');
expect(manager.getCapabilities()[0]?.reason).not.toContain(vendorRoot);
});
it('does not INSTALL on an unsupported tuple under auto policy', async () => {
const vendorRoot = makeRoot();
writeVendorArtifact(vendorRoot, 'linux-x64');
const query = vi.fn();
const installExtension = vi.fn();
const manager = new ExtensionManager({ policy: 'auto', installExtension, warn: noopWarn });
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'win32-arm64' }),
).resolves.toBe(false);
expect(query).not.toHaveBeenCalled();
expect(installExtension).not.toHaveBeenCalled();
expect(manager.getCapabilities()[0]?.reason).toContain('win32-arm64');
expect(manager.getCapabilities()[0]?.reason).toContain('no packaged FTS artifact');
expect(manager.getCapabilities()[0]?.reason).not.toContain(vendorRoot);
});
it('keeps the vendored inspect path when named LOAD has no .lbug_extension path', async () => {
const vendorRoot = makeRoot();
const artifact = writeVendorArtifact(vendorRoot, 'linux-x64');
writeFileSync(artifact, buildHostValidBinary());
const query = vi
.fn()
.mockRejectedValueOnce(
new Error(
`Failed to load library: ${artifact} which is needed by extension: fts. Error: 126 The specified module could not be found.`,
),
)
.mockRejectedValueOnce(new Error('Extension "fts" has not been installed.'));
const installExtension = vi.fn();
const manager = new ExtensionManager({
policy: 'load-only',
installExtension,
warn: noopWarn,
});
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(false);
expect(installExtension).not.toHaveBeenCalled();
const cap = manager.getCapabilities()[0];
expect(cap?.diagnosis?.kind).toBe('missing_dependency');
expect(cap?.diagnosis?.remedy).toMatch(/OpenSSL|VC\+\+|runtime/i);
});
it('diagnoses a truncated home copy as corrupt, not missing_dependency (KTD7)', async () => {
const vendorRoot = makeRoot();
const artifact = writeVendorArtifact(vendorRoot, 'linux-x64');
const homeCopy = path.join(makeRoot(), 'truncated.lbug_extension');
writeFileSync(homeCopy, 'short');
const query = vi
.fn()
.mockRejectedValueOnce(
new Error(`Failed to load library: ${artifact} which is needed by extension: fts`),
)
.mockRejectedValueOnce(
new Error(
`Failed to load library: ${homeCopy} which is needed by extension: fts. file too short`,
),
);
const manager = new ExtensionManager({
policy: 'auto',
installExtension: vi.fn().mockResolvedValue(failedInstall),
warn: noopWarn,
});
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(false);
const cap = manager.getCapabilities()[0];
expect(cap?.reason).not.toContain(artifact);
expect(cap?.diagnosis?.kind).toBe('corrupt_file');
expect(diagnoseExtensionLoad(cap?.reason, 'FTS', homeCopy).kind).toBe('corrupt_file');
});
it('escapes a vendored path that contains a quote', async () => {
const vendorRoot = mkdtempSync(path.join(tmpdir(), "gn-fts-it's-"));
tmpRoots.push(vendorRoot);
const artifact = writeVendorArtifact(vendorRoot, 'linux-x64');
const query = vi.fn().mockResolvedValue({});
const manager = new ExtensionManager({ policy: 'load-only', warn: noopWarn });
await expect(
manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(true);
expect(query.mock.calls[0][0]).toBe(
`LOAD EXTENSION '${escapeCypherString(realpathSync(artifact))}'`,
);
});
it('rejects a sibling directory that only shares the vendor prefix', async () => {
const parent = makeRoot();
const vendorRoot = path.join(parent, 'vendor');
const evil = path.join(parent, 'vendor-evil', 'lbug-fts', 'prebuilds', 'linux-x64');
mkdirSync(path.join(vendorRoot, 'lbug-fts'), { recursive: true });
mkdirSync(evil, { recursive: true });
writeFileSync(path.join(evil, 'libfts.lbug_extension'), 'evil');
writeFileSync(
path.join(vendorRoot, 'lbug-fts', 'manifest.json'),
JSON.stringify({ filename: 'libfts.lbug_extension' }),
);
mkdirSync(path.join(vendorRoot, 'lbug-fts', 'prebuilds', 'linux-x64'), { recursive: true });
// Candidate must exist so resolveVendoredFtsPath reaches realpath containment
// (a prefix-only leak would follow this symlink into vendor-evil).
symlinkSync(
path.join(evil, 'libfts.lbug_extension'),
path.join(vendorRoot, 'lbug-fts', 'prebuilds', 'linux-x64', 'libfts.lbug_extension'),
);
const query = vi.fn().mockRejectedValue(new Error('Extension "fts" not found'));
const manager = new ExtensionManager({ policy: 'load-only', warn: noopWarn });
await manager.ensure(query, 'fts', 'FTS', { vendorRoot, platformTuple: 'linux-x64' });
expect(query.mock.calls.map(([sql]) => sql)).toEqual(['LOAD EXTENSION fts']);
expect(String(query.mock.calls[0][0])).not.toContain('vendor-evil');
});
it('does not try a vendored path for VECTOR and records no tuple', async () => {
const vendorRoot = makeRoot();
writeVendorArtifact(vendorRoot, 'linux-x64');
const query = vi.fn().mockResolvedValue({});
const manager = new ExtensionManager({ policy: 'auto' });
await expect(
manager.ensure(query, 'vector', 'VECTOR', { vendorRoot, platformTuple: 'linux-x64' }),
).resolves.toBe(true);
expect(query.mock.calls.map(([sql]) => sql)).toEqual(['LOAD EXTENSION vector']);
expect(manager.getCapabilities()[0]?.attempts).toBeUndefined();
});
});