rob-revisited/ide/ui/js/vcs-model.test.mjs

57 lines
3.9 KiB
JavaScript

// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/vcs-model.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { detectHost, hostOf, remoteUrl, serverName, urlProblem, stepSpec, stepProblem, stepNeedsHost, hostLabel } from './vcs-model.js';
test('the host of a remote is recognised', () => {
assert.equal(detectHost('https://github.com/ada/game.git'), 'github');
assert.equal(detectHost('[email protected]:ada/game.git'), 'github');
assert.equal(detectHost('https://git.jumphp.net/ada/game.git'), 'forgejo');
assert.equal(detectHost('https://gitlab.com/ada/game.git'), 'gitlab');
assert.equal(detectHost('ssh://[email protected]/ada/game.git'), 'gitlab');
assert.equal(detectHost('https://bitbucket.org/ada/game.git'), 'bitbucket');
assert.equal(detectHost('https://code.example.org/x.git'), 'url');
assert.equal(hostOf('ssh://[email protected]:2222/a/b.git'), 'code.example.org');
assert.equal(hostLabel('github'), 'GitHub');
});
test('the address of owner/repo on each host', () => {
assert.equal(remoteUrl('github', 'ada', 'game'), 'https://github.com/ada/game.git');
assert.equal(remoteUrl('forgejo', 'ada', 'game.git'), 'https://git.jumphp.net/ada/game.git');
assert.equal(remoteUrl('gitlab', 'team/sub', 'game', 'https://git.example.org/'), 'https://git.example.org/team/sub/game.git');
assert.equal(remoteUrl('gitlab', 'ada', 'game'), 'https://gitlab.com/ada/game.git');
assert.equal(remoteUrl('github', 'ada; rm', 'game'), null);
assert.equal(remoteUrl('url', 'a', 'b'), null);
assert.equal(serverName('Git.Example.org'), 'git.example.org');
assert.equal(serverName(''), 'gitlab.com');
assert.equal(serverName('not a host'), '');
});
test('bad addresses are caught before the backend', () => {
for (const bad of ['', '--upload-pack=x', 'https://a.b/c d', 'hello', 'https://ada:[email protected]/a/b.git']) assert.ok(urlProblem(bad), bad);
for (const good of ['https://github.com/a/b.git', '[email protected]:a/b.git', 'ssh://git@h/a/b.git', '/srv/x.git']) assert.equal(urlProblem(good), null, good);
assert.equal(urlProblem('svn://h/r/trunk', 'svn'), null);
assert.ok(urlProblem('[email protected]:a/b.git', 'svn'));
});
test('the step sends what the backend expects', () => {
assert.deepEqual(stepSpec({}), { mode: 'git' });
assert.deepEqual(stepSpec({ mode: 'none' }), { mode: 'none' });
assert.deepEqual(stepSpec({ mode: 'remote', host: 'github' }), { mode: 'remote', host: 'github', private: true });
assert.deepEqual(stepSpec({ mode: 'remote', host: 'github', private: false }), { mode: 'remote', host: 'github', private: false });
assert.deepEqual(stepSpec({ mode: 'remote', host: 'gitlab', gitlabHost: 'https://git.example.org/' }), { mode: 'remote', host: 'gitlab', private: true, gitlabHost: 'git.example.org' });
assert.deepEqual(stepSpec({ mode: 'remote', host: 'existing', url: ' git@h:a/b.git ' }), { mode: 'remote', host: 'existing', url: 'git@h:a/b.git' });
assert.deepEqual(stepSpec({ mode: 'svn', url: 'svn://h/r' }), { mode: 'svn', url: 'svn://h/r' });
assert.deepEqual(stepSpec({ mode: 'weird' }), { mode: 'git' });
});
test('what is missing in a step, and which host needs a token', () => {
assert.ok(stepProblem(stepSpec({ mode: 'svn', url: '' })));
assert.equal(stepProblem(stepSpec({ mode: 'svn', url: 'https://svn.example.org/r' })), null);
assert.ok(stepProblem(stepSpec({ mode: 'remote', host: 'existing', url: 'nope' })));
assert.equal(stepProblem(stepSpec({ mode: 'remote', host: 'forgejo' })), null);
assert.deepEqual(stepNeedsHost(stepSpec({ mode: 'remote', host: 'forgejo' })), { hostId: 'forgejo', host: 'git.jumphp.net' });
assert.deepEqual(stepNeedsHost(stepSpec({ mode: 'remote', host: 'gitlab', gitlabHost: 'git.example.org' })), { hostId: 'gitlab', host: 'git.example.org' });
assert.equal(stepNeedsHost(stepSpec({ mode: 'git' })), null);
assert.equal(stepNeedsHost(stepSpec({ mode: 'remote', host: 'existing', url: 'git@h:a/b.git' })), null);
});