'use strict'; // The rules both editors keep, against the cases in tests/rules/ (the Neovim plugin reads the // same files in nvim/tests/rules.lua). A rule changed here fails until the other editor follows. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { compareVersions } = require('../src/words'); const priv = require('../src/private'); const { serverAddress } = require('../src/messages'); const login = require('../src/login'); const { Rpc } = require('../src/rpc'); const media = require('../src/media'); const cases = (name) => JSON.parse(fs.readFileSync(path.join(__dirname, '..', '..', 'tests', 'rules', name + '.json'), 'utf8')); test('rules: which of two versions is newer, and what is no version', () => { const v = cases('versions'); for (const [a, b, want] of v.compare) { assert.equal(compareVersions(a, b), want, `${a} against ${b}`); assert.equal(compareVersions(b, a), want === 0 ? 0 : -want, `${b} against ${a}`); } for (const s of v.not_versions) { assert.equal(compareVersions(s, '1.0.0'), null, JSON.stringify(s)); assert.equal(compareVersions('1.0.0', s), null, JSON.stringify(s)); } }); test('rules: which files look like they hold secrets, and what is inside a folder', () => { const f = cases('private_files'); for (const p of f.secret) assert.equal(priv.looksSecret(p), true, p); for (const p of f.plain) assert.equal(priv.looksSecret(p), false, p); for (const [p, roots, want] of f.inside) assert.equal(priv.inside(p, roots), want, `${p} inside ${JSON.stringify(roots)}`); }); test("rules: what may be handed to `lembas login` as the server's address", () => { const a = cases('server_address'); for (const s of a.ok) assert.equal(serverAddress(s), s.trim(), JSON.stringify(s)); for (const s of a.refused) assert.equal(serverAddress(s), '', JSON.stringify(s)); }); test("rules: the sign-in's events, in whatever pieces they arrive", () => { cases('login_events').forEach((c, i) => { const got = []; const feed = login.reader((ev) => got.push(ev.event)); for (const chunk of c.chunks) feed(chunk); assert.deepEqual(got, c.events, 'case ' + (i + 1)); }); }); test('rules: a line longer than the reader keeps is dropped whole, and nothing after it', () => { const r = cases('rpc_lines'); r.cases.forEach((c, i) => { const got = []; const rpc = new Rpc({ write: () => {}, maxLine: r.max, onNotify: (m) => got.push(m) }); for (const chunk of c.chunks) rpc.feed(chunk); assert.deepEqual(got, c.methods, 'case ' + (i + 1)); }); }); test('rules: which shown files may be opened, by their first bytes and their name', () => { for (const c of cases('openable')) { const o = media.openable(Buffer.from(c.head, 'hex'), c.name); assert.equal(o ? o.kind : null, c.kind, c.name); } }); test('private: a file goes along only inside the project, not secret, and known not to be ignored', async () => { const asked = []; const ignored = new priv.Ignored({ run: async (dir, file) => { asked.push([dir, file]); return file.endsWith('build.log'); }, }); const roots = ['/home/sam/app']; // Not known yet: left out, and the question is on its way. assert.equal(priv.maySend('/home/sam/app/main.py', roots, ignored), false); assert.equal(await ignored.check('/home/sam/app/main.py'), false); assert.equal(priv.maySend('/home/sam/app/main.py', roots, ignored), true); assert.equal(await ignored.check('/home/sam/app/build.log'), true); assert.equal(priv.maySend('/home/sam/app/build.log', roots, ignored), false); // Asked once a file, in the file's own folder. await ignored.check('/home/sam/app/main.py'); assert.deepEqual(asked, [['/home/sam/app', '/home/sam/app/main.py'], ['/home/sam/app', '/home/sam/app/build.log']]); // Outside the project, or secret by its name: never, and git is not even asked. assert.equal(priv.maySend('/home/sam/other/main.py', roots, ignored), false); assert.equal(priv.maySend('/home/sam/app/.env', roots, ignored), false); assert.equal(priv.maySend('/home/sam/app/main.py', [], ignored), false); assert.equal(asked.length, 2); // A .gitignore that changed: asked again. ignored.forget(); assert.equal(ignored.known('/home/sam/app/main.py'), undefined); // git that fails (not installed, no repository): nothing is ignored. const none = new priv.Ignored({ run: () => Promise.reject(new Error('no git')) }); assert.equal(await none.check('/home/sam/app/main.py'), false); }); test('private: git itself is asked, in a real repository', async (t) => { const os = require('node:os'); const { execFileSync } = require('node:child_process'); const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'llembas-private-')); t.after(() => fs.rmSync(dir, { recursive: true, force: true })); try { execFileSync('git', ['init', '-q', dir], { stdio: 'ignore' }); } catch { return t.skip('git is not installed'); } fs.writeFileSync(path.join(dir, '.gitignore'), 'local.yml\nout/\n'); fs.mkdirSync(path.join(dir, 'out')); for (const f of ['local.yml', 'main.py', 'out/report.txt']) fs.writeFileSync(path.join(dir, f), 'x\n'); const ignored = new priv.Ignored(); assert.equal(await ignored.check(path.join(dir, 'local.yml')), true); assert.equal(await ignored.check(path.join(dir, 'out', 'report.txt')), true); assert.equal(await ignored.check(path.join(dir, 'main.py')), false); // No repository there: nothing is ignored. const bare = fs.mkdtempSync(path.join(os.tmpdir(), 'llembas-private-')); t.after(() => fs.rmSync(bare, { recursive: true, force: true })); fs.writeFileSync(path.join(bare, 'a.txt'), 'x\n'); assert.equal(await new priv.Ignored().check(path.join(bare, 'a.txt')), false); });