Skip to content

Commit

Permalink
fixes shortlink resolution glitch
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanThatOneKid committed Mar 16, 2024
1 parent a6e0f8e commit d276387
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/lib/public/board/data/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import TEAMS_JSON from './teams.json';

/** TEAMS is a map of internal teams by their name, {@link Team.id}. */
export const TEAMS = [...TEAMS_JSON].reduce(
(tt: { [key: typeof TEAMS_JSON[number]['id']]: Team }, t) => {
(tt: { [key: (typeof TEAMS_JSON)[number]['id']]: Team }, t) => {
tt[t.id] = t;
return tt;
},
Expand Down
120 changes: 51 additions & 69 deletions src/lib/server/links/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,89 @@
import { test, expect } from 'vitest';
import { resolve } from './resolve';

test('resolve resolves shortlinks', () => {
const actual = resolve(new URL('https://acmcsuf.com/github'), {
github: 'https://github.com/ethanthatonekid/acmcsuf.com',
});
const expected = new URL('https://github.com/ethanthatonekid/acmcsuf.com');
expect(actual.href).toStrictEqual(expected.href);
});

test('resolve throws for circularly recursive shortlinks', () => {
expect(() =>
resolve(new URL('https://acmcsuf.com/zig'), { zig: '/zag', zag: '/zig' })
).toThrowError(/too many internal redirects/);
function testCircularShortlinks() {
return resolve(new URL('https://acmcsuf.com/zig'), { zig: '/zag', zag: '/zig' });
}

expect(testCircularShortlinks).toThrowError(/too many internal redirects/);
});

test('resolve properly combines queries', () => {
const out = resolve(new URL('https://acmcsuf.com/example?foo=bar'), {
const actual = resolve(new URL('https://acmcsuf.com/example?foo=bar'), {
example: 'https://example.com?baz=qux',
});
expect(out).toStrictEqual(new URL('https://example.com/?baz=qux&foo=bar'));
const expected = new URL('https://example.com/?baz=qux&foo=bar');
expect(actual.href).toStrictEqual(expected.href);
});

test('resolve properly overwrites hash', () => {
const out = resolve(new URL('https://acmcsuf.com/example#yang'), {
const actual = resolve(new URL('https://acmcsuf.com/example#yang'), {
example: 'https://example.com#yin',
});
expect(out).toStrictEqual(new URL('https://example.com/#yang'));
expect(actual).toStrictEqual(new URL('https://example.com/#yang'));
});

test('resolve overwrites hash (2)', () => {
const out = resolve(new URL('https://acmcsuf.com/one#uno'), {
const actual = resolve(new URL('https://acmcsuf.com/one#uno'), {
one: '/two',
two: '/three#dos',
three: '/example#tres',
example: 'https://example.com',
});
expect(out).toStrictEqual(new URL('https://example.com/#uno'));
const expected = new URL('https://example.com/#uno');
expect(actual.href).toStrictEqual(expected.href);
});

test('resolve appends pathnames', () => {
const out = resolve(new URL('https://acmcsuf.com/example/baz/qux'), {
const actual = resolve(new URL('https://acmcsuf.com/example/baz/qux'), {
example: 'https://example.com/foo/bar',
});
expect(out).toStrictEqual(new URL('https://example.com/foo/bar/baz/qux'));
const expected = new URL('https://example.com/foo/bar/baz/qux');
expect(actual.href).toStrictEqual(expected.href);
});

const TEST_SHORTLINKS = {
github: 'https://github.com/ethanthatonekid/acmcsuf.com',
issues: '/github/issues',
gfi: '/issues/?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22',
bug: '/issues/new/?labels=bug&template=bug_report.md',
'dev/template': 'https://example.com/dev-template',
'student-pack': '/blog/806',
discord: 'https://discord.gg/discord-invite',
'dev-example': 'https://example.com/dev-example',
};
// The shortlink resolver previously would check to see one character at a time if a value was available. Instead, the only candidates are between forward slashes.
test('resolve appends pathnames only if separated by / or end of string', () => {
const shortlinks = { c: 'https://example.com/calendar' };
const actual1 = resolve(new URL('https://acmcsuf.com/colors'), shortlinks);
const expected1 = new URL('https://acmcsuf.com/colors');
expect(actual1.href).toStrictEqual(expected1.href);

const VALID_TESTCASES: Array<[URL, ReturnType<typeof resolve>]> = [
[
new URL('https://acmcsuf.com/github'),
new URL('https://github.com/ethanthatonekid/acmcsuf.com'),
],
[
new URL('https://acmcsuf.com/issues'),
new URL('https://github.com/ethanthatonekid/acmcsuf.com/issues'),
],
[
new URL('https://acmcsuf.com/gfi'),
new URL(
'https://github.com/ethanthatonekid/acmcsuf.com/issues/?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22'
),
],
[
new URL('https://acmcsuf.com/bug'),
new URL(
'https://github.com/ethanthatonekid/acmcsuf.com/issues/new/?labels=bug&template=bug_report.md'
),
],
[
new URL('https://acmcsuf.com/issues/new'),
new URL('https://github.com/ethanthatonekid/acmcsuf.com/issues/new'),
],
[
new URL('https://acmcsuf.com/dev/template/new'),
new URL('https://example.com/dev-template/new'),
],
[new URL('https://acmcsuf.com/discord'), new URL('https://discord.gg/discord-invite')],
[new URL('https://acmcsuf.com/dev-example'), new URL('https://example.com/dev-example')],
];
const actual2 = resolve(new URL('https://acmcsuf.com/c'), shortlinks);
const expected2 = new URL('https://example.com/calendar');
expect(actual2.href).toStrictEqual(expected2.href);
});

test('resolve resolves valid URLs', () => {
for (const [inURL, outURL] of VALID_TESTCASES) {
expect(resolve(inURL, TEST_SHORTLINKS), `failed on ${inURL}`).toStrictEqual(outURL);
}
test('resolve resolves relative paths', () => {
const actual = resolve(new URL('https://acmcsuf.com/student-pack'), {
'student-pack': '/blog/806',
});
const expected = new URL('https://acmcsuf.com/blog/806');
expect(actual.href).toStrictEqual(expected.href);
});

test('resolve resolves alias shortlink', () => {
const inURL = new URL('https://acmcsuf.com/student-pack');
const outURL = new URL('https://acmcsuf.com/blog/806');
expect(resolve(inURL, TEST_SHORTLINKS), `failed on ${inURL}`).toStrictEqual(outURL);
const expected = new URL('https://acmcsuf.com/blog/806');
const input = new URL('https://acmcsuf.com/student-pack');
const actual = resolve(input, { 'student-pack': '/blog/806' });
expect(actual.href).toStrictEqual(expected.href);
});

const INVALID_TESTCASES: Array<URL> = [
new URL('https://acmcsuf.com/doesnotexist'),
new URL('https://acmcsuf.com/does/not/exist'),
new URL('https://acmcsuf.com/<invalid>'),
];

test('resolve returns passed URL if invalid or not found', () => {
for (const inURL of INVALID_TESTCASES) {
expect(resolve(inURL, TEST_SHORTLINKS), `failed on ${inURL}`).toStrictEqual(inURL);
}
[
new URL('https://acmcsuf.com/doesnotexist'),
new URL('https://acmcsuf.com/does/not/exist'),
new URL('https://acmcsuf.com/<invalid>'),
].forEach((input) => {
expect(resolve(input, {}), `failed on ${input}`).toStrictEqual(input);
});
});
7 changes: 6 additions & 1 deletion src/lib/server/links/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ function findURL<ID extends string>(

let id = pathname.slice(1) as ID;
while (id.length > 0 && !shortlinks[id]) {
id = id.slice(0, id.lastIndexOf('/')) as ID;
const slashIndex = id.lastIndexOf('/');
if (slashIndex === -1) {
break;
}

id = id.slice(0, slashIndex) as ID;
}

if (!shortlinks[id]) {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/(site)/genuary/collage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const VIEW_TYPES = ['normal', 'wide', 'tall', 'big'] as const;
/**
* ViewType is a type for the view type of a piece.
*/
export type ViewType = typeof VIEW_TYPES[number];
export type ViewType = (typeof VIEW_TYPES)[number];

/**
* CollagePiece is a piece on the Genuary page.
Expand Down

0 comments on commit d276387

Please sign in to comment.