-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add loop detection for patch updates
- Loading branch information
1 parent
69cd3e0
commit 0dffdde
Showing
6 changed files
with
146 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Kysely } from "kysely"; | ||
import type { DB } from "../../src/db/types"; | ||
|
||
export async function seed(db: Kysely<DB>): Promise<void> { | ||
await db | ||
.insertInto("redirects") | ||
.values([ | ||
{ | ||
id: "loop", | ||
destination: "http://localhost:3000/r/loop2", | ||
}, | ||
{ | ||
id: "loop2", | ||
destination: "http://localhost:3000/r/loop", | ||
}, | ||
]) | ||
.execute(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { RedirectsDatasource } from "@/datasources/redirects"; | ||
import type { DB } from "@/db/types"; | ||
import { expect, test, afterEach, mock, beforeEach } from "bun:test"; | ||
import type { Mock } from "bun:test"; | ||
import type { Kysely, Selectable } from "kysely"; | ||
import type { Redirect } from "@/db/types"; | ||
|
||
const mockedRedirects: Record<string, Selectable<Redirect>> = {}; | ||
const mockDb = mock(); | ||
|
||
const redirects = new RedirectsDatasource(mockDb as unknown as Kysely<DB>); | ||
redirects.getRedirect = mock().mockImplementation((id) => { | ||
return mockedRedirects[id]; | ||
}); | ||
|
||
beforeEach(() => { | ||
for (let i = 0; i < 102; i++) { | ||
const id = i.toString(); | ||
mockedRedirects[id] = { | ||
id, | ||
destination: `http://localhost:3000/r/${i + 1}`, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
hits: 0, | ||
pk: i, | ||
deletedAt: null, | ||
}; | ||
} | ||
}); | ||
|
||
afterEach(() => { | ||
Object.keys(mockedRedirects).forEach((key) => { | ||
delete mockedRedirects[key]; | ||
}); | ||
}); | ||
|
||
test("detects loop", async () => { | ||
expect(redirects.detectLoop("5", "http://localhost:3000/r/0")).resolves.toBe( | ||
true, | ||
); | ||
}); | ||
|
||
test.only("loop limit exceeded", async () => { | ||
// Because the beforeEach already created a (non-looping) chain of 101 | ||
// redirects (over the loop detection limit), any attempt to modify the | ||
// existing chain will be detected as a loop. | ||
expect(redirects.detectLoop("0", "http://localhost:3000/r/1")).resolves.toBe( | ||
true, | ||
); | ||
}); |
This file was deleted.
Oops, something went wrong.