Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/helpers/updateAvailable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ Deno.test("#updateAvailableItem", async (t) => {
await t.step(
"it handles when current version is 1.0.0 and latest 1.0.0",
async () => {
const cacheRelease = stub(_internals, "cacheRelease");
const cacheRelease = stub(
_internals,
"cacheRelease",
() => Promise.resolve(),
);

try {
const config = {
Expand All @@ -37,7 +41,11 @@ Deno.test("#updateAvailableItem", async (t) => {
await t.step(
"it handles when current version is 1.0.0 and latest 0.9.0",
async () => {
const cacheRelease = stub(_internals, "cacheRelease");
const cacheRelease = stub(
_internals,
"cacheRelease",
() => Promise.resolve(),
);

try {
const config = {
Expand All @@ -57,7 +65,11 @@ Deno.test("#updateAvailableItem", async (t) => {
await t.step(
"it handles when current version is 1.0.0 and latest 2.0.0",
async () => {
const cacheRelease = stub(_internals, "cacheRelease");
const cacheRelease = stub(
_internals,
"cacheRelease",
() => Promise.resolve(),
);
const builder = { addItem: () => Promise.resolve() };
const buildStub = stub(builder, "addItem");

Expand Down Expand Up @@ -89,11 +101,11 @@ Deno.test("#updateAvailableItem", async (t) => {
);

await t.step("it does check weekly minus 1 second ago", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const fetchAndStore = stub(_internals, "fetchAndStore", () => {
resolve();
return Promise.resolve();
});
const fetchAndStore = stub(
_internals,
"fetchAndStore",
() => Promise.resolve(),
);
const builder = { addItem: () => Promise.resolve() };
const timeEpoch = 1666000000; // in seconds
const time = new FakeTime(timeEpoch * 1000);
Expand All @@ -108,8 +120,6 @@ Deno.test("#updateAvailableItem", async (t) => {
latestVersionLastChecked: lastChecked,
} as Config;
const update = await updateAvailableItem(builder, config);
// Wait for fire-and-forget to complete
await promise;
assertSpyCall(fetchAndStore, 0, {
args: [config, timeEpoch],
});
Expand Down
9 changes: 4 additions & 5 deletions src/helpers/updateAvailable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { fetchData, GhRelease } from "./github.ts";

const workflowRepo = "whomwah/alfred-github-workflow";

export function updateAvailableItem(
export async function updateAvailableItem(
builder: BuilderType,
config: Config,
) {
if (!config.checkForUpdates) return null;
const now = Math.floor(new Date().getTime() / 1000); // Convert to seconds

_internals.cacheRelease(now, config);
await _internals.cacheRelease(now, config);

try {
const isValid = lessOrEqual(
Expand Down Expand Up @@ -49,13 +49,12 @@ async function fetchAndStore(config: Config, now: number) {
storeConfig(config.db, "latestVersionLastChecked", String(now));
}

function cacheRelease(now: number, config: Config) {
async function cacheRelease(now: number, config: Config) {
const invalidateCacheDate = now - TWENTY_FOUR_HOURS * updateFrequency();
const lastChecked = config.latestVersionLastChecked;

if (lastChecked < invalidateCacheDate) {
// Fire-and-forget: don't block the workflow on update check
_internals.fetchAndStore(config, now).catch(console.error);
await _internals.fetchAndStore(config, now).catch(console.error);
}
}

Expand Down