diff options
| -rw-r--r-- | .github/workflows/ci.yml | 5 | ||||
| -rw-r--r-- | .golangci.yml | 34 | ||||
| -rw-r--r-- | DOCS/testing.md | 48 | ||||
| -rw-r--r-- | Dockerfile | 15 | ||||
| -rw-r--r-- | frontend-vanilla/src/main.ts | 2 | ||||
| -rw-r--r-- | web/dist/v3/assets/index-CijeHHiU.js (renamed from web/dist/v3/assets/index-C5ln65P2.js) | 2 | ||||
| -rw-r--r-- | web/dist/v3/index.html | 2 |
7 files changed, 80 insertions, 28 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ed82c88..054cd1f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,16 +24,13 @@ jobs: mkdir -p web/dist/v3 touch web/dist/v3/dummy - - name: Vet - run: go vet ./... - - name: Lint uses: golangci/golangci-lint-action@v7 with: version: v2.10.1 - name: Test - run: go test -v -race ./... + run: go test -v -race -cover ./... frontend: name: Frontend (Node) diff --git a/.golangci.yml b/.golangci.yml index e8e8364..d3062dc 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,25 +1,31 @@ version: "2" run: tests: true - timeout: 5m - linters: - disable-all: true - disable: - - errcheck + default: none enable: - - staticcheck - govet - ineffassign - - unparam - misspell - + - staticcheck + - unparam + exclusions: + generated: lax + rules: + - linters: + - errcheck + path: .*_test\.go + paths: + - third_party$ + - builtin$ + - examples$ issues: - exclude-use-default: false max-issues-per-linter: 0 max-same-issues: 0 - exclude-rules: - - path: .*_test\.go - linters: - - errcheck - +formatters: + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ diff --git a/DOCS/testing.md b/DOCS/testing.md new file mode 100644 index 0000000..40eb12f --- /dev/null +++ b/DOCS/testing.md @@ -0,0 +1,48 @@ +# Testing Strategy + +This document outlines the testing procedures for Neko, both locally and in Continuous Integration (CI). + +## Local Development + +Developers should run tests locally before pushing changes. The `Makefile` provides several targets for this purpose: + +- `make test`: Runs backend tests with coverage and frontend unit tests. +- `make lint`: Runs `golangci-lint` using the project's configuration. +- `make check`: Runs both linting and tests. +- `make ui-check`: Verifies that the built frontend assets in `web/dist/v3` match the current source code (ensures `make all` was run). +- `make test-race`: Runs backend tests with the race detector enabled. + +### Git Hooks + +It is recommended to use the provided git hooks to ensure tests pass before committing. Run `make install-hooks` to set them up. + +## Continuous Integration (CI) + +Our GitHub Actions workflow (`.github/workflows/ci.yml`) mimics the local environment to ensure consistency. + +### Backend (Go) + +- **Go Version**: 1.24 (or higher as specified). +- **Linting**: Uses `golangci/golangci-lint-action` with `golangci-lint` v2.x. This provides annotations directly in Pull Requests. +- **Tests**: Runs `go test -v -race ./...` to catch concurrency issues. + +### Frontend (Node.js) + +- **Node Version**: 20.x. +- **Tests**: Runs `npm test` in the `frontend-vanilla` directory. + +### Build Consistency + +- **UI Check**: Runs `make ui-vanilla` and checks for diffs in `web/dist/v3`. This ensures that the production assets checked into the repository are up-to-date with the frontend source code. + +### Docker + +- **Docker Build**: Verifies that the `Dockerfile` builds successfully. +- **Integration Test**: Uses `docker compose` to start the service and verifies it responds to HTTP requests on port 8080. + +## Why this structure? + +1. **Consistency**: Use of similar tools (Go, Node, golangci-lint) locally and in CI reduces "works on my machine" issues. +2. **Race Detection**: Always run with `-race` in CI to find subtle bugs that might be missed during quick local tests. +3. **Asset Verification**: Since we check in built assets for ease of deployment, we must verify they are consistent with the source code. +4. **Smoke Testing**: The Docker step ensures the final container is actually functional. @@ -1,9 +1,9 @@ # Stage 1: Frontend Build FROM node:20-slim AS frontend-builder WORKDIR /app/frontend -COPY frontend/package*.json ./ +COPY frontend-vanilla/package*.json ./ RUN npm install -COPY frontend/ ./ +COPY frontend-vanilla/ ./ RUN npm run build # Stage 2: Backend Build @@ -14,12 +14,13 @@ RUN go mod download COPY . . # Copy built frontend assets from Stage 1 -# Ensure the target directory structure matches what embed expects in web/web.go -RUN mkdir -p web/dist/v2 -COPY --from=frontend-builder /app/frontend/dist ./web/dist/v2 +RUN mkdir -p web/dist/v3 +COPY --from=frontend-builder /app/frontend/dist/ ./web/dist/v3/ -# Build the binary -RUN go build -o neko ./cmd/neko +# Build the binary with version flags +ARG VERSION=0.3 +ARG BUILD=docker +RUN go build -ldflags "-X main.Version=${VERSION} -X main.Build=${BUILD}" -o neko ./cmd/neko # Stage 3: Final Image FROM debian:bullseye-slim diff --git a/frontend-vanilla/src/main.ts b/frontend-vanilla/src/main.ts index 901e316..abe454a 100644 --- a/frontend-vanilla/src/main.ts +++ b/frontend-vanilla/src/main.ts @@ -861,7 +861,7 @@ function navigateItems(direction: number) { }); const el = document.querySelector(`.feed-item[data-id="${activeItemId}"]`); - if (el) el.scrollIntoView({ block: 'start', behavior: 'smooth' }); + if (el) el.scrollIntoView({ block: 'start', behavior: 'instant' }); if (!store.items[nextIndex].read) updateItem(activeItemId, { read: true }); } diff --git a/web/dist/v3/assets/index-C5ln65P2.js b/web/dist/v3/assets/index-CijeHHiU.js index b54669f..3e6e0fc 100644 --- a/web/dist/v3/assets/index-C5ln65P2.js +++ b/web/dist/v3/assets/index-CijeHHiU.js @@ -181,4 +181,4 @@ </ul> </section> </div> - `,document.getElementById("theme-options")?.addEventListener("click",i=>{const n=i.target.closest("button");n&&(a.setTheme(n.getAttribute("data-theme")),v())}),document.getElementById("style-theme-options")?.addEventListener("click",i=>{const n=i.target.closest("button");n&&a.setStyleTheme(n.getAttribute("data-style-theme"))}),document.getElementById("heading-font-selector")?.addEventListener("change",i=>{a.setHeadingFontTheme(i.target.value)}),document.getElementById("font-selector")?.addEventListener("change",i=>{a.setFontTheme(i.target.value)}),document.getElementById("add-feed-btn")?.addEventListener("click",async()=>{const i=document.getElementById("new-feed-url"),n=i.value.trim();n&&(await j(n)?(i.value="",alert("Feed added successfully!"),T()):alert("Failed to add feed."))});let t="opml";const s=document.getElementById("import-file");document.querySelectorAll(".import-btn").forEach(i=>{i.addEventListener("click",n=>{t=n.currentTarget.getAttribute("data-format")||"opml",s.click()})}),s?.addEventListener("change",async i=>{const n=i.target.files?.[0];n&&(await Q(n,t)?(alert(`Import (${t}) started! check logs.`),T()):alert("Failed to import.")),s.value=""}),document.querySelectorAll(".delete-feed-btn").forEach(i=>{i.addEventListener("click",async n=>{const o=parseInt(n.target.getAttribute("data-id"));confirm("Delete this feed?")&&(await U(o),await T(),v())})})}async function j(e){try{return(await f("/api/feed",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:e})})).ok}catch(t){return console.error("Failed to add feed",t),!1}}async function Q(e,t){try{const s=new FormData;s.append("file",e),s.append("format",t);const i=document.cookie.split("; ").find(o=>o.startsWith("csrf_token="))?.split("=")[1];return(await fetch("/api/import",{method:"POST",headers:{"X-CSRF-Token":i||""},body:s})).ok}catch(s){return console.error("Failed to import",s),!1}}async function U(e){try{return(await f(`/api/feed/${e}`,{method:"DELETE"})).ok}catch(t){return console.error("Failed to delete feed",t),!1}}async function V(e){const t=a.items.find(s=>s._id===e);t&&p(e,{starred:!t.starred})}async function X(e){if(a.items.find(s=>s._id===e))try{const s=await f(`/api/item/${e}/content`);if(s.ok){const i=await s.json();i.full_content&&p(e,{full_content:i.full_content})}}catch(s){console.error("Failed to fetch full content",s)}}async function p(e,t){try{if((await f(`/api/item/${e}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).ok){const i=a.items.find(n=>n._id===e);if(i){Object.assign(i,t);const n=document.querySelector(`.feed-item[data-id="${e}"]`);if(n){if(t.read!==void 0&&n.classList.toggle("read",t.read),t.starred!==void 0){const o=n.querySelector(".star-btn");o&&(o.classList.toggle("is-starred",t.starred),o.classList.toggle("is-unstarred",!t.starred),o.setAttribute("title",t.starred?"Unstar":"Star"))}t.full_content&&I()}}}}catch(s){console.error("Failed to update item",s)}}async function T(){const e=await f("/api/feed/");if(e.ok){const t=await e.json();a.setFeeds(t)}}async function J(){const e=await f("/api/tag");if(e.ok){const t=await e.json();a.setTags(t)}}async function E(e,t,s=!1){a.setLoading(!0);try{const i=new URLSearchParams;e&&i.append("feed_id",e),t&&i.append("tag",t),a.searchQuery&&i.append("q",a.searchQuery),(a.filter==="starred"||a.filter==="all")&&i.append("read_filter","all"),a.filter==="starred"&&i.append("starred","true"),s&&a.items.length>0&&i.append("max_id",String(a.items[a.items.length-1]._id));const n=await f(`/api/stream?${i.toString()}`);if(n.ok){const o=await n.json();a.setHasMore(o.length>0),a.setItems(o,s)}}finally{a.setLoading(!1)}}async function S(){const e=r.getCurrentRoute();E(e.params.feedId,e.params.tagName,!0)}async function W(){await f("/api/logout",{method:"POST"}),window.location.href="/login/"}function k(){const e=r.getCurrentRoute(),t=e.query.get("filter");a.setFilter(t||"unread");const s=e.query.get("q");if(s!==null&&a.setSearchQuery(s),e.path==="/settings"){v();return}if(e.path==="/feed"&&e.params.feedId){const i=parseInt(e.params.feedId);a.setActiveFeed(i),E(e.params.feedId),document.getElementById("section-feeds")?.classList.remove("collapsed")}else e.path==="/tag"&&e.params.tagName?(a.setActiveTag(e.params.tagName),E(void 0,e.params.tagName),document.getElementById("section-tags")?.classList.remove("collapsed")):(a.setActiveFeed(null),a.setActiveTag(null),E())}window.addEventListener("keydown",e=>{if(!["INPUT","TEXTAREA"].includes(e.target.tagName))switch(e.key){case"j":w(1);break;case"k":w(-1);break;case"r":if(c){const t=a.items.find(s=>s._id===c);t&&p(t._id,{read:!t.read})}break;case"s":if(c){const t=a.items.find(s=>s._id===c);t&&p(t._id,{starred:!t.starred})}break;case"/":e.preventDefault(),document.getElementById("search-input")?.focus();break}});function w(e){if(a.items.length===0)return;const t=a.items.findIndex(i=>i._id===c);let s;if(t===-1?s=e>0?0:a.items.length-1:s=t+e,s>=0&&s<a.items.length){c=a.items[s]._id,document.querySelectorAll(".feed-item").forEach(n=>{const o=parseInt(n.getAttribute("data-id")||"0");n.classList.toggle("selected",o===c)});const i=document.querySelector(`.feed-item[data-id="${c}"]`);i&&i.scrollIntoView({block:"start",behavior:"smooth"}),a.items[s].read||p(c,{read:!0})}}a.on("feeds-updated",$);a.on("tags-updated",F);a.on("active-feed-updated",$);a.on("active-tag-updated",F);a.on("filter-updated",R);a.on("search-updated",()=>{const e=document.getElementById("search-input");e&&e.value!==a.searchQuery&&(e.value=a.searchQuery),k()});a.on("theme-updated",()=>{h||(h=document.querySelector("#app")),h&&(h.className=`theme-${a.theme} font-${a.fontTheme} heading-font-${a.headingFontTheme}`);const e=document.getElementById("sidebar-theme-toggle");e&&(e.textContent=a.theme==="light"?"☽":"☀",e.title=a.theme==="light"?"Switch to dark mode":"Switch to light mode"),r.getCurrentRoute().path==="/settings"&&v()});a.on("sidebar-toggle",()=>{const e=document.querySelector(".layout");e&&(a.sidebarVisible?(e.classList.remove("sidebar-hidden"),e.classList.add("sidebar-visible")):(e.classList.remove("sidebar-visible"),e.classList.add("sidebar-hidden")))});a.on("style-theme-updated",()=>{L(a.styleTheme),document.querySelectorAll(".sidebar-style-btn").forEach(e=>{e.classList.toggle("active",e.getAttribute("data-style-theme")===a.styleTheme)}),r.getCurrentRoute().path==="/settings"&&v()});a.on("items-updated",I);a.on("loading-state-changed",I);r.addEventListener("route-changed",k);window.app={navigate:e=>r.navigate(e)};async function G(){const e=await f("/api/auth");if(!e||e.status===401){window.location.href="/login/";return}D(),L(a.styleTheme),R();try{await Promise.all([T(),J()])}catch(t){console.error("Initial fetch failed",t)}k()}typeof window<"u"&&!window.__VITEST__&&G(); + `,document.getElementById("theme-options")?.addEventListener("click",i=>{const n=i.target.closest("button");n&&(a.setTheme(n.getAttribute("data-theme")),v())}),document.getElementById("style-theme-options")?.addEventListener("click",i=>{const n=i.target.closest("button");n&&a.setStyleTheme(n.getAttribute("data-style-theme"))}),document.getElementById("heading-font-selector")?.addEventListener("change",i=>{a.setHeadingFontTheme(i.target.value)}),document.getElementById("font-selector")?.addEventListener("change",i=>{a.setFontTheme(i.target.value)}),document.getElementById("add-feed-btn")?.addEventListener("click",async()=>{const i=document.getElementById("new-feed-url"),n=i.value.trim();n&&(await j(n)?(i.value="",alert("Feed added successfully!"),T()):alert("Failed to add feed."))});let t="opml";const s=document.getElementById("import-file");document.querySelectorAll(".import-btn").forEach(i=>{i.addEventListener("click",n=>{t=n.currentTarget.getAttribute("data-format")||"opml",s.click()})}),s?.addEventListener("change",async i=>{const n=i.target.files?.[0];n&&(await Q(n,t)?(alert(`Import (${t}) started! check logs.`),T()):alert("Failed to import.")),s.value=""}),document.querySelectorAll(".delete-feed-btn").forEach(i=>{i.addEventListener("click",async n=>{const o=parseInt(n.target.getAttribute("data-id"));confirm("Delete this feed?")&&(await U(o),await T(),v())})})}async function j(e){try{return(await f("/api/feed",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({url:e})})).ok}catch(t){return console.error("Failed to add feed",t),!1}}async function Q(e,t){try{const s=new FormData;s.append("file",e),s.append("format",t);const i=document.cookie.split("; ").find(o=>o.startsWith("csrf_token="))?.split("=")[1];return(await fetch("/api/import",{method:"POST",headers:{"X-CSRF-Token":i||""},body:s})).ok}catch(s){return console.error("Failed to import",s),!1}}async function U(e){try{return(await f(`/api/feed/${e}`,{method:"DELETE"})).ok}catch(t){return console.error("Failed to delete feed",t),!1}}async function V(e){const t=a.items.find(s=>s._id===e);t&&p(e,{starred:!t.starred})}async function X(e){if(a.items.find(s=>s._id===e))try{const s=await f(`/api/item/${e}/content`);if(s.ok){const i=await s.json();i.full_content&&p(e,{full_content:i.full_content})}}catch(s){console.error("Failed to fetch full content",s)}}async function p(e,t){try{if((await f(`/api/item/${e}`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)})).ok){const i=a.items.find(n=>n._id===e);if(i){Object.assign(i,t);const n=document.querySelector(`.feed-item[data-id="${e}"]`);if(n){if(t.read!==void 0&&n.classList.toggle("read",t.read),t.starred!==void 0){const o=n.querySelector(".star-btn");o&&(o.classList.toggle("is-starred",t.starred),o.classList.toggle("is-unstarred",!t.starred),o.setAttribute("title",t.starred?"Unstar":"Star"))}t.full_content&&I()}}}}catch(s){console.error("Failed to update item",s)}}async function T(){const e=await f("/api/feed/");if(e.ok){const t=await e.json();a.setFeeds(t)}}async function J(){const e=await f("/api/tag");if(e.ok){const t=await e.json();a.setTags(t)}}async function E(e,t,s=!1){a.setLoading(!0);try{const i=new URLSearchParams;e&&i.append("feed_id",e),t&&i.append("tag",t),a.searchQuery&&i.append("q",a.searchQuery),(a.filter==="starred"||a.filter==="all")&&i.append("read_filter","all"),a.filter==="starred"&&i.append("starred","true"),s&&a.items.length>0&&i.append("max_id",String(a.items[a.items.length-1]._id));const n=await f(`/api/stream?${i.toString()}`);if(n.ok){const o=await n.json();a.setHasMore(o.length>0),a.setItems(o,s)}}finally{a.setLoading(!1)}}async function S(){const e=r.getCurrentRoute();E(e.params.feedId,e.params.tagName,!0)}async function W(){await f("/api/logout",{method:"POST"}),window.location.href="/login/"}function k(){const e=r.getCurrentRoute(),t=e.query.get("filter");a.setFilter(t||"unread");const s=e.query.get("q");if(s!==null&&a.setSearchQuery(s),e.path==="/settings"){v();return}if(e.path==="/feed"&&e.params.feedId){const i=parseInt(e.params.feedId);a.setActiveFeed(i),E(e.params.feedId),document.getElementById("section-feeds")?.classList.remove("collapsed")}else e.path==="/tag"&&e.params.tagName?(a.setActiveTag(e.params.tagName),E(void 0,e.params.tagName),document.getElementById("section-tags")?.classList.remove("collapsed")):(a.setActiveFeed(null),a.setActiveTag(null),E())}window.addEventListener("keydown",e=>{if(!["INPUT","TEXTAREA"].includes(e.target.tagName))switch(e.key){case"j":w(1);break;case"k":w(-1);break;case"r":if(c){const t=a.items.find(s=>s._id===c);t&&p(t._id,{read:!t.read})}break;case"s":if(c){const t=a.items.find(s=>s._id===c);t&&p(t._id,{starred:!t.starred})}break;case"/":e.preventDefault(),document.getElementById("search-input")?.focus();break}});function w(e){if(a.items.length===0)return;const t=a.items.findIndex(i=>i._id===c);let s;if(t===-1?s=e>0?0:a.items.length-1:s=t+e,s>=0&&s<a.items.length){c=a.items[s]._id,document.querySelectorAll(".feed-item").forEach(n=>{const o=parseInt(n.getAttribute("data-id")||"0");n.classList.toggle("selected",o===c)});const i=document.querySelector(`.feed-item[data-id="${c}"]`);i&&i.scrollIntoView({block:"start",behavior:"instant"}),a.items[s].read||p(c,{read:!0})}}a.on("feeds-updated",$);a.on("tags-updated",F);a.on("active-feed-updated",$);a.on("active-tag-updated",F);a.on("filter-updated",R);a.on("search-updated",()=>{const e=document.getElementById("search-input");e&&e.value!==a.searchQuery&&(e.value=a.searchQuery),k()});a.on("theme-updated",()=>{h||(h=document.querySelector("#app")),h&&(h.className=`theme-${a.theme} font-${a.fontTheme} heading-font-${a.headingFontTheme}`);const e=document.getElementById("sidebar-theme-toggle");e&&(e.textContent=a.theme==="light"?"☽":"☀",e.title=a.theme==="light"?"Switch to dark mode":"Switch to light mode"),r.getCurrentRoute().path==="/settings"&&v()});a.on("sidebar-toggle",()=>{const e=document.querySelector(".layout");e&&(a.sidebarVisible?(e.classList.remove("sidebar-hidden"),e.classList.add("sidebar-visible")):(e.classList.remove("sidebar-visible"),e.classList.add("sidebar-hidden")))});a.on("style-theme-updated",()=>{L(a.styleTheme),document.querySelectorAll(".sidebar-style-btn").forEach(e=>{e.classList.toggle("active",e.getAttribute("data-style-theme")===a.styleTheme)}),r.getCurrentRoute().path==="/settings"&&v()});a.on("items-updated",I);a.on("loading-state-changed",I);r.addEventListener("route-changed",k);window.app={navigate:e=>r.navigate(e)};async function G(){const e=await f("/api/auth");if(!e||e.status===401){window.location.href="/login/";return}D(),L(a.styleTheme),R();try{await Promise.all([T(),J()])}catch(t){console.error("Initial fetch failed",t)}k()}typeof window<"u"&&!window.__VITEST__&&G(); diff --git a/web/dist/v3/index.html b/web/dist/v3/index.html index e0f57ef..de98996 100644 --- a/web/dist/v3/index.html +++ b/web/dist/v3/index.html @@ -5,7 +5,7 @@ <link rel="icon" type="image/svg+xml" href="/vite.svg" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>neko</title> - <script type="module" crossorigin src="/v3/assets/index-C5ln65P2.js"></script> + <script type="module" crossorigin src="/v3/assets/index-CijeHHiU.js"></script> <link rel="stylesheet" crossorigin href="/v3/assets/index-DPslwYc8.css"> </head> <body> |
