Compare commits

...

41 commits

Author SHA1 Message Date
Ayo
1dbd3d662c 1.3.78 2026-05-31 18:48:18 +02:00
Ayo
199f3d1429 fix: links to legacy blog 2026-05-31 18:47:57 +02:00
Ayo
74a3a71090 1.3.77 2026-05-31 18:41:01 +02:00
Ayo
7966746711 feat(pgp): copy & download buttons 2026-05-31 18:40:50 +02:00
Ayo
7e0380a324 1.3.76 2026-05-31 17:04:22 +02:00
Ayo
7921721425 feat(pgp): concise description 2026-05-31 17:04:11 +02:00
Ayo
5fa6e68b37 1.3.75 2026-05-31 17:00:38 +02:00
Ayo
a71eb0f975 feat: add 'encryption' purpose for pgp key 2026-05-31 16:28:29 +02:00
Ayo
087403f70f 1.3.74 2026-05-31 13:11:35 +02:00
Ayo
25a83048d5 feat: remove chat link 2026-05-31 13:11:09 +02:00
Ayo
8f09af3829 1.3.73 2026-05-31 10:08:15 +02:00
Ayo
7ff435d619 feat(pgp): concise desc 2026-05-31 10:07:15 +02:00
Ayo
9d21f0282b 1.3.72 2026-05-31 08:42:01 +02:00
Ayo
5cc77e537b fix(pgp): meta descriptions 2026-05-31 08:41:42 +02:00
Ayo
05dd11db99 chore: patch, build, deploy script shortcut 2026-05-31 08:37:36 +02:00
Ayo
2e4cf74810 1.3.71 2026-05-31 08:34:50 +02:00
Ayo
5140c1fe55 feat(pgp): add title and description 2026-05-31 08:34:37 +02:00
Ayo
d9abb4bd53 1.3.70 2026-05-31 02:46:28 +02:00
Ayo
5104dc3176 feat: indicate pgp key expiry date 2026-05-31 02:44:33 +02:00
Ayo
5d2802aca7 feat: copy-able public key 2026-05-31 02:40:12 +02:00
Ayo
f0bb8f9272 feat: pgp key download link 2026-05-31 02:21:03 +02:00
Ayo
e4081c42e4 1.3.69 2026-05-30 23:04:13 +02:00
Ayo
9e4eb6ef15 feat: add PGP key 2026-05-30 23:03:59 +02:00
Ayo
a6f7c1153c feat: add gpg publickey 2026-05-30 22:05:13 +02:00
Ayo
cea29e7a01 chore: use eslint config helpers 2026-05-28 22:12:53 +02:00
Ayo
eaecdbcf23 chore: add eslint & prettier to workspace extensions 2026-05-28 22:09:52 +02:00
Ayo
9d69a6cc67 chore: add vitest-dom & sw test 2026-05-24 15:15:51 +02:00
Ayo
9b4312b49d test: prep-now 2026-05-23 23:26:32 +02:00
Ayo
bb335badf6 chore: add vitest 2026-05-23 23:25:44 +02:00
Ayo
85f9b148de chore: set allowBuild to false 2026-05-23 23:17:28 +02:00
Ayo
ca81b780ce chore: update pnpm & add allowBuild config 2026-05-23 23:16:48 +02:00
Ayo
0f28734a2d chore: add patch build deploy command to readme 2026-05-20 18:08:15 +02:00
Ayo
df5b89e5b1 chore: format 2026-05-17 11:20:17 +02:00
Ayo
8861d7f73d 1.3.68 2026-05-16 16:09:59 +02:00
Ayo
42112a8a9f feat: responsive now-wrapper 2026-05-16 16:09:18 +02:00
Ayo
08ef1cd749 1.3.67 2026-05-15 20:34:03 +02:00
Ayo
79c31a77d3 chore(deps): update astro-sw to v1 2026-05-15 20:33:18 +02:00
Ayo
4e6554e015 1.3.66 2026-05-15 19:03:30 +02:00
Ayo
88c159c33b fix: double define 2026-05-15 19:03:11 +02:00
Ayo
ec6e450205 1.3.65 2026-05-15 18:57:15 +02:00
Ayo
eaad49a9be feat: use import map for webcomponents; include to sw cache 2026-05-15 18:56:42 +02:00
20 changed files with 1110 additions and 378 deletions

View file

@ -1,4 +1,8 @@
{
"recommendations": ["astro-build.astro-vscode"],
"recommendations": [
"astro-build.astro-vscode",
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
],
"unwantedRecommendations": []
}

View file

@ -28,6 +28,7 @@ $ pnpm i
| `pnpm run build` | generate static files to `dist` directory |
| `pnpm run deploy` | upload to my server |
| `pnpm run prep:now` | back up and clear current `now page` constants |
| `pnpm run patch:build:deploy` | increment version, build, then upload |
## Deployment

View file

@ -2,6 +2,7 @@
import { defineConfig } from 'astro/config'
import serviceWorker from '@ayco/astro-sw'
import sitemap from '@astrojs/sitemap'
import { viteStaticCopy } from 'vite-plugin-static-copy'
import * as data from './package.json'
@ -21,6 +22,10 @@ export default defineConfig({
assetCachePrefix: 'ayco-personal-site',
assetCacheVersionID: data.version,
logAssets: true,
include: [
'/wc/node_modules/web-component-base/dist/index.js',
'/wc/node_modules/@ayo-run/status-indicator/dist/status-indicator.js',
],
esbuild: {
minify: true,
},
@ -38,4 +43,20 @@ export default defineConfig({
},
}),
],
vite: {
plugins: [
viteStaticCopy({
targets: [
{
src: './node_modules/web-component-base/dist/index.js',
dest: 'wc',
},
{
src: './node_modules/@ayo-run/status-indicator/dist/status-indicator.js',
dest: 'wc',
},
],
}),
],
},
})

View file

@ -5,7 +5,7 @@ import tseslint from 'typescript-eslint'
import astroSwGlobals from '@ayco/astro-sw/globals'
import astroParser from 'astro-eslint-parser'
import { includeIgnoreFile } from '@eslint/compat'
import { includeIgnoreFile } from '@eslint/config-helpers'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

View file

@ -1,7 +1,7 @@
{
"name": "@ayco/personal-website",
"type": "module",
"version": "1.3.64",
"version": "1.3.78",
"private": true,
"scripts": {
"astro": "astro",
@ -15,33 +15,36 @@
"check": "npm run format && npm run lint",
"deploy": "eval $(grep '^HOST' .env) && scp -r dist ayo@$HOST:~/ayco.io-flask",
"patch:build:deploy": "npm version patch && npm run build && npm run deploy",
"pbd": "npm run patch:build:deploy",
"build:preview": "npm run build && astro preview",
"build:deploy": "npm run build && npm run deploy",
"copy:dist": "npm run build && cp -R dist ../ayco.io-flask/",
"prepare": "husky",
"prep:now": "npx jiti ./commands/prep-now.js"
"prep:now": "npx jiti ./commands/prep-now.js",
"test": "vitest run ."
},
"devDependencies": {
"@astrojs/sitemap": "^3.7.2",
"@ayco/astro-sw": "^0.10.1",
"@ayco/astro-sw": "^1.0.0",
"@eslint/compat": "^2.1.0",
"@eslint/config-helpers": "^0.6.0",
"@eslint/js": "^10.0.1",
"@iconify-json/mdi": "^1.2.3",
"@iconify-json/simple-icons": "^1.2.81",
"@iconify-json/tabler": "^1.2.34",
"@typescript-eslint/parser": "^8.59.2",
"astro": "^6.3.1",
"@iconify-json/simple-icons": "^1.2.82",
"@iconify-json/tabler": "^1.2.35",
"@typescript-eslint/parser": "^8.59.3",
"astro": "^6.3.3",
"astro-eslint-parser": "^1.4.0",
"astro-github-stats": "^0.8.0",
"astro-icon": "^1.1.5",
"consola": "^3.4.2",
"eslint": "^10.3.0",
"eslint": "^10.4.0",
"eslint-plugin-astro": "^1.7.0",
"eslint-plugin-jsx-a11y": "^6.10.2",
"globals": "^17.6.0",
"husky": "^9.1.7",
"jiti": "^2.7.0",
"lint-staged": "^17.0.2",
"lint-staged": "^17.0.4",
"pathe": "^2.0.3",
"prettier": "^3.8.3",
"prettier-plugin-astro": "^0.14.1",
@ -50,8 +53,11 @@
"remark-rehype": "^11.1.2",
"tslib": "^2.8.1",
"typescript": "^6.0.3",
"typescript-eslint": "^8.59.2",
"unified": "^11.0.5"
"typescript-eslint": "^8.59.3",
"unified": "^11.0.5",
"vite-plugin-static-copy": "^4.1.0",
"vitest": "^4.1.7",
"vitest-dom": "^0.1.1"
},
"lint-staged": {
"*.{js,mjs,astro,ts}": [
@ -62,8 +68,9 @@
"prettier --write"
]
},
"packageManager": "pnpm@10.33.2",
"packageManager": "pnpm@11.2.2",
"dependencies": {
"@ayo-run/status-indicator": "^2.1.2"
"@ayo-run/status-indicator": "^2.1.2",
"web-component-base": "^4.1.2"
}
}

File diff suppressed because it is too large Load diff

4
pnpm-workspace.yaml Normal file
View file

@ -0,0 +1,4 @@
allowBuilds:
esbuild: false
sharp: false
web-component-base: false

13
public/publickey.asc Normal file
View file

@ -0,0 +1,13 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
mDMEahs0NhYJKwYBBAHaRw8BAQdAV2CfblniKxklPgW9eYt2gBl0jMHLwtjrQaY+
BQoWdIa0FkF5byBBeWNvIDxheW9AYXljby5pbz6ImQQTFgoAQRYhBBfxPV6P9zcr
E1RcOGXmv2QVKTxlBQJqGzQ2AhsDBQkB4TOABQsJCAcCAiICBhUKCQgLAgQWAgMB
Ah4HAheAAAoJEGXmv2QVKTxlIeYA/2WLvkDapBbVmGXoACAhqcTN93/CoPyzUbhN
hHE6mmAUAQDHsgCHmh3S/Rn4NRE3Gb41kiPTBWHrlYLTj3Kiw0ASB7g4BGobNDYS
CisGAQQBl1UBBQEBB0C6OEKEIPLaNDOM6JYhvvq0Q4Mk/B1eyZBKF/b4fQvlGgMB
CAeIfgQYFgoAJhYhBBfxPV6P9zcrE1RcOGXmv2QVKTxlBQJqGzQ2AhsMBQkB4TOA
AAoJEGXmv2QVKTxlMSQBAP5ta1kUFp3HAYwcun8qmoiVq1dEJSN1LnI7HlX4ucTl
AP0YLC768PFTBm9CM5T1BE0xjJ7s4dZSrVoI4n8RSe1nCA==
=MTsv
-----END PGP PUBLIC KEY BLOCK-----

View file

@ -1,23 +0,0 @@
---
import '../layouts/variables.css'
---
<a href="https://chat.ayo.run/join/7IKG-h3nW-pD1H">
<status-indicator id="chat-link" pulse status="positive">
Chat
</status-indicator>
</a>
<style>
status-indicator#chat-link {
display: inline-block;
padding: 5px 7px;
border: 1px solid var(--pill-border-color);
border-radius: var(--pill-border-radius);
font-size: var(--font-size-sm);
a {
text-decoration: none;
}
}
</style>

View file

@ -45,7 +45,7 @@ const year = new Date().getFullYear()
<p>
Copyright &#169; 2022-{year}
<a href="/">Ayo Ayco</a>. This website <a
href="https://ayos.blog/stopped-tracking-on-my-sites"
href="https://v1.ayos.blog/stopped-tracking-on-my-sites"
>does not track users</a
>. See the <a href="https://git.ayo.run/ayo/ayco.io-astro">source code.</a>
</p>

View file

@ -56,5 +56,17 @@ const baseURL = Astro.site?.toString().slice(0, -1) // ?? 'https://ayo.ayco.io'
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
<!-- Web Components -->
<script type="module" src="https://esm.sh/@ayo-run/status-indicator"></script>
<script type="importmap">
{
"imports": {
"web-component-base": "/wc/node_modules/web-component-base/dist/index.js",
"status-indicator": "/wc/node_modules/@ayo-run/status-indicator/dist/status-indicator.js"
}
}
</script>
<script type="module">
// importing is enough to register the elements
// eslint-disable-next-line
import StatusIndicator from 'status-indicator'
</script>
</head>

View file

@ -127,10 +127,10 @@ const ogFileType = 'image/png'
}
</style>
<!-- Anonymous RUM for web perf by Cloudflare Web Analytics -->
<script
<!-- <script
defer
src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon='{"token": "a39ad600e67a4db8960c639d2552435c"}'></script>
data-cf-beacon='{"token": "a39ad600e67a4db8960c639d2552435c"}'></script> -->
<!-- End Cloudflare Web Analytics -->
</body>
</html>

View file

@ -26,8 +26,4 @@
--bg-dark: #343a40;
--bg-darker: #212529;
--bg-darkest: #000;
--pill-border-color: rgba(255, 255, 255, 0.2);
--pill-border-radius: 40px;
--pill-padding: 8px 4px;
}

View file

@ -36,8 +36,9 @@ import Footer from '../components/Footer.astro'
<p>
In my spare time, I find it fun building <a href="/showcase">projects</a>,
running self-hosted services at <a href="https://ayo.run">ayo.run</a>, and
volunteering to <a href="https://ayos.blog/why-fediverse/" target="_blank"
>Fediverse</a
volunteering to <a
href="https://v1.ayos.blog/why-fediverse/"
target="_blank">Fediverse</a
> projects like <a href="https://elk.zone">elk.zone</a>,
<a href="https://m.webtoo.ls/public/local">webtoo.ls</a>, and <a
href="https://m.webtoo.ls/@vitest">vitest's fedi presence</a
@ -52,11 +53,9 @@ import Footer from '../components/Footer.astro'
<h2 id="contact">Contact info</h2>
<p>My inbox is open to everyone.</p>
<ul>
<li>✉️ Email me: <a href="mailto:hi@ayo.run">hi@ayo.run</a></li>
<li>
💬 Let's chat: <a href="https://chat.ayo.run/join/7IKG-h3nW-pD1H"
>chat.ayo.run</a
>
✉️ Email me: <a href="mailto:ayo@ayco.io">ayo@ayco.io</a> &middot
<small> (<a href="/pgp">PGP key</a>)</small>
</li>
<li>
💬 Signal: <a
@ -68,11 +67,11 @@ import Footer from '../components/Footer.astro'
</ul>
</main>
<Footer />
</Layout>
<style>
ul {
list-style: none;
padding-left: 0;
}
</style>
<style>
ul {
list-style: none;
padding-left: 0;
}
</style>
</Layout>

View file

@ -5,7 +5,6 @@ import Footer from '../components/Footer.astro'
import now from '../constants/now.json'
import { Picture } from 'astro:assets'
import ChatLink from '../components/ChatLink.astro'
// fetch mastodon account
const response = await fetch(
@ -37,14 +36,11 @@ const avatarSize = 150
<h1 title="Ayo Ayco | Software Engineer + Web Developer">
Hi, I'm <span class="heavy-text">Ayo</span>!
</h1>
<!--a href="https://forms.ayo.run/form/tnz7FybY" class="now-wrapper"-->
<a href="/now" class="now-wrapper pill">
<a href="/now" class="now-wrapper action primary">
<span class="now-label">now</span>
<span class="status">{now.title}</span>
</a>
<ChatLink class="pill" />
</div>
</div>
</section>
@ -54,9 +50,6 @@ const avatarSize = 150
I care about the <em>Web</em>, and I love to <em>create</em> stuff to <em
>inspire</em
> and <em>serve</em> others.
<!--
<a href="/about">More?</a>
-->
</p>
</section>
<section class="cards-section">
@ -90,18 +83,30 @@ const avatarSize = 150
color: white;
}
.pill {
.action {
margin-right: 0.5rem;
}
.now-wrapper {
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 40px;
padding: 8px 4px;
font-weight: normal;
color: white;
font-size: var(--font-size-sm);
text-decoration: none;
font-weight: normal;
&.primary {
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 40px;
}
}
.now-wrapper {
display: inline-block;
max-width: 390px;
white-space: nowrap;
overflow: clip;
text-overflow: ellipsis;
.status {
max-width: 300px;
}
}
.now-label {
@ -201,7 +206,7 @@ const avatarSize = 150
}
@media only screen and (max-device-width: 360px) {
.now-wrapper {
.action.primary {
border: 0px;
border-radius: 0;
font-size: var(--font-size-sm);
@ -232,6 +237,12 @@ const avatarSize = 150
}
@media only screen and (min-device-width: 280px) and (max-device-width: 653px) {
.action {
display: block;
}
.now-wrapper {
max-width: 600px;
}
.highlighted-section__content {
padding: 1rem;
}

View file

@ -37,7 +37,6 @@ publishedOn = publishedOn === '' ? publishDate : publishedOn
</p>
<Fragment set:html={content} />
</main>
<Footer />
</Layout>

124
src/pages/pgp.astro Normal file
View file

@ -0,0 +1,124 @@
---
import Layout from '../layouts/Layout.astro'
import Footer from '../components/Footer.astro'
const title = 'PGP public key'
const description =
'Use to verify my digital signature or to encrypt messages intended only for me.'
---
<Layout title={"Ayo's " + title} description={description}>
<main>
<h1>My {title}</h1>
<p>{description}</p>
<div class="key-block" role="region" aria-labelledby="public-key">
<div class="btn-wrapper">
<button class="copy-btn btn" aria-label="Copy key to clipboard"
>Copy</button
>
<a href="/publickey.asc" class="btn">Download</a>
</div>
<pre
id="public-key"><code>
-----BEGIN PGP PUBLIC KEY BLOCK-----
mDMEahs0NhYJKwYBBAHaRw8BAQdAV2CfblniKxklPgW9eYt2gBl0jMHLwtjrQaY+
BQoWdIa0FkF5byBBeWNvIDxheW9AYXljby5pbz6ImQQTFgoAQRYhBBfxPV6P9zcr
E1RcOGXmv2QVKTxlBQJqGzQ2AhsDBQkB4TOABQsJCAcCAiICBhUKCQgLAgQWAgMB
Ah4HAheAAAoJEGXmv2QVKTxlIeYA/2WLvkDapBbVmGXoACAhqcTN93/CoPyzUbhN
hHE6mmAUAQDHsgCHmh3S/Rn4NRE3Gb41kiPTBWHrlYLTj3Kiw0ASB7g4BGobNDYS
CisGAQQBl1UBBQEBB0C6OEKEIPLaNDOM6JYhvvq0Q4Mk/B1eyZBKF/b4fQvlGgMB
CAeIfgQYFgoAJhYhBBfxPV6P9zcrE1RcOGXmv2QVKTxlBQJqGzQ2AhsMBQkB4TOA
AAoJEGXmv2QVKTxlMSQBAP5ta1kUFp3HAYwcun8qmoiVq1dEJSN1LnI7HlX4ucTl
AP0YLC768PFTBm9CM5T1BE0xjJ7s4dZSrVoI4n8RSe1nCA==
=MTsv
-----END PGP PUBLIC KEY BLOCK-----
</code></pre>
</div>
<p>Expiry Date: 2027-05-30</p>
<p>
Fingerprint: <code
>17F1 3D5E 8FF7 372B 1354 5C38 65E6 BF64 1529 3C65</code>
</p>
<p></p>
</main>
<Footer />
</Layout>
<script>
/* Clipboard copy logic */
document.querySelector('.copy-btn').addEventListener('click', async () => {
const keyText = document.querySelector('#public-key code').innerText.trim()
try {
await navigator.clipboard.writeText(keyText)
const btn = document.querySelector('.copy-btn')
const original = btn.textContent
btn.textContent = 'Copied!'
setTimeout(() => {
btn.textContent = original
}, 2000)
} catch (err) {
console.error('Copy failed', err)
alert('Unable to copy the key. Please copy it manually.')
}
})
</script>
<style>
code {
font-size: var(--font-size-sm);
}
.key-block {
position: relative;
background: #272822;
color: #f8f8f2;
font-family: monospace;
font-size: 0.9rem;
line-height: 1.4;
padding: 1rem 1rem 1rem 1.5rem;
border-radius: 4px;
overflow-x: auto;
white-space: pre-wrap;
word-wrap: break-word;
margin: 1em 0;
}
.key-block code {
display: block;
}
/* buttons */
.btn-wrapper {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}
.btn {
display: inline-block;
position: relative;
background: var(--color-brand-blue-1);
color: #fff;
border: none;
border-radius: 4px;
padding: 0.3rem 0.6rem;
font-size: 0.8rem;
cursor: pointer;
opacity: 0.9;
transition: opacity 0.2s;
text-decoration: none;
}
.btn:hover,
.btn:focus {
opacity: 1;
}
.btn:focus {
outline: 2px solid #0056b3;
outline-offset: 2px;
}
</style>

View file

@ -11,8 +11,8 @@ import Card from '../components/Card.astro'
<main>
<h1><span class="text-gradient">Hobby Projects</span></h1>
<p>
See more of my previous projects at <a href="https://ayos.blog"
>my blog</a
See more of my previous projects at <a
href="https://v1.ayos.blog/projects">my blog</a
>.
</p>
<ul>

121
tests/prep-now.test.ts Normal file
View file

@ -0,0 +1,121 @@
import { readFileSync, writeFileSync, copyFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { consola } from 'consola'
import newNow from '../commands/prep-now'
// Mock file system operations
vi.mock('path', () => ({
resolve: vi.fn(),
readFileSync: vi.fn(),
writeFileSync: vi.fn(),
copyFileSync: vi.fn(),
}))
// Mock consola
vi.mock('consola', () => ({
consola: {
box: vi.fn(),
start: vi.fn(),
success: vi.fn(),
fail: vi.fn(),
error: vi.fn(),
},
}))
// Mock mdToHTML
vi.mock('../command/md-to-html', () => ({
mdToHTML: vi.fn(),
}))
describe('prep-now', () => {
// const mockNow = {
// title: 'Test Title',
// description: 'Test Description',
// publishedOn: '2023-01-01',
// publishDate: '2023-01-01',
// publishState: 'draft',
// content: '# Test Content',
// }
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(resolve).mockImplementation((...paths) => paths.join('/'))
vi.mocked(readFileSync).mockReturnValue('template content')
vi.mocked(writeFileSync).mockImplementation(() => {})
vi.mocked(copyFileSync).mockImplementation(() => {})
vi.mocked(consola.box).mockImplementation(() => {})
vi.mocked(consola.start).mockImplementation(() => {})
vi.mocked(consola.success).mockImplementation(() => {})
vi.mocked(consola.fail).mockImplementation(() => {})
vi.mocked(consola.error).mockImplementation(() => {})
})
it('should create a new now page and clear the original files', async () => {
// Mock the mdToHTML function
const { mdToHTML } = await import('../utils/md-to-html')
vi.mocked(mdToHTML).mockResolvedValue('<h1>Test Content</h1>')
// Mock file paths
vi.mocked(resolve)
.mockReturnValueOnce('/src/constants/now.md')
.mockReturnValueOnce('/src/constants/now.json')
.mockReturnValueOnce('/src/pages/now/and-then/posts/2023-01-01.astro')
.mockReturnValueOnce('/src/constants/bkup/2023-01-01.md')
.mockReturnValueOnce('/src/constants/bkup/2023-01-01.json')
// Mock file content
vi.mocked(readFileSync)
.mockReturnValueOnce('template content')
.mockReturnValueOnce('# Test Content')
// Mock file paths
const __filename = '/src/commands/prep-now.ts'
const __dirname = '/src/commands'
vi.mocked(fileURLToPath).mockReturnValue(__filename)
vi.mocked(dirname).mockReturnValue(__dirname)
// Execute the function
await newNow()
// Verify file operations
expect(writeFileSync).toHaveBeenCalledTimes(2)
expect(copyFileSync).toHaveBeenCalledTimes(2)
expect(consola.success).toHaveBeenCalledWith('now.md cleared')
expect(consola.success).toHaveBeenCalledWith(
'You may now update your Now content and props.\n'
)
})
it('should handle errors gracefully', async () => {
// Mock an error
vi.mocked(writeFileSync).mockImplementation(() => {
throw new Error('Write failed')
})
// Mock file paths
vi.mocked(resolve)
.mockReturnValueOnce('/src/constants/now.md')
.mockReturnValueOnce('/src/constants/now.json')
.mockReturnValueOnce('/src/pages/now/and-then/posts/2023-01-01.astro')
.mockReturnValueOnce('/src/constants/bkup/2023-01-01.md')
.mockReturnValueOnce('/src/constants/bkup/2023-01-01.json')
// Mock file content
vi.mocked(readFileSync)
.mockReturnValueOnce('template content')
.mockReturnValueOnce('# Test Content')
// Execute the function
await newNow()
// Verify error handling
expect(consola.fail).toHaveBeenCalledWith(
'Failed to create a new post from Now'
)
expect(consola.error).toHaveBeenCalled()
})
})

91
tests/sw.test.js Normal file
View file

@ -0,0 +1,91 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { install } from 'vitest-dom'
// Install DOM environment for vitest
install()
// Mock the service worker globals
global.caches = {
keys: vi.fn(),
delete: vi.fn(),
open: vi.fn(),
match: vi.fn(),
}
// Mock the global self object
global.self = {
skipWaiting: vi.fn(),
addEventListener: vi.fn(),
}
// Mock the fetch function
global.fetch = vi.fn()
// Import the service worker module
// Note: We need to use dynamic import to avoid module loading issues
let swModule
beforeEach(async () => {
// Clear all mocks
vi.clearAllMocks()
// Mock the module
swModule = await import('../src/sw.mjs')
})
describe('Service Worker', () => {
describe('cleanOldCaches', () => {
it('should delete old caches', async () => {
const cacheName = 'app-v000'
const oldCacheName = 'app-v001'
global.caches.keys.mockResolvedValue([cacheName, oldCacheName])
global.caches.delete.mockResolvedValue(true)
await swModule.cleanOldCaches()
expect(global.caches.delete).toHaveBeenCalledWith(oldCacheName)
})
})
describe('addResourcesToCache', () => {
it('should add resources to cache', async () => {
const resources = ['/index.html', '/style.css']
const cache = { addAll: vi.fn() }
global.caches.open.mockResolvedValue(cache)
await swModule.addResourcesToCache(resources)
expect(cache.addAll).toHaveBeenCalledWith(resources)
})
})
describe('networkFirst', () => {
it('should return network response when available', async () => {
const request = new Request('/test')
const networkResponse = new Response('network content')
global.fetch.mockResolvedValue(networkResponse)
global.caches.open.mockResolvedValue({ put: vi.fn() })
const result = await swModule.networkFirst({ request })
expect(result).toEqual(networkResponse)
})
it('should return cached response when network fails', async () => {
const request = new Request('/test')
const cachedResponse = new Response('cached content')
global.fetch.mockRejectedValue(new Error('Network error'))
global.caches.open.mockResolvedValue({
match: vi.fn().mockResolvedValue(cachedResponse),
})
const result = await swModule.networkFirst({ request })
expect(result).toEqual(cachedResponse)
})
})
})