diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..a5f5b035 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,21 @@ +--- +name: Bug report +about: Create a bug report +title: '' +labels: '' +assignees: '' + +--- + +## Environment + +* project (cli, server, browser-extension): +* version: +* operating system: + + +## Description + + +## Steps to reproduce + diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md new file mode 100644 index 00000000..75c8a7b4 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/feature_request.md @@ -0,0 +1,14 @@ +--- +name: Feature request +about: Suggest an idea for a feature + +--- + +* **Please describe the feature you would like to have.** + + +* **What alternatives have you considered?** + + +* **Any additional context** + diff --git a/.github/ISSUE_TEMPLATE/pull_request_template.md b/.github/ISSUE_TEMPLATE/pull_request_template.md new file mode 100644 index 00000000..a0e96a8d --- /dev/null +++ b/.github/ISSUE_TEMPLATE/pull_request_template.md @@ -0,0 +1 @@ +## Short description diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..ac44b4f3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,34 @@ +name: CI +on: + push: + branches: + - master + pull_request: + branches: + - master + +jobs: + build: + runs-on: ubuntu-22.04 + + steps: + - uses: actions/checkout@v5 + - uses: actions/setup-go@v6 + with: + go-version: '>=1.25.0' + + - name: Install dependencies + run: | + make install + + - name: Test cli + run: | + make test-cli + + - name: Test app + run: | + make test-api + + - name: Test e2e + run: | + make test-e2e diff --git a/.github/workflows/release-cli.yml b/.github/workflows/release-cli.yml new file mode 100644 index 00000000..81a2ea5e --- /dev/null +++ b/.github/workflows/release-cli.yml @@ -0,0 +1,77 @@ +name: Release CLI + +on: + push: + tags: + - 'cli-v*' + +jobs: + release: + runs-on: ubuntu-22.04 + permissions: + contents: write + + steps: + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + - uses: actions/setup-go@v6 + with: + go-version: '>=1.25.0' + + - name: Extract version from tag + id: version + run: | + TAG=${GITHUB_REF#refs/tags/cli-v} + echo "version=$TAG" >> $GITHUB_OUTPUT + echo "Releasing version: $TAG" + + - name: Install dependencies + run: make install + + - name: Run CLI tests + run: make test-cli + + - name: Run E2E tests + run: make test-e2e + + - name: Build CLI + run: make version=${{ steps.version.outputs.version }} build-cli + + - name: Generate changelog + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="cli-v${VERSION}" + + # Find previous CLI tag + PREV_TAG=$(git tag --sort=-version:refname | grep "^cli-" | grep -v "^${TAG}$" | head -n 1) + + if [ -z "$PREV_TAG" ]; then + echo "Error: No previous CLI tag found" + echo "This appears to be the first release." + exit 1 + fi + + ./scripts/generate-changelog.sh cli "$TAG" "$PREV_TAG" > /tmp/changelog.txt + cat /tmp/changelog.txt + + - name: Create GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="cli-v${VERSION}" + + # Determine if prerelease (version not matching major.minor.patch) + FLAGS="" + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + FLAGS="--prerelease" + fi + + gh release create "$TAG" \ + build/cli/*.tar.gz \ + build/cli/*_checksums.txt \ + $FLAGS \ + --title="$TAG" \ + --notes-file=/tmp/changelog.txt \ + --draft diff --git a/.github/workflows/release-server.yml b/.github/workflows/release-server.yml new file mode 100644 index 00000000..a5f52933 --- /dev/null +++ b/.github/workflows/release-server.yml @@ -0,0 +1,109 @@ +name: Release Server + +on: + push: + tags: + - 'server-v*' + +jobs: + release: + runs-on: ubuntu-22.04 + permissions: + contents: write + + steps: + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + - uses: actions/setup-go@v6 + with: + go-version: '>=1.25.0' + - uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Extract version from tag + id: version + run: | + TAG=${GITHUB_REF#refs/tags/server-v} + echo "version=$TAG" >> $GITHUB_OUTPUT + echo "Releasing version: $TAG" + + - name: Install dependencies + run: make install + + - name: Run tests + run: make test + + - name: Build server + run: make version=${{ steps.version.outputs.version }} build-server + + - name: Generate changelog + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="server-v${VERSION}" + + # Find previous server tag + PREV_TAG=$(git tag --sort=-version:refname | grep "^server-" | grep -v "^${TAG}$" | head -n 1) + + if [ -z "$PREV_TAG" ]; then + echo "Error: No previous server tag found" + echo "This appears to be the first release." + exit 1 + fi + + ./scripts/generate-changelog.sh server "$TAG" "$PREV_TAG" > /tmp/changelog.txt + cat /tmp/changelog.txt + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Prepare Docker build context + run: | + VERSION="${{ steps.version.outputs.version }}" + cp build/server/dnote_server_${VERSION}_linux_amd64.tar.gz host/docker/ + cp build/server/dnote_server_${VERSION}_linux_arm64.tar.gz host/docker/ + cp build/server/dnote_server_${VERSION}_linux_arm.tar.gz host/docker/ + cp build/server/dnote_server_${VERSION}_linux_386.tar.gz host/docker/ + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKER_USERNAME }} + password: ${{ secrets.DOCKER_TOKEN }} + + - name: Build and push Docker image + uses: docker/build-push-action@v6 + with: + context: ./host/docker + push: true + platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/386 + tags: | + dnote/dnote:${{ steps.version.outputs.version }} + dnote/dnote:latest + build-args: | + version=${{ steps.version.outputs.version }} + + - name: Create GitHub release + env: + GH_TOKEN: ${{ github.token }} + run: | + VERSION="${{ steps.version.outputs.version }}" + TAG="server-v${VERSION}" + + # Determine if prerelease (version not matching major.minor.patch) + FLAGS="" + if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + FLAGS="--prerelease" + fi + + gh release create "$TAG" \ + build/server/*.tar.gz \ + build/server/*_checksums.txt \ + $FLAGS \ + --title="$TAG" \ + --notes-file=/tmp/changelog.txt \ + --draft diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..2847e75d --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +/vendor +/build +.vagrant +*.log +node_modules +/test +tmp +*.db +/server diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 00000000..b0fa33eb --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,68 @@ +# Contributing to Dnote + +Dnote is an open source project. + +* [Setting up](#setting-up) +* [Server](#server) +* [Command Line Interface](#command-line-interface) + +## Setting up + +The CLI and server are single single binary files with SQLite embedded - no databases to install, no containers to run, no VMs required. + +**Prerequisites** + +* Go 1.25+ ([Download](https://go.dev/dl/)) +* Node.js 18+ ([Download](https://nodejs.org/) - only needed for building frontend assets) + +**Quick Start** + +1. Clone the repository +2. Install dependencies: + ```bash + make install + ``` +3. Start developing! Run tests: + ```bash + make test + ``` + Or start the dev server: + ```bash + make dev-server + ``` + +That's it. You're ready to contribute. + +## Server + +```bash +# Start dev server (runs on localhost:3001) +make dev-server + +# Run tests +make test-api + +# Run tests in watch mode +WATCH=true make test-api +``` + +## Command Line Interface + +```bash +# Run tests +make test-cli + +# Build dev version (places in your PATH) +make debug=true build-cli + +# Build production version for all platforms +make version=v0.1.0 build-cli + +# Build for a specific platform +# Note: You cannot cross-compile using this method because Dnote uses CGO +# and requires the OS specific headers. +GOOS=[insert OS] GOARCH=[insert arch] make version=v0.1.0 build-cli + +# Debug mode +DNOTE_DEBUG=1 dnote sync +``` diff --git a/LICENSE b/LICENSE index 283b7c68..6b0b1270 100644 --- a/LICENSE +++ b/LICENSE @@ -1,8 +1,203 @@ -Source code in this repository is variously licensed under the GNU Affero General Public -License v3.0 (GNU AGPLv3), and GNU General Public License v3.0 (GNU GPLv3). A copy of each -license can be found in the licenses directory. The Source code for the cli is licensed under -GNU GPLv3. The source code for the server and the web is licensed under GNU AGPLv3. In all -other cases, source code in a given file is licensed under the GNU AGPLv3. -Unless otherwise noted at the beginning of the file, the copyright belongs to -Monomax Software Pty Ltd. + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..c38819c0 --- /dev/null +++ b/Makefile @@ -0,0 +1,104 @@ +NPM := $(shell command -v npm 2> /dev/null) +GH := $(shell command -v gh 2> /dev/null) + +currentDir = $(shell pwd) +serverOutputDir = ${currentDir}/build/server +cliOutputDir = ${currentDir}/build/cli +cliHomebrewDir = ${currentDir}/../homebrew-dnote + +## installation +install: install-go install-js +.PHONY: install + +install-go: + @echo "==> installing go dependencies" + @go mod download +.PHONY: install-go + +install-js: +ifndef NPM + $(error npm is not installed) +endif + + @echo "==> installing js dependencies" + +ifeq ($(CI), true) + @(cd ${currentDir}/pkg/server/assets && npm ci --cache $(NPM_CACHE_DIR) --prefer-offline --unsafe-perm=true) +else + @(cd ${currentDir}/pkg/server/assets && npm install) +endif +.PHONY: install-js + +## test +test: test-cli test-api test-e2e +.PHONY: test + +test-cli: generate-cli-schema + @echo "==> running CLI test" + @(${currentDir}/scripts/cli/test.sh) +.PHONY: test-cli + +test-api: + @echo "==> running API test" + @(${currentDir}/scripts/server/test-local.sh) +.PHONY: test-api + +test-e2e: + @echo "==> running E2E test" + @(${currentDir}/scripts/e2e/test.sh) +.PHONY: test-e2e + +# development +dev-server: + @echo "==> running dev environment" + @VERSION=master ${currentDir}/scripts/server/dev.sh +.PHONY: dev-server + +build-server: +ifndef version + $(error version is required. Usage: make version=0.1.0 build-server) +endif + + @echo "==> building server assets" + @(cd "${currentDir}/pkg/server/assets/" && ./styles/build.sh) + @(cd "${currentDir}/pkg/server/assets/" && ./js/build.sh) + + @echo "==> building server" + @${currentDir}/scripts/server/build.sh $(version) +.PHONY: build-server + +build-server-docker: build-server +ifndef version + $(error version is required. Usage: make version=0.1.0 [platform=linux/amd64] build-server-docker) +endif + + @echo "==> building Docker image" + @(cd ${currentDir}/host/docker && ./build.sh $(version) $(platform)) +.PHONY: build-server-docker + +generate-cli-schema: + @echo "==> generating CLI database schema" + @mkdir -p pkg/cli/database + @touch pkg/cli/database/schema.sql + @go run -tags fts5 ./pkg/cli/database/schema +.PHONY: generate-cli-schema + +build-cli: generate-cli-schema +ifeq ($(debug), true) + @echo "==> building cli in dev mode" + @${currentDir}/scripts/cli/dev.sh +else + +ifndef version + $(error version is required. Usage: make version=0.1.0 build-cli) +endif + + @echo "==> building cli" + @${currentDir}/scripts/cli/build.sh $(version) +endif +.PHONY: build-cli + +clean: + @git clean -f + @rm -rf build +.PHONY: clean diff --git a/README.md b/README.md index 956936fa..7ab0063f 100644 --- a/README.md +++ b/README.md @@ -1,42 +1,64 @@ -# Dnote + +========================= -A simple, encrypted personal knowledge base. + -## What It Does +Dnote is a simple command line notebook. Single binary, no dependencies. Since 2017. -Instantly capture your microlessons and get automatic reminders for spaced repetition. Because: +Your notes are stored in **one SQLite file** - portable, searchable, and completely under your control. Optional sync between devices via a self-hosted server with REST API access. -* we forget exponentially unless we write down what we learn and come back. -* ideas cannot be grokked unless we can put them down in clear words. +```sh +# Add a note (or omit -c to launch your editor) +dnote add linux -c "Check disk usage with df -h" -## How to Use +# View notes in a book +dnote view linux -Use the following to keep Dnote handy. +# Full-text search +dnote find "disk usage" -- [CLI](https://github.com/dnote/dnote/blob/fix-loading/cli/README.md) -- [Web](https://dnote.io) -- [Browser extension](https://github.com/dnote/browser-extension) -- [Atom](https://github.com/dnote/dnote-atom) +# Sync notes +dnote sync +``` +## Installation -### User Stories +```bash +# Linux, macOS, FreeBSD, Windows +curl -s https://www.getdnote.com/install | sh -- [How I Built a Personal Knowledge Base for Myself](https://dnote.io/blog/how-i-built-personal-knowledge-base-for-myself/) -- [I Wrote Down Everything I Learned While Programming for a Month](https://dnote.io/blog/writing-everything-i-learn-coding-for-a-month/) +# macOS with Homebrew +brew install dnote +``` -## Security +Or [download binary](https://github.com/dnote/dnote/releases). -Dnote is end-to-end encrypted and respects your privacy. It does not track you. +## Server (Optional) -When syncing, your data is encrypted with AES-256. Dnote server has zero knowledge about note contents. +Server is a binary with SQLite embedded. No database setup is required. -## Self-host +If using docker, create a compose.yml: -Instructions are coming soon. +```yaml +services: + dnote: + image: dnote/dnote:latest + container_name: dnote + ports: + - 3001:3001 + volumes: + - ./dnote_data:/data + restart: unless-stopped +``` -## Links +Then run: -- [Dnote](https://dnote.io) -- [Dnote Pro](https://dnote.io/pricing) +```bash +docker-compose up -d +``` -[](https://semaphoreci.com/dnote/dnote-2) +Or see the [guide](https://www.getdnote.com/docs/server/manual) for binary installation. + +## Documentation + +See the [Dnote doc](https://www.getdnote.com/docs). diff --git a/SELF_HOSTING.md b/SELF_HOSTING.md new file mode 100644 index 00000000..e67df891 --- /dev/null +++ b/SELF_HOSTING.md @@ -0,0 +1,43 @@ +# Self-Hosting Dnote Server + +Please see the [doc](https://www.getdnote.com/docs/server) for more. + +## Docker Installation + +1. Install [Docker](https://docs.docker.com/install/). +2. Install Docker [Compose plugin](https://docs.docker.com/compose/install/linux/). +3. Create a `compose.yml` file with the following content: + +```yaml +services: + dnote: + image: dnote/dnote:latest + container_name: dnote + ports: + - 3001:3001 + volumes: + - ./dnote_data:/data + restart: unless-stopped +``` + +4. Run the following to download the image and start the container + +``` +docker compose up -d +``` + +Visit http://localhost:3001 in your browser to see Dnote running. + +## Manual Installation + +Download from [releases](https://github.com/dnote/dnote/releases), extract, and run: + +```bash +tar -xzf dnote-server-$version-$os.tar.gz +mv ./dnote-server /usr/local/bin +dnote-server start --baseUrl=https://your.server +``` + +You're up and running. Database: `~/.local/share/dnote/server.db` (customize with `--dbPath`). Run `dnote-server start --help` for options. + +Set `apiEndpoint: https://your.server/api` in `~/.config/dnote/dnoterc` to connect your CLI to the server. diff --git a/assets/logo.png b/assets/logo.png new file mode 100644 index 00000000..e446301e Binary files /dev/null and b/assets/logo.png differ diff --git a/cli/CONBTRIBUTING.md b/cli/CONBTRIBUTING.md deleted file mode 100644 index ec5f3e4c..00000000 --- a/cli/CONBTRIBUTING.md +++ /dev/null @@ -1,50 +0,0 @@ -# Contributing - -This is a guide for contributors. - -## Set up - -First, download the project - -```sh -go get github.com/dnote/dnote/cli -``` - -Go to the project root and download dependencies using [dep](https://github.com/golang/dep). - -```sh -dep ensure -``` - -## Test - -Run - -```sh -./scripts/test.sh -``` - -## Debug - -Run Dnote with `DNOTE_DEBUG=1` to print debugging statements. - -## Release - -* Build for all target platforms, tag, push tags -* Release on GitHub and [Dnote Homebrew tap](https://github.com/dnote/homebrew-dnote). - -```sh -VERSION=0.4.8 make release -``` - -* Build, without releasing, for all target platforms - -```sh -VERSION=0.4.8 make -``` - -**Note** - -- If a release is not stable, - - disable the homebrew release by commenting out `homebrew` block in `.goreleaser.yml` - - mark release as pre-release on GitHub release diff --git a/cli/Gopkg.lock b/cli/Gopkg.lock deleted file mode 100644 index 4649541f..00000000 --- a/cli/Gopkg.lock +++ /dev/null @@ -1,148 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - digest = "1:cd0089a5b5d872ac1b772087c7ee0ff2e71de50aa3a51826be64a63963a85287" - name = "github.com/dnote/actions" - packages = ["."] - pruneopts = "" - revision = "60e81aff027d39f4494c5ee5c1db9c3efc015ccf" - version = "v0.2.0" - -[[projects]] - digest = "1:e988ed0ca0d81f4d28772760c02ee95084961311291bdfefc1b04617c178b722" - name = "github.com/dnote/color" - packages = ["."] - pruneopts = "" - revision = "5b77d2a35fb0ede96d138fc9a99f5c9b6aef11b4" - version = "v1.7.0" - -[[projects]] - branch = "master" - digest = "1:fe99ddb68e996f2f9f7995e9765bc283ceef12dbe30de17922900c1cfa9dfc09" - name = "github.com/google/go-github" - packages = ["github"] - pruneopts = "" - revision = "b7b480f79db7ae436e87bef80ff47596139af8f2" - -[[projects]] - digest = "1:cea4aa2038169ee558bf507d5ea02c94ca85bcca28a4c7bb99fd59b31e43a686" - name = "github.com/google/go-querystring" - packages = ["query"] - pruneopts = "" - revision = "44c6ddd0a2342c386950e880b658017258da92fc" - version = "v1.0.0" - -[[projects]] - digest = "1:870d441fe217b8e689d7949fef6e43efbc787e50f200cb1e70dbca9204a1d6be" - name = "github.com/inconshreveable/mousetrap" - packages = ["."] - pruneopts = "" - revision = "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75" - version = "v1.0" - -[[projects]] - digest = "1:9ea83adf8e96d6304f394d40436f2eb44c1dc3250d223b74088cc253a6cd0a1c" - name = "github.com/mattn/go-colorable" - packages = ["."] - pruneopts = "" - revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" - version = "v0.0.9" - -[[projects]] - digest = "1:3140e04675a6a91d2a20ea9d10bdadf6072085502e6def6768361260aee4b967" - name = "github.com/mattn/go-isatty" - packages = ["."] - pruneopts = "" - revision = "6ca4dbf54d38eea1a992b3c722a76a5d1c4cb25c" - version = "v0.0.4" - -[[projects]] - digest = "1:8bbdb2b3dce59271877770d6fe7dcbb8362438fa7d2e1e1f688e4bf2aac72706" - name = "github.com/mattn/go-sqlite3" - packages = ["."] - pruneopts = "" - revision = "c7c4067b79cc51e6dfdcef5c702e74b1e0fa7c75" - version = "v1.10.0" - -[[projects]] - digest = "1:1d7e1867c49a6dd9856598ef7c3123604ea3daabf5b83f303ff457bcbc410b1d" - name = "github.com/pkg/errors" - packages = ["."] - pruneopts = "" - revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4" - version = "v0.8.1" - -[[projects]] - digest = "1:7f569d906bdd20d906b606415b7d794f798f91a62fcfb6a4daa6d50690fb7a3f" - name = "github.com/satori/go.uuid" - packages = ["."] - pruneopts = "" - revision = "f58768cc1a7a7e77a3bd49e98cdd21419399b6a3" - version = "v1.2.0" - -[[projects]] - digest = "1:a1403cc8a94b8d7956ee5e9694badef0e7b051af289caad1cf668331e3ffa4f6" - name = "github.com/spf13/cobra" - packages = ["."] - pruneopts = "" - revision = "ef82de70bb3f60c65fb8eebacbb2d122ef517385" - version = "v0.0.3" - -[[projects]] - digest = "1:cbaf13cdbfef0e4734ed8a7504f57fe893d471d62a35b982bf6fb3f036449a66" - name = "github.com/spf13/pflag" - packages = ["."] - pruneopts = "" - revision = "298182f68c66c05229eb03ac171abe6e309ee79a" - version = "v1.0.3" - -[[projects]] - branch = "master" - digest = "1:d0f4eb7abce3fbd3f0dcbbc03ffe18464846afd34c815928d2ae11c1e5aded04" - name = "golang.org/x/crypto" - packages = [ - "hkdf", - "pbkdf2", - "ssh/terminal", - ] - pruneopts = "" - revision = "ffb98f73852f696ea2bb21a617a5c4b3e067a439" - -[[projects]] - branch = "master" - digest = "1:7e3b61f51ebcb58b3894928ed7c63aae68820dec1dd57166e5d6e65ef2868f40" - name = "golang.org/x/sys" - packages = [ - "unix", - "windows", - ] - pruneopts = "" - revision = "b90733256f2e882e81d52f9126de08df5615afd9" - -[[projects]] - branch = "v2" - digest = "1:cedccf16b71e86db87a24f8d4c70b0a855872eb967cb906a66b95de56aefbd0d" - name = "gopkg.in/yaml.v2" - packages = ["."] - pruneopts = "" - revision = "51d6538a90f86fe93ac480b35f37b2be17fef232" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - input-imports = [ - "github.com/dnote/actions", - "github.com/dnote/color", - "github.com/google/go-github/github", - "github.com/mattn/go-sqlite3", - "github.com/pkg/errors", - "github.com/satori/go.uuid", - "github.com/spf13/cobra", - "golang.org/x/crypto/hkdf", - "golang.org/x/crypto/pbkdf2", - "golang.org/x/crypto/ssh/terminal", - "gopkg.in/yaml.v2", - ] - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/cli/Gopkg.toml b/cli/Gopkg.toml deleted file mode 100644 index e5e6ef80..00000000 --- a/cli/Gopkg.toml +++ /dev/null @@ -1,53 +0,0 @@ -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" - - -[[constraint]] - branch = "master" - name = "github.com/google/go-github" - -[[constraint]] - name = "github.com/pkg/errors" - version = "0.8.0" - -[[constraint]] - name = "github.com/spf13/cobra" - version = "0.0.3" - -[[constraint]] - branch = "v2" - name = "gopkg.in/yaml.v2" - -[[constraint]] - name = "github.com/satori/go.uuid" - version = "1.1.0" - -[[constraint]] - name = "github.com/dnote/actions" - version = "0.2.0" - -[[constraint]] - name = "github.com/mattn/go-sqlite3" - version = "1.10.0" - -[[constraint]] - name = "github.com/dnote/color" - version = "1.7.0" diff --git a/cli/Makefile b/cli/Makefile deleted file mode 100644 index 1cfaed29..00000000 --- a/cli/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -all: - ./scripts/build.sh $(VERSION) -.PHONY: all - -release: - ./scripts/build.sh $(VERSION) - ./scripts/release.sh $(VERSION) -.PHONY: release - -clean: - @git clean -f -.PHONY: clean diff --git a/cli/README.md b/cli/README.md deleted file mode 100644 index 9a4c9cff..00000000 --- a/cli/README.md +++ /dev/null @@ -1,33 +0,0 @@ -# Dnote CLI - -A simple command line interface for Dnote. - - - -It is Designed to minimize context switching for taking notes. - -## Install - -On macOS, you can install using Homebrew: - -```sh -brew tap dnote/dnote -brew install dnote - -# to upgrade to the latest version -brew upgrade dnote -``` - -On Linux or macOS, you can use the installation script: - - curl -s https://raw.githubusercontent.com/dnote/dnote/master/cli/install.sh | sh - -In some cases, you might need an elevated permission: - - curl -s https://raw.githubusercontent.com/dnote/dnote/master/cli/install.sh | sudo sh - -Otherwise, you can download the binary for your platform manually from the [releases page](https://github.com/dnote/dnote/releases). - -## Commands - -Please refer to [commands](/COMMANDS.md). diff --git a/cli/assets/dnote.gif b/cli/assets/dnote.gif deleted file mode 100644 index 23867eef..00000000 Binary files a/cli/assets/dnote.gif and /dev/null differ diff --git a/cli/assets/main.png b/cli/assets/main.png deleted file mode 100644 index d5542855..00000000 Binary files a/cli/assets/main.png and /dev/null differ diff --git a/cli/cmd/add/add.go b/cli/cmd/add/add.go deleted file mode 100644 index b9e1f1c2..00000000 --- a/cli/cmd/add/add.go +++ /dev/null @@ -1,149 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package add - -import ( - "database/sql" - "time" - - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var reservedBookNames = []string{"trash", "conflicts"} - -var content string - -var example = ` - * Open an editor to write content - dnote add git - - * Skip the editor by providing content directly - dnote add git -c "time is a part of the commit hash"` - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) != 1 { - return errors.New("Incorrect number of argument") - } - - return nil -} - -// NewCmd returns a new add command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "add ", - Short: "Add a new note", - Aliases: []string{"a", "n", "new"}, - Example: example, - PreRunE: preRun, - RunE: newRun(ctx), - } - - f := cmd.Flags() - f.StringVarP(&content, "content", "c", "", "The new content for the note") - - return cmd -} - -func isReservedName(name string) bool { - for _, n := range reservedBookNames { - if name == n { - return true - } - } - - return false -} - -func newRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - bookName := args[0] - - if isReservedName(bookName) { - return errors.Errorf("book name '%s' is reserved", bookName) - } - - if content == "" { - fpath := core.GetDnoteTmpContentPath(ctx) - err := core.GetEditorInput(ctx, fpath, &content) - if err != nil { - return errors.Wrap(err, "Failed to get editor input") - } - } - - if content == "" { - return errors.New("Empty content") - } - - ts := time.Now().UnixNano() - err := writeNote(ctx, bookName, content, ts) - if err != nil { - return errors.Wrap(err, "Failed to write note") - } - - log.Successf("added to %s\n", bookName) - log.PrintContent(content) - - if err := core.CheckUpdate(ctx); err != nil { - log.Error(errors.Wrap(err, "automatically checking updates").Error()) - } - - return nil - } -} - -func writeNote(ctx infra.DnoteCtx, bookLabel string, content string, ts int64) error { - tx, err := ctx.DB.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - var bookUUID string - err = tx.QueryRow("SELECT uuid FROM books WHERE label = ?", bookLabel).Scan(&bookUUID) - if err == sql.ErrNoRows { - bookUUID = utils.GenerateUUID() - - b := core.NewBook(bookUUID, bookLabel, 0, false, true) - err = b.Insert(tx) - if err != nil { - tx.Rollback() - return errors.Wrap(err, "creating the book") - } - } else if err != nil { - return errors.Wrap(err, "finding the book") - } - - noteUUID := utils.GenerateUUID() - n := core.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, false, true) - - err = n.Insert(tx) - if err != nil { - tx.Rollback() - return errors.Wrap(err, "creating the note") - } - - tx.Commit() - - return nil -} diff --git a/cli/cmd/cat/cat.go b/cli/cmd/cat/cat.go deleted file mode 100644 index 60ccbd6e..00000000 --- a/cli/cmd/cat/cat.go +++ /dev/null @@ -1,113 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package cat - -import ( - "database/sql" - "fmt" - "time" - - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var example = ` - * See the notes with index 2 from a book 'javascript' - dnote cat javascript 2 - ` - -var deprecationWarning = `and "view" will replace it in v0.5.0. - - Run "dnote view --help" for more information. -` - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) != 2 { - return errors.New("Incorrect number of arguments") - } - - return nil -} - -// NewCmd returns a new cat command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "cat ", - Aliases: []string{"c"}, - Short: "See a note", - Example: example, - RunE: NewRun(ctx), - PreRunE: preRun, - Deprecated: deprecationWarning, - } - - return cmd -} - -type noteInfo struct { - BookLabel string - UUID string - Content string - AddedOn int64 - EditedOn int64 -} - -// NewRun returns a new run function -func NewRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - db := ctx.DB - bookLabel := args[0] - noteRowID := args[1] - - var bookUUID string - err := db.QueryRow("SELECT uuid FROM books WHERE label = ?", bookLabel).Scan(&bookUUID) - if err == sql.ErrNoRows { - return errors.Errorf("book '%s' not found", bookLabel) - } else if err != nil { - return errors.Wrap(err, "querying the book") - } - - var info noteInfo - err = db.QueryRow(`SELECT books.label, notes.uuid, notes.body, notes.added_on, notes.edited_on - FROM notes - INNER JOIN books ON books.uuid = notes.book_uuid - WHERE notes.rowid = ? AND books.uuid = ?`, noteRowID, bookUUID). - Scan(&info.BookLabel, &info.UUID, &info.Content, &info.AddedOn, &info.EditedOn) - if err == sql.ErrNoRows { - return errors.Errorf("note %s not found in the book '%s'", noteRowID, bookLabel) - } else if err != nil { - return errors.Wrap(err, "querying the note") - } - - log.Infof("book name: %s\n", info.BookLabel) - log.Infof("note uuid: %s\n", info.UUID) - log.Infof("created at: %s\n", time.Unix(0, info.AddedOn).Format("Jan 2, 2006 3:04pm (MST)")) - if info.EditedOn != 0 { - log.Infof("updated at: %s\n", time.Unix(0, info.EditedOn).Format("Jan 2, 2006 3:04pm (MST)")) - } - fmt.Printf("\n------------------------content------------------------\n") - fmt.Printf("%s", info.Content) - fmt.Printf("\n-------------------------------------------------------\n") - - return nil - } -} diff --git a/cli/cmd/edit/edit.go b/cli/cmd/edit/edit.go deleted file mode 100644 index 23665005..00000000 --- a/cli/cmd/edit/edit.go +++ /dev/null @@ -1,130 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package edit - -import ( - "database/sql" - "fmt" - "io/ioutil" - "time" - - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var newContent string - -var example = ` - * Edit the note by index in a book - dnote edit js 3 - - * Skip the prompt by providing new content directly - dnote edit js 3 -c "new content"` - -// NewCmd returns a new edit command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "edit", - Short: "Edit a note or a book", - Aliases: []string{"e"}, - Example: example, - PreRunE: preRun, - RunE: newRun(ctx), - } - - f := cmd.Flags() - f.StringVarP(&newContent, "content", "c", "", "The new content for the note") - - return cmd -} - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) != 2 { - return errors.New("Incorrect number of argument") - } - - return nil -} - -func newRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - db := ctx.DB - bookLabel := args[0] - noteRowID := args[1] - - bookUUID, err := core.GetBookUUID(ctx, bookLabel) - if err != nil { - return errors.Wrap(err, "finding book uuid") - } - - var noteUUID, oldContent string - err = db.QueryRow("SELECT uuid, body FROM notes WHERE rowid = ? AND book_uuid = ?", noteRowID, bookUUID).Scan(¬eUUID, &oldContent) - if err == sql.ErrNoRows { - return errors.Errorf("note %s not found in the book '%s'", noteRowID, bookLabel) - } else if err != nil { - return errors.Wrap(err, "querying the book") - } - - if newContent == "" { - fpath := core.GetDnoteTmpContentPath(ctx) - - e := ioutil.WriteFile(fpath, []byte(oldContent), 0644) - if e != nil { - return errors.Wrap(e, "preparing tmp content file") - } - - e = core.GetEditorInput(ctx, fpath, &newContent) - if e != nil { - return errors.Wrap(err, "getting editor input") - } - } - - if oldContent == newContent { - return errors.New("Nothing changed") - } - - ts := time.Now().UnixNano() - newContent = core.SanitizeContent(newContent) - - tx, err := db.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - _, err = tx.Exec(`UPDATE notes - SET body = ?, edited_on = ?, dirty = ? - WHERE rowid = ? AND book_uuid = ?`, newContent, ts, true, noteRowID, bookUUID) - if err != nil { - tx.Rollback() - return errors.Wrap(err, "updating the note") - } - - tx.Commit() - - log.Success("edited the note\n") - fmt.Printf("\n------------------------content------------------------\n") - fmt.Printf("%s", newContent) - fmt.Printf("\n-------------------------------------------------------\n") - - return nil - } -} diff --git a/cli/cmd/login/login.go b/cli/cmd/login/login.go deleted file mode 100644 index e2ffdf95..00000000 --- a/cli/cmd/login/login.go +++ /dev/null @@ -1,126 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package login - -import ( - "encoding/base64" - "strconv" - - "github.com/dnote/dnote/cli/client" - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/crypt" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var example = ` - dnote login` - -// NewCmd returns a new login command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "login", - Short: "Login to dnote server", - Example: example, - RunE: newRun(ctx), - } - - return cmd -} - -// Do dervies credentials on the client side and requests a session token from the server -func Do(ctx infra.DnoteCtx, email, password string) error { - presigninResp, err := client.GetPresignin(ctx, email) - if err != nil { - return errors.Wrap(err, "getting presiginin") - } - - masterKey, authKey, err := crypt.MakeKeys([]byte(password), []byte(email), presigninResp.Iteration) - if err != nil { - return errors.Wrap(err, "making keys") - } - - authKeyB64 := base64.StdEncoding.EncodeToString(authKey) - signinResp, err := client.Signin(ctx, email, authKeyB64) - if err != nil { - return errors.Wrap(err, "requesting session") - } - - cipherKeyDec, err := crypt.AesGcmDecrypt(masterKey, signinResp.CipherKeyEnc) - if err != nil { - return errors.Wrap(err, "decrypting cipher key") - } - - cipherKeyDecB64 := base64.StdEncoding.EncodeToString(cipherKeyDec) - - db := ctx.DB - tx, err := db.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - if err := core.UpsertSystem(tx, infra.SystemCipherKey, cipherKeyDecB64); err != nil { - return errors.Wrap(err, "saving enc key") - } - if err := core.UpsertSystem(tx, infra.SystemSessionKey, signinResp.Key); err != nil { - return errors.Wrap(err, "saving session key") - } - if err := core.UpsertSystem(tx, infra.SystemSessionKeyExpiry, strconv.FormatInt(signinResp.ExpiresAt, 10)); err != nil { - return errors.Wrap(err, "saving session key") - } - - tx.Commit() - - return nil -} - -func newRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - var email, password string - if err := utils.PromptInput("email", &email); err != nil { - return errors.Wrap(err, "getting email input") - } - if email == "" { - return errors.New("Email is empty") - } - - if err := utils.PromptPassword("password", &password); err != nil { - return errors.Wrap(err, "getting password input") - } - if password == "" { - return errors.New("Password is empty") - } - - err := Do(ctx, email, password) - if errors.Cause(err) == client.ErrInvalidLogin { - log.Error("wrong login\n") - return nil - } else if err != nil { - return errors.Wrap(err, "logging in") - } - - log.Success("logged in\n") - - return nil - } - -} diff --git a/cli/cmd/logout/logout.go b/cli/cmd/logout/logout.go deleted file mode 100644 index 4f8536dc..00000000 --- a/cli/cmd/logout/logout.go +++ /dev/null @@ -1,100 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package logout - -import ( - "database/sql" - - "github.com/dnote/dnote/cli/client" - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -// ErrNotLoggedIn is an error for logging out when not logged in -var ErrNotLoggedIn = errors.New("not logged in") - -var example = ` - dnote logout` - -// NewCmd returns a new logout command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "logout", - Short: "Logout from the server", - Example: example, - RunE: newRun(ctx), - } - - return cmd -} - -// Do performs logout -func Do(ctx infra.DnoteCtx) error { - db := ctx.DB - tx, err := db.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - var key string - err = core.GetSystem(tx, infra.SystemSessionKey, &key) - if errors.Cause(err) == sql.ErrNoRows { - return ErrNotLoggedIn - } else if err != nil { - return errors.Wrap(err, "getting session key") - } - - err = client.Signout(ctx, key) - if err != nil { - return errors.Wrap(err, "requesting logout") - } - - if err := core.DeleteSystem(tx, infra.SystemCipherKey); err != nil { - return errors.Wrap(err, "deleting enc key") - } - if err := core.DeleteSystem(tx, infra.SystemSessionKey); err != nil { - return errors.Wrap(err, "deleting session key") - } - if err := core.DeleteSystem(tx, infra.SystemSessionKeyExpiry); err != nil { - return errors.Wrap(err, "deleting session key expiry") - } - - tx.Commit() - - return nil -} - -func newRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - err := Do(ctx) - if err == ErrNotLoggedIn { - log.Error("not logged in\n") - return nil - } else if err != nil { - return errors.Wrap(err, "logging out") - } - - log.Success("logged out\n") - - return nil - } -} diff --git a/cli/cmd/ls/ls.go b/cli/cmd/ls/ls.go deleted file mode 100644 index c232e247..00000000 --- a/cli/cmd/ls/ls.go +++ /dev/null @@ -1,202 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package ls - -import ( - "database/sql" - "fmt" - "strings" - - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var example = ` - * List all books - dnote ls - - * List notes in a book - dnote ls javascript - ` - -var deprecationWarning = `and "view" will replace it in v1.0.0. - -Run "dnote view --help" for more information. -` - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) > 1 { - return errors.New("Incorrect number of argument") - } - - return nil -} - -// NewCmd returns a new ls command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "ls ", - Aliases: []string{"l", "notes"}, - Short: "List all notes", - Example: example, - RunE: NewRun(ctx), - PreRunE: preRun, - Deprecated: deprecationWarning, - } - - return cmd -} - -// NewRun returns a new run function for ls -func NewRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - if err := printBooks(ctx); err != nil { - return errors.Wrap(err, "viewing books") - } - - return nil - } - - bookName := args[0] - if err := printNotes(ctx, bookName); err != nil { - return errors.Wrapf(err, "viewing book '%s'", bookName) - } - - return nil - } -} - -// bookInfo is an information about the book to be printed on screen -type bookInfo struct { - BookLabel string - NoteCount int -} - -// noteInfo is an information about the note to be printed on screen -type noteInfo struct { - RowID int - Body string -} - -// getNewlineIdx returns the index of newline character in a string -func getNewlineIdx(str string) int { - var ret int - - ret = strings.Index(str, "\n") - - if ret == -1 { - ret = strings.Index(str, "\r\n") - } - - return ret -} - -// formatBody returns an excerpt of the given raw note content and a boolean -// indicating if the returned string has been excertped -func formatBody(noteBody string) (string, bool) { - newlineIdx := getNewlineIdx(noteBody) - - if newlineIdx > -1 { - ret := strings.Trim(noteBody[0:newlineIdx], " ") - - return ret, true - } - - return strings.Trim(noteBody, " "), false -} - -func printBooks(ctx infra.DnoteCtx) error { - db := ctx.DB - - rows, err := db.Query(`SELECT books.label, count(notes.uuid) note_count - FROM books - LEFT JOIN notes ON notes.book_uuid = books.uuid AND notes.deleted = false - WHERE books.deleted = false - GROUP BY books.uuid - ORDER BY books.label ASC;`) - if err != nil { - return errors.Wrap(err, "querying books") - } - defer rows.Close() - - infos := []bookInfo{} - for rows.Next() { - var info bookInfo - err = rows.Scan(&info.BookLabel, &info.NoteCount) - if err != nil { - return errors.Wrap(err, "scanning a row") - } - - infos = append(infos, info) - } - - for _, info := range infos { - log.Printf("%s %s\n", info.BookLabel, log.ColorYellow.Sprintf("(%d)", info.NoteCount)) - } - - return nil -} - -func printNotes(ctx infra.DnoteCtx, bookName string) error { - db := ctx.DB - - var bookUUID string - err := db.QueryRow("SELECT uuid FROM books WHERE label = ?", bookName).Scan(&bookUUID) - if err == sql.ErrNoRows { - return errors.New("book not found") - } else if err != nil { - return errors.Wrap(err, "querying the book") - } - - rows, err := db.Query(`SELECT rowid, body FROM notes WHERE book_uuid = ? AND deleted = ? ORDER BY added_on ASC;`, bookUUID, false) - if err != nil { - return errors.Wrap(err, "querying notes") - } - defer rows.Close() - - infos := []noteInfo{} - for rows.Next() { - var info noteInfo - err = rows.Scan(&info.RowID, &info.Body) - if err != nil { - return errors.Wrap(err, "scanning a row") - } - - infos = append(infos, info) - } - - log.Infof("on book %s\n", bookName) - - for _, info := range infos { - body, isExcerpt := formatBody(info.Body) - - rowid := log.ColorYellow.Sprintf("(%d)", info.RowID) - if isExcerpt { - body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]")) - } - - log.Plainf("%s %s\n", rowid, body) - } - - return nil -} diff --git a/cli/cmd/remove/remove.go b/cli/cmd/remove/remove.go deleted file mode 100644 index 781279a4..00000000 --- a/cli/cmd/remove/remove.go +++ /dev/null @@ -1,163 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package remove - -import ( - "database/sql" - "fmt" - - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var targetBookName string - -var example = ` - * Delete a note by its index from a book - dnote delete js 2 - - * Delete a book - dnote delete -b js` - -// NewCmd returns a new remove command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "remove", - Short: "Remove a note or a book", - Aliases: []string{"rm", "d", "delete"}, - Example: example, - RunE: newRun(ctx), - } - - f := cmd.Flags() - f.StringVarP(&targetBookName, "book", "b", "", "The book name to delete") - - return cmd -} - -func newRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - if targetBookName != "" { - if err := removeBook(ctx, targetBookName); err != nil { - return errors.Wrap(err, "removing the book") - } - - return nil - } - - if len(args) < 2 { - return errors.New("Missing argument") - } - - targetBook := args[0] - noteRowID := args[1] - - if err := removeNote(ctx, noteRowID, targetBook); err != nil { - return errors.Wrap(err, "removing the note") - } - - return nil - } -} - -func removeNote(ctx infra.DnoteCtx, noteRowID, bookLabel string) error { - db := ctx.DB - - bookUUID, err := core.GetBookUUID(ctx, bookLabel) - if err != nil { - return errors.Wrap(err, "finding book uuid") - } - - var noteUUID, noteContent string - err = db.QueryRow("SELECT uuid, body FROM notes WHERE rowid = ? AND book_uuid = ?", noteRowID, bookUUID).Scan(¬eUUID, ¬eContent) - if err == sql.ErrNoRows { - return errors.Errorf("note %s not found in the book '%s'", noteRowID, bookLabel) - } else if err != nil { - return errors.Wrap(err, "querying the book") - } - - // todo: multiline - log.Printf("body: \"%s\"\n", noteContent) - - ok, err := utils.AskConfirmation("remove this note?", false) - if err != nil { - return errors.Wrap(err, "getting confirmation") - } - if !ok { - log.Warnf("aborted by user\n") - return nil - } - - tx, err := db.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE uuid = ? AND book_uuid = ?", true, true, "", noteUUID, bookUUID); err != nil { - return errors.Wrap(err, "removing the note") - } - tx.Commit() - - log.Successf("removed from %s\n", bookLabel) - - return nil -} - -func removeBook(ctx infra.DnoteCtx, bookLabel string) error { - db := ctx.DB - - bookUUID, err := core.GetBookUUID(ctx, bookLabel) - if err != nil { - return errors.Wrap(err, "finding book uuid") - } - - ok, err := utils.AskConfirmation(fmt.Sprintf("delete book '%s' and all its notes?", bookLabel), false) - if err != nil { - return errors.Wrap(err, "getting confirmation") - } - if !ok { - log.Warnf("aborted by user\n") - return nil - } - - tx, err := db.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE book_uuid = ?", true, true, "", bookUUID); err != nil { - return errors.Wrap(err, "removing notes in the book") - } - - // override the label with a random string - uniqLabel := utils.GenerateUUID() - if _, err = tx.Exec("UPDATE books SET deleted = ?, dirty = ?, label = ? WHERE uuid = ?", true, true, uniqLabel, bookUUID); err != nil { - return errors.Wrap(err, "removing the book") - } - - tx.Commit() - - log.Success("removed book\n") - - return nil -} diff --git a/cli/cmd/root/root.go b/cli/cmd/root/root.go deleted file mode 100644 index 3da8e7e0..00000000 --- a/cli/cmd/root/root.go +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package root - -import ( - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/migrate" - "github.com/pkg/errors" - "github.com/spf13/cobra" -) - -var root = &cobra.Command{ - Use: "dnote", - Short: "Dnote - Instantly capture what you learn while coding", - SilenceErrors: true, - SilenceUsage: true, -} - -// Register adds a new command -func Register(cmd *cobra.Command) { - root.AddCommand(cmd) -} - -// Execute runs the main command -func Execute() error { - return root.Execute() -} - -// Prepare initializes necessary files -func Prepare(ctx infra.DnoteCtx) error { - if err := core.InitFiles(ctx); err != nil { - return errors.Wrap(err, "initializing files") - } - - if err := infra.InitDB(ctx); err != nil { - return errors.Wrap(err, "initializing database") - } - if err := core.InitSystem(ctx); err != nil { - return errors.Wrap(err, "initializing system data") - } - - if err := migrate.Legacy(ctx); err != nil { - return errors.Wrap(err, "running legacy migration") - } - if err := migrate.Run(ctx, migrate.LocalSequence, migrate.LocalMode); err != nil { - return errors.Wrap(err, "running migration") - } - - return nil -} diff --git a/cli/cmd/sync/sync_test.go b/cli/cmd/sync/sync_test.go deleted file mode 100644 index b87ca333..00000000 --- a/cli/cmd/sync/sync_test.go +++ /dev/null @@ -1,3183 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package sync - -import ( - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - "sort" - "strings" - "testing" - - "github.com/dnote/dnote/cli/client" - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/crypt" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/testutils" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" -) - -var cipherKey = []byte("AES256Key-32Characters1234567890") - -func TestProcessFragments(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - fragments := []client.SyncFragment{ - client.SyncFragment{ - FragMaxUSN: 10, - UserMaxUSN: 10, - CurrentTime: 1550436136, - Notes: []client.SyncFragNote{ - client.SyncFragNote{ - UUID: "45546de0-40ed-45cf-9bfc-62ce729a7d3d", - Body: "7GgIppDdxDn+4DUoVoLXbncZDRqXGwbDVNF/eCssu+1BXMdq+HAziJHGgK7drdcIBtYDDXj0OwHz9dQDDOyWeNqkLWEIQ2Roygs229dRxdO3Z6ST+qSOr/9TTjDlFxydF5Ps7nAXdN9KVxH8FKIZDsxJ45qeLKpQK/6poAM39BCOiysqAXJQz9ngOJiqImAuftS6d/XhwX77QvnM91VCKK0tFmsMdDDw0J9QMwnlYU1CViHy1Hdhhcf9Ea38Mj4SCrWMPscXyP2fpAu5ukbIK3vS2pvbnH5vC8ZuvihrQif1BsiwfYmN981mLYs069Dn4B72qcXPwU7qrN3V0k57JGcAlTiEoOD5QowyraensQlR1doorLb43SjTiJLItougn5K5QPRiHuNxfv39pa7A0gKA1n/3UhG/SBuCpDuPYjwmBkvkzCKJNgpbLQ8p29JXMQcWrm4e9GfnVjMhAEtxttIta3MN6EcYG7cB1dJ04OLYVcJuRA==", - }, - client.SyncFragNote{ - UUID: "a25a5336-afe9-46c4-b881-acab911c0bc3", - Body: "WGzcYA6kLuUFEU7HLTDJt7UWF7fEmbCPHfC16VBrAyfT2wDejXbIuFpU5L7g0aU=", - }, - }, - Books: []client.SyncFragBook{ - client.SyncFragBook{ - UUID: "e8ac6f25-d95b-435a-9fae-094f7506a5ac", - Label: "qBrSrAcnTUHu51bIrv6jSA/dNffr/kRlIg+MklxeQQ==", - }, - client.SyncFragBook{ - UUID: "05fd8b95-ddcd-4071-9380-4358ffb8a436", - Label: "uHWoBFdKT78gTkFR7qhyzZkrn59c8ktEa8idrLkksKzIQ3VVAXxq0QZp7Uc=", - }, - }, - ExpungedNotes: []string{}, - ExpungedBooks: []string{}, - }, - } - - // exec - sl, err := processFragments(fragments, cipherKey) - if err != nil { - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - expected := syncList{ - Notes: map[string]client.SyncFragNote{ - "45546de0-40ed-45cf-9bfc-62ce729a7d3d": client.SyncFragNote{ - UUID: "45546de0-40ed-45cf-9bfc-62ce729a7d3d", - Body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Donec ac libero efficitur, posuere dui non, egestas lectus.\n Aliquam urna ligula, sagittis eu volutpat vel, consequat et augue.\n\n Ut mi urna, dignissim a ex eget, venenatis accumsan sem. Praesent facilisis, ligula hendrerit auctor varius, mauris metus hendrerit dolor, sit amet pulvinar.", - }, - "a25a5336-afe9-46c4-b881-acab911c0bc3": client.SyncFragNote{ - UUID: "a25a5336-afe9-46c4-b881-acab911c0bc3", - Body: "foo bar baz quz\nqux", - }, - }, - Books: map[string]client.SyncFragBook{ - "e8ac6f25-d95b-435a-9fae-094f7506a5ac": client.SyncFragBook{ - UUID: "e8ac6f25-d95b-435a-9fae-094f7506a5ac", - Label: "foo", - }, - "05fd8b95-ddcd-4071-9380-4358ffb8a436": client.SyncFragBook{ - UUID: "05fd8b95-ddcd-4071-9380-4358ffb8a436", - Label: "foo-bar-baz-1000", - }, - }, - ExpungedNotes: map[string]bool{}, - ExpungedBooks: map[string]bool{}, - MaxUSN: 10, - MaxCurrentTime: 1550436136, - } - - // test - testutils.AssertDeepEqual(t, sl, expected, "syncList mismatch") -} - -func TestGetLastSyncAt(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "setting up last_sync_at", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastSyncAt, 1541108743) - - // exec - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - got, err := getLastSyncAt(tx) - if err != nil { - t.Fatalf(errors.Wrap(err, "getting last_sync_at").Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, got, 1541108743, "last_sync_at mismatch") -} - -func TestGetLastMaxUSN(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "setting up last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, 20001) - - // exec - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - got, err := getLastMaxUSN(tx) - if err != nil { - t.Fatalf(errors.Wrap(err, "getting last_max_usn").Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, got, 20001, "last_max_usn mismatch") -} - -func TestResolveLabel(t *testing.T) { - testCases := []struct { - input string - expected string - }{ - { - input: "js", - expected: "js (2)", - }, - { - input: "css", - expected: "css (3)", - }, - { - input: "linux", - expected: "linux (4)", - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b1-uuid", "js") - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b2-uuid", "css (2)") - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b3-uuid", "linux (1)") - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b4-uuid", "linux (2)") - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b5-uuid", "linux (3)") - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - got, err := resolveLabel(tx, tc.input) - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - tx.Rollback() - - testutils.AssertEqual(t, got, tc.expected, fmt.Sprintf("output mismatch for test case %d", idx)) - }() - } -} - -func TestSyncDeleteNote(t *testing.T) { - t.Run("exists on server only", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := syncDeleteNote(tx, "nonexistent-note-uuid"); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 0, "book count mismatch") - }) - - t.Run("local copy is dirty", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - testutils.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) - testutils.MustExec(t, "inserting n2 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 11, "n2 body", 1541108743, false, true) - - var n1 core.Note - testutils.MustScan(t, "getting n1 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - var n2 core.Note - testutils.MustScan(t, "getting n2 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", "n2-uuid"), - &n2.UUID, &n2.BookUUID, &n2.USN, &n2.AddedOn, &n2.EditedOn, &n2.Body, &n2.Public, &n2.Deleted, &n2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) - } - - if err := syncDeleteNote(tx, "n1-uuid"); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - // do not delete note if local copy is dirty - testutils.AssertEqualf(t, noteCount, 2, "note count mismatch for test case") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch for test case") - - var n1Record core.Note - testutils.MustScan(t, "getting n1 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n1.UUID), - &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.Body, &n1Record.Public, &n1Record.Deleted, &n1Record.Dirty) - var n2Record core.Note - testutils.MustScan(t, "getting n2 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.USN, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.Body, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) - - testutils.AssertEqual(t, n1Record.UUID, n1.UUID, "n1 UUID mismatch for test case") - testutils.AssertEqual(t, n1Record.BookUUID, n1.BookUUID, "n1 BookUUID mismatch for test case") - testutils.AssertEqual(t, n1Record.USN, n1.USN, "n1 USN mismatch for test case") - testutils.AssertEqual(t, n1Record.AddedOn, n1.AddedOn, "n1 AddedOn mismatch for test case") - testutils.AssertEqual(t, n1Record.EditedOn, n1.EditedOn, "n1 EditedOn mismatch for test case") - testutils.AssertEqual(t, n1Record.Body, n1.Body, "n1 Body mismatch for test case") - testutils.AssertEqual(t, n1Record.Public, n1.Public, "n1 Public mismatch for test case") - testutils.AssertEqual(t, n1Record.Deleted, n1.Deleted, "n1 Deleted mismatch for test case") - testutils.AssertEqual(t, n1Record.Dirty, n1.Dirty, "n1 Dirty mismatch for test case") - - testutils.AssertEqual(t, n2Record.UUID, n2.UUID, "n2 UUID mismatch for test case") - testutils.AssertEqual(t, n2Record.BookUUID, n2.BookUUID, "n2 BookUUID mismatch for test case") - testutils.AssertEqual(t, n2Record.USN, n2.USN, "n2 USN mismatch for test case") - testutils.AssertEqual(t, n2Record.AddedOn, n2.AddedOn, "n2 AddedOn mismatch for test case") - testutils.AssertEqual(t, n2Record.EditedOn, n2.EditedOn, "n2 EditedOn mismatch for test case") - testutils.AssertEqual(t, n2Record.Body, n2.Body, "n2 Body mismatch for test case") - testutils.AssertEqual(t, n2Record.Public, n2.Public, "n2 Public mismatch for test case") - testutils.AssertEqual(t, n2Record.Deleted, n2.Deleted, "n2 Deleted mismatch for test case") - testutils.AssertEqual(t, n2Record.Dirty, n2.Dirty, "n2 Dirty mismatch for test case") - }) - - t.Run("local copy is not dirty", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - testutils.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n2 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 11, "n2 body", 1541108743, false, false) - - var n1 core.Note - testutils.MustScan(t, "getting n1 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - var n2 core.Note - testutils.MustScan(t, "getting n2 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", "n2-uuid"), - &n2.UUID, &n2.BookUUID, &n2.USN, &n2.AddedOn, &n2.EditedOn, &n2.Body, &n2.Public, &n2.Deleted, &n2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) - } - - if err := syncDeleteNote(tx, "n1-uuid"); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch for test case") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch for test case") - - var n2Record core.Note - testutils.MustScan(t, "getting n2 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.USN, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.Body, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) - - testutils.AssertEqual(t, n2Record.UUID, n2.UUID, "n2 UUID mismatch for test case") - testutils.AssertEqual(t, n2Record.BookUUID, n2.BookUUID, "n2 BookUUID mismatch for test case") - testutils.AssertEqual(t, n2Record.USN, n2.USN, "n2 USN mismatch for test case") - testutils.AssertEqual(t, n2Record.AddedOn, n2.AddedOn, "n2 AddedOn mismatch for test case") - testutils.AssertEqual(t, n2Record.EditedOn, n2.EditedOn, "n2 EditedOn mismatch for test case") - testutils.AssertEqual(t, n2Record.Body, n2.Body, "n2 Body mismatch for test case") - testutils.AssertEqual(t, n2Record.Public, n2.Public, "n2 Public mismatch for test case") - testutils.AssertEqual(t, n2Record.Deleted, n2.Deleted, "n2 Deleted mismatch for test case") - testutils.AssertEqual(t, n2Record.Dirty, n2.Dirty, "n2 Dirty mismatch for test case") - }) -} - -func TestSyncDeleteBook(t *testing.T) { - t.Run("exists on server only", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b1-uuid", "b1-label") - - var b1 core.Book - testutils.MustScan(t, "getting b1 for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1.UUID, &b1.Label, &b1.USN, &b1.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := syncDeleteBook(tx, "nonexistent-book-uuid"); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - - var b1Record core.Book - testutils.MustScan(t, "getting b1 for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch for test case") - testutils.AssertEqual(t, b1Record.Label, b1.Label, "b1 Label mismatch for test case") - testutils.AssertEqual(t, b1Record.USN, b1.USN, "b1 USN mismatch for test case") - testutils.AssertEqual(t, b1Record.Dirty, b1.Dirty, "b1 Dirty mismatch for test case") - }) - - t.Run("local copy is dirty", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b1UUID, "b1-label", 12, true) - testutils.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) - - var b1 core.Book - testutils.MustScan(t, "getting b1 for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), - &b1.UUID, &b1.Label, &b1.USN, &b1.Dirty) - var n1 core.Note - testutils.MustScan(t, "getting n1 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) - } - - if err := syncDeleteBook(tx, b1UUID); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - // do not delete note if local copy is dirty - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch for test case") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch for test case") - - var b1Record core.Book - testutils.MustScan(t, "getting b1Record for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - var n1Record core.Note - testutils.MustScan(t, "getting n1 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n1.UUID), - &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.Body, &n1Record.Public, &n1Record.Deleted, &n1Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch for test case") - testutils.AssertEqual(t, b1Record.Label, b1.Label, "b1 Label mismatch for test case") - testutils.AssertEqual(t, b1Record.USN, b1.USN, "b1 USN mismatch for test case") - testutils.AssertEqual(t, b1Record.Dirty, b1.Dirty, "b1 Dirty mismatch for test case") - - testutils.AssertEqual(t, n1Record.UUID, n1.UUID, "n1 UUID mismatch for test case") - testutils.AssertEqual(t, n1Record.BookUUID, n1.BookUUID, "n1 BookUUID mismatch for test case") - testutils.AssertEqual(t, n1Record.USN, n1.USN, "n1 USN mismatch for test case") - testutils.AssertEqual(t, n1Record.AddedOn, n1.AddedOn, "n1 AddedOn mismatch for test case") - testutils.AssertEqual(t, n1Record.EditedOn, n1.EditedOn, "n1 EditedOn mismatch for test case") - testutils.AssertEqual(t, n1Record.Body, n1.Body, "n1 Body mismatch for test case") - testutils.AssertEqual(t, n1Record.Public, n1.Public, "n1 Public mismatch for test case") - testutils.AssertEqual(t, n1Record.Deleted, n1.Deleted, "n1 Deleted mismatch for test case") - testutils.AssertEqual(t, n1Record.Dirty, n1.Dirty, "n1 Dirty mismatch for test case") - }) - - t.Run("local copy is not dirty", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - b2UUID := utils.GenerateUUID() - - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - testutils.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) - testutils.MustExec(t, "inserting b2 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") - testutils.MustExec(t, "inserting n2 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b2UUID, 11, "n2 body", 1541108743, false, false) - - var b2 core.Book - testutils.MustScan(t, "getting b2 for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b2UUID), - &b2.UUID, &b2.Label, &b2.USN, &b2.Dirty) - var n2 core.Note - testutils.MustScan(t, "getting n2 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", "n2-uuid"), - &n2.UUID, &n2.BookUUID, &n2.USN, &n2.AddedOn, &n2.EditedOn, &n2.Body, &n2.Public, &n2.Deleted, &n2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) - } - - if err := syncDeleteBook(tx, b1UUID); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch for test case") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch for test case") - - var b2Record core.Book - testutils.MustScan(t, "getting b2 for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b2UUID), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) - var n2Record core.Note - testutils.MustScan(t, "getting n2 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.USN, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.Body, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) - - testutils.AssertEqual(t, b2Record.UUID, b2.UUID, "b2 UUID mismatch for test case") - testutils.AssertEqual(t, b2Record.Label, b2.Label, "b2 Label mismatch for test case") - testutils.AssertEqual(t, b2Record.USN, b2.USN, "b2 USN mismatch for test case") - testutils.AssertEqual(t, b2Record.Dirty, b2.Dirty, "b2 Dirty mismatch for test case") - - testutils.AssertEqual(t, n2Record.UUID, n2.UUID, "n2 UUID mismatch for test case") - testutils.AssertEqual(t, n2Record.BookUUID, n2.BookUUID, "n2 BookUUID mismatch for test case") - testutils.AssertEqual(t, n2Record.USN, n2.USN, "n2 USN mismatch for test case") - testutils.AssertEqual(t, n2Record.AddedOn, n2.AddedOn, "n2 AddedOn mismatch for test case") - testutils.AssertEqual(t, n2Record.EditedOn, n2.EditedOn, "n2 EditedOn mismatch for test case") - testutils.AssertEqual(t, n2Record.Body, n2.Body, "n2 Body mismatch for test case") - testutils.AssertEqual(t, n2Record.Public, n2.Public, "n2 Public mismatch for test case") - testutils.AssertEqual(t, n2Record.Deleted, n2.Deleted, "n2 Deleted mismatch for test case") - testutils.AssertEqual(t, n2Record.Dirty, n2.Dirty, "n2 Dirty mismatch for test case") - }) - - t.Run("local copy has at least one note that is dirty", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - testutils.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction for test case").Error()) - } - - if err := syncDeleteBook(tx, b1UUID); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch for test case") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch for test case") - - var b1Record core.Book - testutils.MustScan(t, "getting b1 for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - var n1Record core.Note - testutils.MustScan(t, "getting n1 for test case", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, body,deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), - &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.Body, &n1Record.Deleted, &n1Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1UUID, "b1 UUID mismatch for test case") - testutils.AssertEqual(t, b1Record.Label, "b1-label", "b1 Label mismatch for test case") - testutils.AssertEqual(t, b1Record.Dirty, true, "b1 Dirty mismatch for test case") - - testutils.AssertEqual(t, n1Record.UUID, "n1-uuid", "n1 UUID mismatch for test case") - testutils.AssertEqual(t, n1Record.BookUUID, b1UUID, "n1 BookUUID mismatch for test case") - testutils.AssertEqual(t, n1Record.USN, 10, "n1 USN mismatch for test case") - testutils.AssertEqual(t, n1Record.AddedOn, int64(1541108743), "n1 AddedOn mismatch for test case") - testutils.AssertEqual(t, n1Record.Body, "n1 body", "n1 Body mismatch for test case") - testutils.AssertEqual(t, n1Record.Deleted, false, "n1 Deleted mismatch for test case") - testutils.AssertEqual(t, n1Record.Dirty, true, "n1 Dirty mismatch for test case") - }) -} - -func TestFullSyncNote(t *testing.T) { - t.Run("exists on server only", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - n := client.SyncFragNote{ - UUID: "n1-uuid", - BookUUID: b1UUID, - USN: 128, - AddedOn: 1541232118, - EditedOn: 1541219321, - Body: "n1-body", - Public: true, - Deleted: false, - } - - if err := fullSyncNote(tx, n); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - - var n1 core.Note - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - - testutils.AssertEqual(t, n1.UUID, n.UUID, "n1 UUID mismatch") - testutils.AssertEqual(t, n1.BookUUID, n.BookUUID, "n1 BookUUID mismatch") - testutils.AssertEqual(t, n1.USN, n.USN, "n1 USN mismatch") - testutils.AssertEqual(t, n1.AddedOn, n.AddedOn, "n1 AddedOn mismatch") - testutils.AssertEqual(t, n1.EditedOn, n.EditedOn, "n1 EditedOn mismatch") - testutils.AssertEqual(t, n1.Body, n.Body, "n1 Body mismatch") - testutils.AssertEqual(t, n1.Public, n.Public, "n1 Public mismatch") - testutils.AssertEqual(t, n1.Deleted, n.Deleted, "n1 Deleted mismatch") - testutils.AssertEqual(t, n1.Dirty, false, "n1 Dirty mismatch") - }) - - t.Run("exists on server and client", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - b2UUID := utils.GenerateUUID() - - testCases := []struct { - addedOn int64 - clientUSN int - clientEditedOn int64 - clientBody string - clientPublic bool - clientDeleted bool - clientBookUUID string - clientDirty bool - serverUSN int - serverEditedOn int64 - serverBody string - serverPublic bool - serverDeleted bool - serverBookUUID string - expectedUSN int - expectedAddedOn int64 - expectedEditedOn int64 - expectedBody string - expectedPublic bool - expectedDeleted bool - expectedBookUUID string - expectedDirty bool - }{ - // server has higher usn and client is dirty - { - clientDirty: true, - clientUSN: 1, - clientEditedOn: 0, - clientBody: "n1 body", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: true, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: true, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: true, - }, - // server has higher usn and client deleted locally - { - clientDirty: true, - clientUSN: 1, - clientEditedOn: 0, - clientBody: "", - clientPublic: false, - clientDeleted: true, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body server", - serverPublic: false, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body server", - expectedPublic: false, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: false, - }, - // server has higher usn and client is not dirty - { - clientDirty: false, - clientUSN: 1, - clientEditedOn: 0, - clientBody: "n1 body", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: true, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: true, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: false, - }, - // they're in sync - { - clientDirty: true, - clientUSN: 21, - clientEditedOn: 1541219321, - clientBody: "n1 body", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b2UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body", - serverPublic: false, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body", - expectedPublic: false, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: true, - }, - // they have the same usn but client is dirty - // not sure if this is a possible scenario but if it happens, the local copy will - // be uploaded to the server anyway. - { - clientDirty: true, - clientUSN: 21, - clientEditedOn: 1541219320, - clientBody: "n1 body client", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b2UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body server", - serverPublic: true, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219320, - expectedBody: "n1 body client", - expectedPublic: false, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - testutils.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, tc.clientBookUUID, tc.clientUSN, tc.addedOn, tc.clientEditedOn, tc.clientBody, tc.clientPublic, tc.clientDeleted, tc.clientDirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - // update all fields but uuid and bump usn - n := client.SyncFragNote{ - UUID: n1UUID, - BookUUID: tc.serverBookUUID, - USN: tc.serverUSN, - AddedOn: tc.addedOn, - EditedOn: tc.serverEditedOn, - Body: tc.serverBody, - Public: tc.serverPublic, - Deleted: tc.serverDeleted, - } - - if err := fullSyncNote(tx, n); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, fmt.Sprintf("note count mismatch for test case %d", idx)) - testutils.AssertEqualf(t, bookCount, 2, fmt.Sprintf("book count mismatch for test case %d", idx)) - - var n1 core.Note - testutils.MustScan(t, fmt.Sprintf("getting n1 for test case %d", idx), - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - - testutils.AssertEqual(t, n1.UUID, n.UUID, fmt.Sprintf("n1 UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.BookUUID, tc.expectedBookUUID, fmt.Sprintf("n1 BookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.USN, tc.expectedUSN, fmt.Sprintf("n1 USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.AddedOn, tc.expectedAddedOn, fmt.Sprintf("n1 AddedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.EditedOn, tc.expectedEditedOn, fmt.Sprintf("n1 EditedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Body, tc.expectedBody, fmt.Sprintf("n1 Body mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Public, tc.expectedPublic, fmt.Sprintf("n1 Public mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Deleted, tc.expectedDeleted, fmt.Sprintf("n1 Deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Dirty, tc.expectedDirty, fmt.Sprintf("n1 Dirty mismatch for test case %d", idx)) - }() - } - }) -} - -func TestFullSyncBook(t *testing.T) { - t.Run("exists on server only", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 555, "b1-label", true, false) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b2UUID := utils.GenerateUUID() - b := client.SyncFragBook{ - UUID: b2UUID, - USN: 1, - AddedOn: 1541108743, - Label: "b2-label", - Deleted: false, - } - - if err := fullSyncBook(tx, b); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 2, "book count mismatch") - - var b1, b2 core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), - &b1.UUID, &b1.USN, &b1.Label, &b1.Dirty, &b1.Deleted) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b2UUID), - &b2.UUID, &b2.USN, &b2.Label, &b2.Dirty, &b2.Deleted) - - testutils.AssertEqual(t, b1.UUID, b1UUID, "b1 UUID mismatch") - testutils.AssertEqual(t, b1.USN, 555, "b1 USN mismatch") - testutils.AssertEqual(t, b1.Label, "b1-label", "b1 Label mismatch") - testutils.AssertEqual(t, b1.Dirty, true, "b1 Dirty mismatch") - testutils.AssertEqual(t, b1.Deleted, false, "b1 Deleted mismatch") - - testutils.AssertEqual(t, b2.UUID, b2UUID, "b2 UUID mismatch") - testutils.AssertEqual(t, b2.USN, b.USN, "b2 USN mismatch") - testutils.AssertEqual(t, b2.Label, b.Label, "b2 Label mismatch") - testutils.AssertEqual(t, b2.Dirty, false, "b2 Dirty mismatch") - testutils.AssertEqual(t, b2.Deleted, b.Deleted, "b2 Deleted mismatch") - }) - - t.Run("exists on server and client", func(t *testing.T) { - testCases := []struct { - clientDirty bool - clientUSN int - clientLabel string - clientDeleted bool - serverUSN int - serverLabel string - serverDeleted bool - expectedUSN int - expectedLabel string - expectedDeleted bool - }{ - // server has higher usn and client is dirty - { - clientDirty: true, - clientUSN: 1, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "b2-label-updated", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "b2-label-updated", - expectedDeleted: false, - }, - { - clientDirty: true, - clientUSN: 1, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "", - serverDeleted: true, - expectedUSN: 3, - expectedLabel: "", - expectedDeleted: true, - }, - // server has higher usn and client is not dirty - { - clientDirty: false, - clientUSN: 1, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "b2-label-updated", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "b2-label-updated", - expectedDeleted: false, - }, - // they are in sync - { - clientDirty: false, - clientUSN: 3, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "b2-label", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "b2-label", - expectedDeleted: false, - }, - // they have the same usn but client is dirty - { - clientDirty: true, - clientUSN: 3, - clientLabel: "b2-label-client", - clientDeleted: false, - serverUSN: 3, - serverLabel: "b2-label", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "b2-label-client", - expectedDeleted: false, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, tc.clientUSN, tc.clientLabel, tc.clientDirty, tc.clientDeleted) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - // update all fields but uuid and bump usn - b := client.SyncFragBook{ - UUID: b1UUID, - USN: tc.serverUSN, - Label: tc.serverLabel, - Deleted: tc.serverDeleted, - } - - if err := fullSyncBook(tx, b); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, fmt.Sprintf("note count mismatch for test case %d", idx)) - testutils.AssertEqualf(t, bookCount, 1, fmt.Sprintf("book count mismatch for test case %d", idx)) - - var b1 core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), - &b1.UUID, &b1.USN, &b1.Label, &b1.Dirty, &b1.Deleted) - - testutils.AssertEqual(t, b1.UUID, b1UUID, fmt.Sprintf("b1 UUID mismatch for idx %d", idx)) - testutils.AssertEqual(t, b1.USN, tc.expectedUSN, fmt.Sprintf("b1 USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1.Label, tc.expectedLabel, fmt.Sprintf("b1 Label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1.Dirty, tc.clientDirty, fmt.Sprintf("b1 Dirty mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1.Deleted, tc.expectedDeleted, fmt.Sprintf("b1 Deleted mismatch for test case %d", idx)) - }() - } - }) -} - -func TestStepSyncNote(t *testing.T) { - t.Run("exists on server only", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - n := client.SyncFragNote{ - UUID: "n1-uuid", - BookUUID: b1UUID, - USN: 128, - AddedOn: 1541232118, - EditedOn: 1541219321, - Body: "n1-body", - Public: true, - Deleted: false, - } - - if err := stepSyncNote(tx, n); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - - var n1 core.Note - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - - testutils.AssertEqual(t, n1.UUID, n.UUID, "n1 UUID mismatch") - testutils.AssertEqual(t, n1.BookUUID, n.BookUUID, "n1 BookUUID mismatch") - testutils.AssertEqual(t, n1.USN, n.USN, "n1 USN mismatch") - testutils.AssertEqual(t, n1.AddedOn, n.AddedOn, "n1 AddedOn mismatch") - testutils.AssertEqual(t, n1.EditedOn, n.EditedOn, "n1 EditedOn mismatch") - testutils.AssertEqual(t, n1.Body, n.Body, "n1 Body mismatch") - testutils.AssertEqual(t, n1.Public, n.Public, "n1 Public mismatch") - testutils.AssertEqual(t, n1.Deleted, n.Deleted, "n1 Deleted mismatch") - testutils.AssertEqual(t, n1.Dirty, false, "n1 Dirty mismatch") - }) - - t.Run("exists on server and client", func(t *testing.T) { - b1UUID := utils.GenerateUUID() - b2UUID := utils.GenerateUUID() - - testCases := []struct { - addedOn int64 - clientUSN int - clientEditedOn int64 - clientBody string - clientPublic bool - clientDeleted bool - clientBookUUID string - clientDirty bool - serverUSN int - serverEditedOn int64 - serverBody string - serverPublic bool - serverDeleted bool - serverBookUUID string - expectedUSN int - expectedAddedOn int64 - expectedEditedOn int64 - expectedBody string - expectedPublic bool - expectedDeleted bool - expectedBookUUID string - expectedDirty bool - }{ - { - clientDirty: true, - clientUSN: 1, - clientEditedOn: 0, - clientBody: "n1 body", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: true, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: true, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: true, - }, - // if deleted locally, resurrect it - { - clientDirty: true, - clientUSN: 1, - clientEditedOn: 1541219321, - clientBody: "", - clientPublic: false, - clientDeleted: true, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: false, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: false, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: false, - }, - { - clientDirty: false, - clientUSN: 1, - clientEditedOn: 0, - clientBody: "n1 body", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: true, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: true, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: false, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") - testutils.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, tc.clientBookUUID, tc.clientUSN, tc.addedOn, tc.clientEditedOn, tc.clientBody, tc.clientPublic, tc.clientDeleted, tc.clientDirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - // update all fields but uuid and bump usn - n := client.SyncFragNote{ - UUID: n1UUID, - BookUUID: tc.serverBookUUID, - USN: tc.serverUSN, - AddedOn: tc.addedOn, - EditedOn: tc.serverEditedOn, - Body: tc.serverBody, - Public: tc.serverPublic, - Deleted: tc.serverDeleted, - } - - if err := stepSyncNote(tx, n); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, fmt.Sprintf("note count mismatch for test case %d", idx)) - testutils.AssertEqualf(t, bookCount, 2, fmt.Sprintf("book count mismatch for test case %d", idx)) - - var n1 core.Note - testutils.MustScan(t, fmt.Sprintf("getting n1 for test case %d", idx), - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), - &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Public, &n1.Deleted, &n1.Dirty) - - testutils.AssertEqual(t, n1.UUID, n.UUID, fmt.Sprintf("n1 UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.BookUUID, tc.expectedBookUUID, fmt.Sprintf("n1 BookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.USN, tc.expectedUSN, fmt.Sprintf("n1 USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.AddedOn, tc.expectedAddedOn, fmt.Sprintf("n1 AddedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.EditedOn, tc.expectedEditedOn, fmt.Sprintf("n1 EditedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Body, tc.expectedBody, fmt.Sprintf("n1 Body mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Public, tc.expectedPublic, fmt.Sprintf("n1 Public mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Deleted, tc.expectedDeleted, fmt.Sprintf("n1 Deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1.Dirty, tc.expectedDirty, fmt.Sprintf("n1 Dirty mismatch for test case %d", idx)) - }() - } - }) -} - -func TestStepSyncBook(t *testing.T) { - t.Run("exists on server only", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 555, "b1-label", true, false) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b2UUID := utils.GenerateUUID() - b := client.SyncFragBook{ - UUID: b2UUID, - USN: 1, - AddedOn: 1541108743, - Label: "b2-label", - Deleted: false, - } - - if err := stepSyncBook(tx, b); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 2, "book count mismatch") - - var b1, b2 core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), - &b1.UUID, &b1.USN, &b1.Label, &b1.Dirty, &b1.Deleted) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b2UUID), - &b2.UUID, &b2.USN, &b2.Label, &b2.Dirty, &b2.Deleted) - - testutils.AssertEqual(t, b1.UUID, b1UUID, "b1 UUID mismatch") - testutils.AssertEqual(t, b1.USN, 555, "b1 USN mismatch") - testutils.AssertEqual(t, b1.Label, "b1-label", "b1 Label mismatch") - testutils.AssertEqual(t, b1.Dirty, true, "b1 Dirty mismatch") - testutils.AssertEqual(t, b1.Deleted, false, "b1 Deleted mismatch") - - testutils.AssertEqual(t, b2.UUID, b2UUID, "b2 UUID mismatch") - testutils.AssertEqual(t, b2.USN, b.USN, "b2 USN mismatch") - testutils.AssertEqual(t, b2.Label, b.Label, "b2 Label mismatch") - testutils.AssertEqual(t, b2.Dirty, false, "b2 Dirty mismatch") - testutils.AssertEqual(t, b2.Deleted, b.Deleted, "b2 Deleted mismatch") - }) - - t.Run("exists on server and client", func(t *testing.T) { - testCases := []struct { - clientDirty bool - clientUSN int - clientLabel string - clientDeleted bool - serverUSN int - serverLabel string - serverDeleted bool - expectedUSN int - expectedLabel string - expectedDeleted bool - anotherBookLabel string - expectedAnotherBookLabel string - expectedAnotherBookDirty bool - }{ - { - clientDirty: true, - clientUSN: 1, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "b2-label-updated", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "b2-label-updated", - expectedDeleted: false, - anotherBookLabel: "foo", - expectedAnotherBookLabel: "foo", - expectedAnotherBookDirty: false, - }, - { - clientDirty: false, - clientUSN: 1, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "b2-label-updated", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "b2-label-updated", - expectedDeleted: false, - anotherBookLabel: "foo", - expectedAnotherBookLabel: "foo", - expectedAnotherBookDirty: false, - }, - { - clientDirty: false, - clientUSN: 1, - clientLabel: "b2-label", - clientDeleted: false, - serverUSN: 3, - serverLabel: "foo", - serverDeleted: false, - expectedUSN: 3, - expectedLabel: "foo", - expectedDeleted: false, - anotherBookLabel: "foo", - expectedAnotherBookLabel: "foo (2)", - expectedAnotherBookDirty: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, tc.clientUSN, tc.clientLabel, tc.clientDirty, tc.clientDeleted) - b2UUID := utils.GenerateUUID() - testutils.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b2UUID, 2, tc.anotherBookLabel, false, false) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - // update all fields but uuid and bump usn - b := client.SyncFragBook{ - UUID: b1UUID, - USN: tc.serverUSN, - Label: tc.serverLabel, - Deleted: tc.serverDeleted, - } - - if err := fullSyncBook(tx, b); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, fmt.Sprintf("note count mismatch for test case %d", idx)) - testutils.AssertEqualf(t, bookCount, 2, fmt.Sprintf("book count mismatch for test case %d", idx)) - - var b1Record, b2Record core.Book - testutils.MustScan(t, "getting b1Record", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), - &b1Record.UUID, &b1Record.USN, &b1Record.Label, &b1Record.Dirty, &b1Record.Deleted) - testutils.MustScan(t, "getting b2Record", - db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b2UUID), - &b2Record.UUID, &b2Record.USN, &b2Record.Label, &b2Record.Dirty, &b2Record.Deleted) - - testutils.AssertEqual(t, b1Record.UUID, b1UUID, fmt.Sprintf("b1Record UUID mismatch for idx %d", idx)) - testutils.AssertEqual(t, b1Record.USN, tc.expectedUSN, fmt.Sprintf("b1Record USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Label, tc.expectedLabel, fmt.Sprintf("b1Record Label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Dirty, tc.clientDirty, fmt.Sprintf("b1Record Dirty mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Deleted, tc.expectedDeleted, fmt.Sprintf("b1Record Deleted mismatch for test case %d", idx)) - - testutils.AssertEqual(t, b2Record.UUID, b2UUID, fmt.Sprintf("b2Record UUID mismatch for idx %d", idx)) - testutils.AssertEqual(t, b2Record.USN, 2, fmt.Sprintf("b2Record USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Label, tc.expectedAnotherBookLabel, fmt.Sprintf("b2Record Label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Dirty, tc.expectedAnotherBookDirty, fmt.Sprintf("b2Record Dirty mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Deleted, false, fmt.Sprintf("b2Record Deleted mismatch for test case %d", idx)) - }() - } - }) -} - -func TestMergeBook(t *testing.T) { - t.Run("insert, no duplicates", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - // test - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b1 := client.SyncFragBook{ - UUID: "b1-uuid", - USN: 12, - AddedOn: 1541108743, - Label: "b1-label", - Deleted: false, - } - - if err := mergeBook(tx, b1, modeInsert); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // execute - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - - var b1Record core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch") - testutils.AssertEqual(t, b1Record.Label, b1.Label, "b1 Label mismatch") - testutils.AssertEqual(t, b1Record.USN, b1.USN, "b1 USN mismatch") - }) - - t.Run("insert, 1 duplicate", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) - - // test - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b := client.SyncFragBook{ - UUID: "b2-uuid", - USN: 12, - AddedOn: 1541108743, - Label: "foo", - Deleted: false, - } - - if err := mergeBook(tx, b, modeInsert); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // execute - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 2, "book count mismatch") - - var b1Record, b2Record core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) - - testutils.AssertEqual(t, b1Record.Label, "foo (2)", "b1 Label mismatch") - testutils.AssertEqual(t, b1Record.USN, 1, "b1 USN mismatch") - testutils.AssertEqual(t, b1Record.Dirty, true, "b1 should have been marked dirty") - - testutils.AssertEqual(t, b2Record.Label, "foo", "b2 Label mismatch") - testutils.AssertEqual(t, b2Record.USN, 12, "b2 USN mismatch") - testutils.AssertEqual(t, b2Record.Dirty, false, "b2 Dirty mismatch") - }) - - t.Run("insert, 3 duplicates", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "foo (2)", true, false) - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b3-uuid", 3, "foo (3)", false, false) - - // test - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b := client.SyncFragBook{ - UUID: "b4-uuid", - USN: 12, - AddedOn: 1541108743, - Label: "foo", - Deleted: false, - } - - if err := mergeBook(tx, b, modeInsert); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // execute - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 4, "book count mismatch") - - var b1Record, b2Record, b3Record, b4Record core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) - testutils.MustScan(t, "getting b3", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b3-uuid"), - &b3Record.UUID, &b3Record.Label, &b3Record.USN, &b3Record.Dirty) - testutils.MustScan(t, "getting b4", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b4-uuid"), - &b4Record.UUID, &b4Record.Label, &b4Record.USN, &b4Record.Dirty) - - testutils.AssertEqual(t, b1Record.Label, "foo (4)", "b1 Label mismatch") - testutils.AssertEqual(t, b1Record.USN, 1, "b1 USN mismatch") - testutils.AssertEqual(t, b1Record.Dirty, true, "b1 Dirty mismatch") - - testutils.AssertEqual(t, b2Record.Label, "foo (2)", "b2 Label mismatch") - testutils.AssertEqual(t, b2Record.USN, 2, "b2 USN mismatch") - testutils.AssertEqual(t, b2Record.Dirty, true, "b2 Dirty mismatch") - - testutils.AssertEqual(t, b3Record.Label, "foo (3)", "b3 Label mismatch") - testutils.AssertEqual(t, b3Record.USN, 3, "b3 USN mismatch") - testutils.AssertEqual(t, b3Record.Dirty, false, "b3 Dirty mismatch") - - testutils.AssertEqual(t, b4Record.Label, "foo", "b4 Label mismatch") - testutils.AssertEqual(t, b4Record.USN, 12, "b4 USN mismatch") - testutils.AssertEqual(t, b4Record.Dirty, false, "b4 Dirty mismatch") - }) - - t.Run("update, no duplicates", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - // test - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 1, "b1-label", false, false) - - b1 := client.SyncFragBook{ - UUID: b1UUID, - USN: 12, - AddedOn: 1541108743, - Label: "b1-label-edited", - Deleted: false, - } - - if err := mergeBook(tx, b1, modeUpdate); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // execute - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - - var b1Record core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1UUID, "b1 UUID mismatch") - testutils.AssertEqual(t, b1Record.Label, "b1-label-edited", "b1 Label mismatch") - testutils.AssertEqual(t, b1Record.USN, 12, "b1 USN mismatch") - }) - - t.Run("update, 1 duplicate", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "bar", false, false) - - // test - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b := client.SyncFragBook{ - UUID: "b1-uuid", - USN: 12, - AddedOn: 1541108743, - Label: "bar", - Deleted: false, - } - - if err := mergeBook(tx, b, modeUpdate); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // execute - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 2, "book count mismatch") - - var b1Record, b2Record core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) - - testutils.AssertEqual(t, b1Record.Label, "bar", "b1 Label mismatch") - testutils.AssertEqual(t, b1Record.USN, 12, "b1 USN mismatch") - testutils.AssertEqual(t, b1Record.Dirty, false, "b1 Dirty mismatch") - - testutils.AssertEqual(t, b2Record.Label, "bar (2)", "b2 Label mismatch") - testutils.AssertEqual(t, b2Record.USN, 2, "b2 USN mismatch") - testutils.AssertEqual(t, b2Record.Dirty, true, "b2 Dirty mismatch") - }) - - t.Run("update, 3 duplicate", func(t *testing.T) { - // set uj - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "bar", false, false) - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b3-uuid", 3, "bar (2)", true, false) - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b4-uuid", 4, "bar (3)", false, false) - - // test - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - b := client.SyncFragBook{ - UUID: "b1-uuid", - USN: 12, - AddedOn: 1541108743, - Label: "bar", - Deleted: false, - } - - if err := mergeBook(tx, b, modeUpdate); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // execute - var noteCount, bookCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 0, "note count mismatch") - testutils.AssertEqualf(t, bookCount, 4, "book count mismatch") - - var b1Record, b2Record, b3Record, b4Record core.Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) - testutils.MustScan(t, "getting b3", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b3-uuid"), - &b3Record.UUID, &b3Record.Label, &b3Record.USN, &b3Record.Dirty) - testutils.MustScan(t, "getting b4", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b4-uuid"), - &b4Record.UUID, &b4Record.Label, &b4Record.USN, &b4Record.Dirty) - - testutils.AssertEqual(t, b1Record.Label, "bar", "b1 Label mismatch") - testutils.AssertEqual(t, b1Record.USN, 12, "b1 USN mismatch") - testutils.AssertEqual(t, b1Record.Dirty, false, "b1 Dirty mismatch") - - testutils.AssertEqual(t, b2Record.Label, "bar (4)", "b2 Label mismatch") - testutils.AssertEqual(t, b2Record.USN, 2, "b2 USN mismatch") - testutils.AssertEqual(t, b2Record.Dirty, true, "b2 Dirty mismatch") - - testutils.AssertEqual(t, b3Record.Label, "bar (2)", "b3 Label mismatch") - testutils.AssertEqual(t, b3Record.USN, 3, "b3 USN mismatch") - testutils.AssertEqual(t, b3Record.Dirty, true, "b3 Dirty mismatch") - - testutils.AssertEqual(t, b4Record.Label, "bar (3)", "b4 Label mismatch") - testutils.AssertEqual(t, b4Record.USN, 4, "b4 USN mismatch") - testutils.AssertEqual(t, b4Record.Dirty, false, "b4 Dirty mismatch") - }) -} - -func TestSaveServerState(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastSyncAt, int64(1231108742)) - testutils.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, 8) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - serverTime := int64(1541108743) - serverMaxUSN := 100 - - err = saveSyncState(tx, serverTime, serverMaxUSN) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var lastSyncedAt int64 - var lastMaxUSN int - - testutils.MustScan(t, "getting system value", - db.QueryRow("SELECT value FROM system WHERE key = ?", infra.SystemLastSyncAt), &lastSyncedAt) - testutils.MustScan(t, "getting system value", - db.QueryRow("SELECT value FROM system WHERE key = ?", infra.SystemLastMaxUSN), &lastMaxUSN) - - testutils.AssertEqual(t, lastSyncedAt, serverTime, "last synced at mismatch") - testutils.AssertEqual(t, lastMaxUSN, serverMaxUSN, "last max usn mismatch") -} - -// TestSendBooks tests that books are put to correct 'buckets' by running a test server and recording the -// uuid from the incoming data. It also tests that the uuid of the created books and book_uuids of their notes -// are updated accordingly based on the server response. -func TestSendBooks(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, 0) - - // should be ignored - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) - testutils.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b2-uuid", "b2-label", 2, false, false) - // should be created - testutils.MustExec(t, "inserting b3", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 0, false, true) - testutils.MustExec(t, "inserting b4", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b4-uuid", "b4-label", 0, false, true) - // should be only expunged locally without syncing to server - testutils.MustExec(t, "inserting b5", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b5-uuid", "b5-label", 0, true, true) - // should be deleted - testutils.MustExec(t, "inserting b6", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b6-uuid", "b6-label", 10, true, true) - // should be updated - testutils.MustExec(t, "inserting b7", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b7-uuid", "b7-label", 11, false, true) - testutils.MustExec(t, "inserting b8", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b8-uuid", "b8-label", 18, false, true) - - // some random notes - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 10, "n1 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", "b5-uuid", 10, "n2 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", "b6-uuid", 10, "n3 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n4-uuid", "b7-uuid", 10, "n4 body", 1541108743, false, false) - // notes that belong to the created book. Their book_uuid should be updated. - testutils.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", "b3-uuid", 10, "n5 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", "b3-uuid", 10, "n6 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n7", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n7-uuid", "b4-uuid", 10, "n7 body", 1541108743, false, false) - - var createdLabels []string - var updatesUUIDs []string - var deletedUUIDs []string - - // fire up a test server. It decrypts the payload for test purposes. - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/v2/books" && r.Method == "POST" { - var payload client.CreateBookPayload - - err := json.NewDecoder(r.Body).Decode(&payload) - if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) - return - } - - labelDec, err := crypt.AesGcmDecrypt(cipherKey, payload.Name) - if err != nil { - t.Fatalf(errors.Wrap(err, "decrypting label").Error()) - } - - labelDecStr := string(labelDec) - - createdLabels = append(createdLabels, labelDecStr) - - resp := client.CreateBookResp{ - Book: client.RespBook{ - UUID: fmt.Sprintf("server-%s-uuid", labelDecStr), - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - - p := strings.Split(r.URL.Path, "/") - if len(p) == 4 && p[0] == "" && p[1] == "v1" && p[2] == "books" { - if r.Method == "PATCH" { - uuid := p[3] - updatesUUIDs = append(updatesUUIDs, uuid) - - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{}")) - return - } else if r.Method == "DELETE" { - uuid := p[3] - deletedUUIDs = append(deletedUUIDs, uuid) - - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{}")) - return - } - } - - t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) - })) - defer ts.Close() - - ctx.APIEndpoint = ts.URL - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if _, err := sendBooks(ctx, tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - - // First, decrypt data so that they can be asserted - sort.SliceStable(createdLabels, func(i, j int) bool { - return strings.Compare(createdLabels[i], createdLabels[j]) < 0 - }) - - testutils.AssertDeepEqual(t, createdLabels, []string{"b3-label", "b4-label"}, "createdLabels mismatch") - testutils.AssertDeepEqual(t, updatesUUIDs, []string{"b7-uuid", "b8-uuid"}, "updatesUUIDs mismatch") - testutils.AssertDeepEqual(t, deletedUUIDs, []string{"b6-uuid"}, "deletedUUIDs mismatch") - - var b1, b2, b3, b4, b7, b8 core.Book - testutils.MustScan(t, "getting b1", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b1-label"), &b1.UUID, &b1.Dirty) - testutils.MustScan(t, "getting b2", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b2-label"), &b2.UUID, &b2.Dirty) - testutils.MustScan(t, "getting b3", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b3-label"), &b3.UUID, &b3.Dirty) - testutils.MustScan(t, "getting b4", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b4-label"), &b4.UUID, &b4.Dirty) - testutils.MustScan(t, "getting b7", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b7-label"), &b7.UUID, &b7.Dirty) - testutils.MustScan(t, "getting b8", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b8-label"), &b8.UUID, &b8.Dirty) - - var bookCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.AssertEqualf(t, bookCount, 6, "book count mismatch") - - testutils.AssertEqual(t, b1.Dirty, false, "b1 Dirty mismatch") - testutils.AssertEqual(t, b2.Dirty, false, "b2 Dirty mismatch") - testutils.AssertEqual(t, b3.Dirty, false, "b3 Dirty mismatch") - testutils.AssertEqual(t, b4.Dirty, false, "b4 Dirty mismatch") - testutils.AssertEqual(t, b7.Dirty, false, "b7 Dirty mismatch") - testutils.AssertEqual(t, b8.Dirty, false, "b8 Dirty mismatch") - testutils.AssertEqual(t, b1.UUID, "b1-uuid", "b1 UUID mismatch") - testutils.AssertEqual(t, b2.UUID, "b2-uuid", "b2 UUID mismatch") - // uuids of created books should have been updated - testutils.AssertEqual(t, b3.UUID, "server-b3-label-uuid", "b3 UUID mismatch") - testutils.AssertEqual(t, b4.UUID, "server-b4-label-uuid", "b4 UUID mismatch") - testutils.AssertEqual(t, b7.UUID, "b7-uuid", "b7 UUID mismatch") - testutils.AssertEqual(t, b8.UUID, "b8-uuid", "b8 UUID mismatch") - - var n1, n2, n3, n4, n5, n6, n7 core.Note - testutils.MustScan(t, "getting n1", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n1 body"), &n1.BookUUID) - testutils.MustScan(t, "getting n2", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n2 body"), &n2.BookUUID) - testutils.MustScan(t, "getting n3", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n3 body"), &n3.BookUUID) - testutils.MustScan(t, "getting n4", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n4 body"), &n4.BookUUID) - testutils.MustScan(t, "getting n5", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n5 body"), &n5.BookUUID) - testutils.MustScan(t, "getting n6", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n6 body"), &n6.BookUUID) - testutils.MustScan(t, "getting n7", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n7 body"), &n7.BookUUID) - testutils.AssertEqual(t, n1.BookUUID, "b1-uuid", "n1 bookUUID mismatch") - testutils.AssertEqual(t, n2.BookUUID, "b5-uuid", "n2 bookUUID mismatch") - testutils.AssertEqual(t, n3.BookUUID, "b6-uuid", "n3 bookUUID mismatch") - testutils.AssertEqual(t, n4.BookUUID, "b7-uuid", "n4 bookUUID mismatch") - testutils.AssertEqual(t, n5.BookUUID, "server-b3-label-uuid", "n5 bookUUID mismatch") - testutils.AssertEqual(t, n6.BookUUID, "server-b3-label-uuid", "n6 bookUUID mismatch") - testutils.AssertEqual(t, n7.BookUUID, "server-b4-label-uuid", "n7 bookUUID mismatch") -} - -func TestSendBooks_isBehind(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/v2/books" && r.Method == "POST" { - var payload client.CreateBookPayload - - err := json.NewDecoder(r.Body).Decode(&payload) - if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) - return - } - - resp := client.CreateBookResp{ - Book: client.RespBook{ - USN: 11, - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - - p := strings.Split(r.URL.Path, "/") - if len(p) == 4 && p[0] == "" && p[1] == "v1" && p[2] == "books" { - if r.Method == "PATCH" { - resp := client.UpdateBookResp{ - Book: client.RespBook{ - USN: 11, - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } else if r.Method == "DELETE" { - resp := client.DeleteBookResp{ - Book: client.RespBook{ - USN: 11, - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - } - - t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) - })) - defer ts.Close() - - t.Run("create book", func(t *testing.T) { - testCases := []struct { - systemLastMaxUSN int - expectedIsBehind bool - }{ - { - systemLastMaxUSN: 10, - expectedIsBehind: false, - }, - { - systemLastMaxUSN: 9, - expectedIsBehind: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - ctx.APIEndpoint = ts.URL - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, tc.systemLastMaxUSN) - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 0, false, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - isBehind, err := sendBooks(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) - }() - } - }) - - t.Run("delete book", func(t *testing.T) { - testCases := []struct { - systemLastMaxUSN int - expectedIsBehind bool - }{ - { - systemLastMaxUSN: 10, - expectedIsBehind: false, - }, - { - systemLastMaxUSN: 9, - expectedIsBehind: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - ctx.APIEndpoint = ts.URL - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, tc.systemLastMaxUSN) - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, true, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - isBehind, err := sendBooks(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) - }() - } - }) - - t.Run("update book", func(t *testing.T) { - testCases := []struct { - systemLastMaxUSN int - expectedIsBehind bool - }{ - { - systemLastMaxUSN: 10, - expectedIsBehind: false, - }, - { - systemLastMaxUSN: 9, - expectedIsBehind: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - ctx.APIEndpoint = ts.URL - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, tc.systemLastMaxUSN) - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 11, false, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - isBehind, err := sendBooks(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) - }() - } - }) -} - -// TestSendNotes tests that notes are put to correct 'buckets' by running a test server and recording the -// uuid from the incoming data. -func TestSendNotes(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, 0) - - b1UUID := "b1-uuid" - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 1, false, false) - - // should be ignored - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1-body", 1541108743, false, false) - // should be created - testutils.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 0, "n2-body", 1541108743, false, true) - // should be updated - testutils.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", b1UUID, 11, "n3-body", 1541108743, false, true) - // should be only expunged locally without syncing to server - testutils.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n4-uuid", b1UUID, 0, "n4-body", 1541108743, true, true) - // should be deleted - testutils.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", b1UUID, 17, "n5-body", 1541108743, true, true) - // should be created - testutils.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", b1UUID, 0, "n6-body", 1541108743, false, true) - // should be ignored - testutils.MustExec(t, "inserting n7", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n7-uuid", b1UUID, 12, "n7-body", 1541108743, false, false) - // should be updated - testutils.MustExec(t, "inserting n8", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n8-uuid", b1UUID, 17, "n8-body", 1541108743, false, true) - // should be deleted - testutils.MustExec(t, "inserting n9", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n9-uuid", b1UUID, 17, "n9-body", 1541108743, true, true) - // should be created - testutils.MustExec(t, "inserting n10", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n10-uuid", b1UUID, 0, "n10-body", 1541108743, false, true) - - var createdBodys []string - var updatedUUIDs []string - var deletedUUIDs []string - - // fire up a test server. It decrypts the payload for test purposes. - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/v2/notes" && r.Method == "POST" { - var payload client.CreateNotePayload - - err := json.NewDecoder(r.Body).Decode(&payload) - if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) - return - } - - bodyDec, err := crypt.AesGcmDecrypt(cipherKey, payload.Body) - if err != nil { - t.Fatalf(errors.Wrap(err, "decrypting body").Error()) - } - bodyDecStr := string(bodyDec) - - createdBodys = append(createdBodys, bodyDecStr) - - resp := client.CreateNoteResp{ - Result: client.RespNote{ - UUID: fmt.Sprintf("server-%s-uuid", bodyDecStr), - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - - p := strings.Split(r.URL.Path, "/") - if len(p) == 4 && p[0] == "" && p[1] == "v1" && p[2] == "notes" { - if r.Method == "PATCH" { - uuid := p[3] - updatedUUIDs = append(updatedUUIDs, uuid) - - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{}")) - return - } else if r.Method == "DELETE" { - uuid := p[3] - deletedUUIDs = append(deletedUUIDs, uuid) - - w.Header().Set("Content-Type", "application/json") - w.Write([]byte("{}")) - return - } - } - - t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) - })) - defer ts.Close() - - ctx.APIEndpoint = ts.URL - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if _, err := sendNotes(ctx, tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - sort.SliceStable(createdBodys, func(i, j int) bool { - return strings.Compare(createdBodys[i], createdBodys[j]) < 0 - }) - - testutils.AssertDeepEqual(t, createdBodys, []string{"n10-body", "n2-body", "n6-body"}, "createdBodys mismatch") - testutils.AssertDeepEqual(t, updatedUUIDs, []string{"n3-uuid", "n8-uuid"}, "updatedUUIDs mismatch") - testutils.AssertDeepEqual(t, deletedUUIDs, []string{"n5-uuid", "n9-uuid"}, "deletedUUIDs mismatch") - - var noteCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.AssertEqualf(t, noteCount, 7, "note count mismatch") - - var n1, n2, n3, n6, n7, n8, n10 core.Note - testutils.MustScan(t, "getting n1", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n1-body"), &n1.UUID, &n1.AddedOn, &n1.Dirty) - testutils.MustScan(t, "getting n2", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n2-body"), &n2.UUID, &n2.AddedOn, &n2.Dirty) - testutils.MustScan(t, "getting n3", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n3-body"), &n3.UUID, &n3.AddedOn, &n3.Dirty) - testutils.MustScan(t, "getting n6", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n6-body"), &n6.UUID, &n6.AddedOn, &n6.Dirty) - testutils.MustScan(t, "getting n7", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n7-body"), &n7.UUID, &n7.AddedOn, &n7.Dirty) - testutils.MustScan(t, "getting n8", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n8-body"), &n8.UUID, &n8.AddedOn, &n8.Dirty) - testutils.MustScan(t, "getting n10", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n10-body"), &n10.UUID, &n10.AddedOn, &n10.Dirty) - - testutils.AssertEqualf(t, noteCount, 7, "note count mismatch") - - testutils.AssertEqual(t, n1.Dirty, false, "n1 Dirty mismatch") - testutils.AssertEqual(t, n2.Dirty, false, "n2 Dirty mismatch") - testutils.AssertEqual(t, n3.Dirty, false, "n3 Dirty mismatch") - testutils.AssertEqual(t, n6.Dirty, false, "n6 Dirty mismatch") - testutils.AssertEqual(t, n7.Dirty, false, "n7 Dirty mismatch") - testutils.AssertEqual(t, n8.Dirty, false, "n8 Dirty mismatch") - testutils.AssertEqual(t, n10.Dirty, false, "n10 Dirty mismatch") - - testutils.AssertEqual(t, n1.AddedOn, int64(1541108743), "n1 AddedOn mismatch") - testutils.AssertEqual(t, n2.AddedOn, int64(1541108743), "n2 AddedOn mismatch") - testutils.AssertEqual(t, n3.AddedOn, int64(1541108743), "n3 AddedOn mismatch") - testutils.AssertEqual(t, n6.AddedOn, int64(1541108743), "n6 AddedOn mismatch") - testutils.AssertEqual(t, n7.AddedOn, int64(1541108743), "n7 AddedOn mismatch") - testutils.AssertEqual(t, n8.AddedOn, int64(1541108743), "n8 AddedOn mismatch") - testutils.AssertEqual(t, n10.AddedOn, int64(1541108743), "n10 AddedOn mismatch") - - // UUIDs of created notes should have been updated with those from the server response - testutils.AssertEqual(t, n1.UUID, "n1-uuid", "n1 UUID mismatch") - testutils.AssertEqual(t, n2.UUID, "server-n2-body-uuid", "n2 UUID mismatch") - testutils.AssertEqual(t, n3.UUID, "n3-uuid", "n3 UUID mismatch") - testutils.AssertEqual(t, n6.UUID, "server-n6-body-uuid", "n6 UUID mismatch") - testutils.AssertEqual(t, n7.UUID, "n7-uuid", "n7 UUID mismatch") - testutils.AssertEqual(t, n8.UUID, "n8-uuid", "n8 UUID mismatch") - testutils.AssertEqual(t, n10.UUID, "server-n10-body-uuid", "n10 UUID mismatch") -} - -func TestSendNotes_addedOn(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, 0) - - // should be created - b1UUID := "b1-uuid" - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 0, "n1-body", 1541108743, false, true) - - // fire up a test server. It decrypts the payload for test purposes. - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/v2/notes" && r.Method == "POST" { - resp := client.CreateNoteResp{ - Result: client.RespNote{ - UUID: utils.GenerateUUID(), - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - - t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) - })) - defer ts.Close() - - ctx.APIEndpoint = ts.URL - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if _, err := sendNotes(ctx, tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var n1 core.Note - testutils.MustScan(t, "getting n1", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n1-body"), &n1.UUID, &n1.AddedOn, &n1.Dirty) - testutils.AssertEqual(t, n1.AddedOn, int64(1541108743), "n1 AddedOn mismatch") -} - -func TestSendNotes_isBehind(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/v1/notes" && r.Method == "POST" { - var payload client.CreateBookPayload - - err := json.NewDecoder(r.Body).Decode(&payload) - if err != nil { - t.Fatalf(errors.Wrap(err, "decoding payload in the test server").Error()) - return - } - - resp := client.CreateNoteResp{ - Result: client.RespNote{ - USN: 11, - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - - p := strings.Split(r.URL.Path, "/") - if len(p) == 4 && p[0] == "" && p[1] == "v1" && p[2] == "notes" { - if r.Method == "PATCH" { - resp := client.UpdateNoteResp{ - Result: client.RespNote{ - USN: 11, - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } else if r.Method == "DELETE" { - resp := client.DeleteNoteResp{ - Result: client.RespNote{ - USN: 11, - }, - } - - w.Header().Set("Content-Type", "application/json") - if err := json.NewEncoder(w).Encode(resp); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - return - } - } - - t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) - })) - defer ts.Close() - - t.Run("create note", func(t *testing.T) { - testCases := []struct { - systemLastMaxUSN int - expectedIsBehind bool - }{ - { - systemLastMaxUSN: 10, - expectedIsBehind: false, - }, - { - systemLastMaxUSN: 9, - expectedIsBehind: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - ctx.APIEndpoint = ts.URL - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, tc.systemLastMaxUSN) - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 1, "n1 body", 1541108743, false, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - isBehind, err := sendNotes(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) - }() - } - }) - - t.Run("delete note", func(t *testing.T) { - testCases := []struct { - systemLastMaxUSN int - expectedIsBehind bool - }{ - { - systemLastMaxUSN: 10, - expectedIsBehind: false, - }, - { - systemLastMaxUSN: 9, - expectedIsBehind: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - ctx.APIEndpoint = ts.URL - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, tc.systemLastMaxUSN) - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 2, "n1 body", 1541108743, true, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - isBehind, err := sendNotes(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) - }() - } - }) - - t.Run("update note", func(t *testing.T) { - testCases := []struct { - systemLastMaxUSN int - expectedIsBehind bool - }{ - { - systemLastMaxUSN: 10, - expectedIsBehind: false, - }, - { - systemLastMaxUSN: 9, - expectedIsBehind: true, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - testutils.Login(t, &ctx) - ctx.APIEndpoint = ts.URL - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemLastMaxUSN, tc.systemLastMaxUSN) - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 8, "n1 body", 1541108743, false, true) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - isBehind, err := sendNotes(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) - }() - } - }) -} - -func TestMergeNote(t *testing.T) { - b1UUID := "b1-uuid" - b2UUID := "b2-uuid" - - testCases := []struct { - addedOn int64 - clientUSN int - clientEditedOn int64 - clientBody string - clientPublic bool - clientDeleted bool - clientBookUUID string - clientDirty bool - serverUSN int - serverEditedOn int64 - serverBody string - serverPublic bool - serverDeleted bool - serverBookUUID string - expectedUSN int - expectedAddedOn int64 - expectedEditedOn int64 - expectedBody string - expectedPublic bool - expectedDeleted bool - expectedBookUUID string - expectedDirty bool - }{ - { - clientDirty: false, - clientUSN: 1, - clientEditedOn: 0, - clientBody: "n1 body", - clientPublic: false, - clientDeleted: false, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: true, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: true, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: false, - }, - // deleted locally and edited on server - { - clientDirty: true, - clientUSN: 1, - clientEditedOn: 1541219321, - clientBody: "", - clientPublic: false, - clientDeleted: true, - clientBookUUID: b1UUID, - addedOn: 1541232118, - serverUSN: 21, - serverEditedOn: 1541219321, - serverBody: "n1 body edited", - serverPublic: false, - serverDeleted: false, - serverBookUUID: b2UUID, - expectedUSN: 21, - expectedAddedOn: 1541232118, - expectedEditedOn: 1541219321, - expectedBody: "n1 body edited", - expectedPublic: false, - expectedDeleted: false, - expectedBookUUID: b2UUID, - expectedDirty: false, - }, - } - - for idx, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b1UUID, "b1-label", 5, false) - testutils.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b2UUID, "b2-label", 6, false) - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, b1UUID, tc.clientUSN, tc.addedOn, tc.clientEditedOn, tc.clientBody, tc.clientPublic, tc.clientDeleted, tc.clientDirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - // update all fields but uuid and bump usn - fragNote := client.SyncFragNote{ - UUID: n1UUID, - BookUUID: tc.serverBookUUID, - USN: tc.serverUSN, - AddedOn: tc.addedOn, - EditedOn: tc.serverEditedOn, - Body: tc.serverBody, - Public: tc.serverPublic, - Deleted: tc.serverDeleted, - } - var localNote core.Note - testutils.MustScan(t, fmt.Sprintf("getting localNote for test case %d", idx), - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n1UUID), - &localNote.UUID, &localNote.BookUUID, &localNote.USN, &localNote.AddedOn, &localNote.EditedOn, &localNote.Body, &localNote.Public, &localNote.Deleted, &localNote.Dirty) - - if err := mergeNote(tx, fragNote, localNote); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var noteCount, bookCount int - testutils.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, noteCount, 1, fmt.Sprintf("note count mismatch for test case %d", idx)) - testutils.AssertEqualf(t, bookCount, 2, fmt.Sprintf("book count mismatch for test case %d", idx)) - - var n1Record core.Note - testutils.MustScan(t, fmt.Sprintf("getting n1Record for test case %d", idx), - db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty FROM notes WHERE uuid = ?", n1UUID), - &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.Body, &n1Record.Public, &n1Record.Deleted, &n1Record.Dirty) - var b1Record core.Book - testutils.MustScan(t, "getting b1Record for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) - var b2Record core.Book - testutils.MustScan(t, "getting b2Record for test case", - db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b2UUID), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1UUID, fmt.Sprintf("b1Record UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Label, "b1-label", fmt.Sprintf("b1Record Label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.USN, 5, fmt.Sprintf("b1Record USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Dirty, false, fmt.Sprintf("b1Record Dirty mismatch for test case %d", idx)) - - testutils.AssertEqual(t, b2Record.UUID, b2UUID, fmt.Sprintf("b2Record UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Label, "b2-label", fmt.Sprintf("b2Record Label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.USN, 6, fmt.Sprintf("b2Record USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Dirty, false, fmt.Sprintf("b2Record Dirty mismatch for test case %d", idx)) - - testutils.AssertEqual(t, n1Record.UUID, n1UUID, fmt.Sprintf("n1Record UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.BookUUID, tc.expectedBookUUID, fmt.Sprintf("n1Record BookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.USN, tc.expectedUSN, fmt.Sprintf("n1Record USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.AddedOn, tc.expectedAddedOn, fmt.Sprintf("n1Record AddedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.EditedOn, tc.expectedEditedOn, fmt.Sprintf("n1Record EditedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Body, tc.expectedBody, fmt.Sprintf("n1Record Body mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Public, tc.expectedPublic, fmt.Sprintf("n1Record Public mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Deleted, tc.expectedDeleted, fmt.Sprintf("n1Record Deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Dirty, tc.expectedDirty, fmt.Sprintf("n1Record Dirty mismatch for test case %d", idx)) - }() - } -} - -func TestCheckBookPristine(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", "b1-uuid", "b1-label", 5, false) - testutils.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", "b2-uuid", "b2-label", 6, false) - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 1541108743, "n1 body", false) - testutils.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n2-uuid", "b1-uuid", 1541108743, "n2 body", false) - testutils.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n3-uuid", "b1-uuid", 1541108743, "n3 body", true) - testutils.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n4-uuid", "b2-uuid", 1541108743, "n4 body", false) - testutils.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n5-uuid", "b2-uuid", 1541108743, "n5 body", false) - - t.Run("b1", func(t *testing.T) { - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - got, err := checkNotesPristine(tx, "b1-uuid") - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, got, false, "b1 should not be pristine") - }) - - t.Run("b2", func(t *testing.T) { - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - got, err := checkNotesPristine(tx, "b2-uuid") - if err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - testutils.AssertEqual(t, got, true, "b2 should be pristine") - }) -} - -func TestCheckNoteInList(t *testing.T) { - list := syncList{ - Notes: map[string]client.SyncFragNote{ - "n1-uuid": client.SyncFragNote{ - UUID: "n1-uuid", - }, - "n2-uuid": client.SyncFragNote{ - UUID: "n2-uuid", - }, - }, - Books: map[string]client.SyncFragBook{ - "b1-uuid": client.SyncFragBook{ - UUID: "b1-uuid", - }, - "b2-uuid": client.SyncFragBook{ - UUID: "b2-uuid", - }, - }, - ExpungedNotes: map[string]bool{ - "n3-uuid": true, - "n4-uuid": true, - }, - ExpungedBooks: map[string]bool{ - "b3-uuid": true, - "b4-uuid": true, - }, - MaxUSN: 1, - MaxCurrentTime: 2, - } - - testCases := []struct { - uuid string - expected bool - }{ - { - uuid: "n1-uuid", - expected: true, - }, - { - uuid: "n2-uuid", - expected: true, - }, - { - uuid: "n3-uuid", - expected: true, - }, - { - uuid: "n4-uuid", - expected: true, - }, - { - uuid: "nonexistent-note-uuid", - expected: false, - }, - } - - for idx, tc := range testCases { - got := checkNoteInList(tc.uuid, &list) - testutils.AssertEqual(t, got, tc.expected, fmt.Sprintf("result mismatch for test case %d", idx)) - } -} - -func TestCheckBookInList(t *testing.T) { - list := syncList{ - Notes: map[string]client.SyncFragNote{ - "n1-uuid": client.SyncFragNote{ - UUID: "n1-uuid", - }, - "n2-uuid": client.SyncFragNote{ - UUID: "n2-uuid", - }, - }, - Books: map[string]client.SyncFragBook{ - "b1-uuid": client.SyncFragBook{ - UUID: "b1-uuid", - }, - "b2-uuid": client.SyncFragBook{ - UUID: "b2-uuid", - }, - }, - ExpungedNotes: map[string]bool{ - "n3-uuid": true, - "n4-uuid": true, - }, - ExpungedBooks: map[string]bool{ - "b3-uuid": true, - "b4-uuid": true, - }, - MaxUSN: 1, - MaxCurrentTime: 2, - } - - testCases := []struct { - uuid string - expected bool - }{ - { - uuid: "b1-uuid", - expected: true, - }, - { - uuid: "b2-uuid", - expected: true, - }, - { - uuid: "b3-uuid", - expected: true, - }, - { - uuid: "b4-uuid", - expected: true, - }, - { - uuid: "nonexistent-book-uuid", - expected: false, - }, - } - - for idx, tc := range testCases { - got := checkBookInList(tc.uuid, &list) - testutils.AssertEqual(t, got, tc.expected, fmt.Sprintf("result mismatch for test case %d", idx)) - } -} - -func TestCleanLocalNotes(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - list := syncList{ - Notes: map[string]client.SyncFragNote{ - "n1-uuid": client.SyncFragNote{ - UUID: "n1-uuid", - }, - "n2-uuid": client.SyncFragNote{ - UUID: "n2-uuid", - }, - }, - Books: map[string]client.SyncFragBook{ - "b1-uuid": client.SyncFragBook{ - UUID: "b1-uuid", - }, - "b2-uuid": client.SyncFragBook{ - UUID: "b2-uuid", - }, - }, - ExpungedNotes: map[string]bool{ - "n3-uuid": true, - "n4-uuid": true, - }, - ExpungedBooks: map[string]bool{ - "b3-uuid": true, - "b4-uuid": true, - }, - MaxUSN: 1, - MaxCurrentTime: 2, - } - - b1UUID := "b1-uuid" - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 1, false, false) - - // exists in the list - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) - testutils.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 0, "n2 body", 1541108743, false, true) - // non-existent in the list but in valid state - // (created in the cli and hasn't been uploaded) - testutils.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", b1UUID, 0, "n6 body", 1541108743, false, true) - // non-existent in the list and in an invalid state - testutils.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", b1UUID, 7, "n5 body", 1541108743, true, true) - testutils.MustExec(t, "inserting n9", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n9-uuid", b1UUID, 17, "n9 body", 1541108743, true, false) - testutils.MustExec(t, "inserting n10", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n10-uuid", b1UUID, 0, "n10 body", 1541108743, false, false) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := cleanLocalNotes(tx, &list); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.AssertEqual(t, noteCount, 3, "note count mismatch") - - var n1, n2, n6 core.Note - testutils.MustScan(t, "getting n1", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", "n1-uuid"), &n1.Dirty) - testutils.MustScan(t, "getting n2", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", "n2-uuid"), &n2.Dirty) - testutils.MustScan(t, "getting n6", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", "n6-uuid"), &n6.Dirty) -} - -func TestCleanLocalBooks(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../../tmp", "../../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - list := syncList{ - Notes: map[string]client.SyncFragNote{ - "n1-uuid": client.SyncFragNote{ - UUID: "n1-uuid", - }, - "n2-uuid": client.SyncFragNote{ - UUID: "n2-uuid", - }, - }, - Books: map[string]client.SyncFragBook{ - "b1-uuid": client.SyncFragBook{ - UUID: "b1-uuid", - }, - "b2-uuid": client.SyncFragBook{ - UUID: "b2-uuid", - }, - }, - ExpungedNotes: map[string]bool{ - "n3-uuid": true, - "n4-uuid": true, - }, - ExpungedBooks: map[string]bool{ - "b3-uuid": true, - "b4-uuid": true, - }, - MaxUSN: 1, - MaxCurrentTime: 2, - } - - // existent in the server - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) - testutils.MustExec(t, "inserting b3", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 0, false, true) - // non-existent in the server but in valid state - testutils.MustExec(t, "inserting b5", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b5-uuid", "b5-label", 0, true, true) - // non-existent in the server and in an invalid state - testutils.MustExec(t, "inserting b6", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b6-uuid", "b6-label", 10, true, true) - testutils.MustExec(t, "inserting b7", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b7-uuid", "b7-label", 11, false, false) - testutils.MustExec(t, "inserting b8", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b8-uuid", "b8-label", 0, false, false) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := cleanLocalBooks(tx, &list); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var bookCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.AssertEqual(t, bookCount, 3, "note count mismatch") - - var b1, b3, b5 core.Book - testutils.MustScan(t, "getting b1", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b1-uuid"), &b1.Label) - testutils.MustScan(t, "getting b3", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b3-uuid"), &b3.Label) - testutils.MustScan(t, "getting b5", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b5-uuid"), &b5.Label) -} diff --git a/cli/cmd/version/version.go b/cli/cmd/version/version.go deleted file mode 100644 index 27b530e8..00000000 --- a/cli/cmd/version/version.go +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package version - -import ( - "fmt" - - "github.com/dnote/dnote/cli/infra" - "github.com/spf13/cobra" -) - -// NewCmd returns a new version command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "version", - Short: "Print the version number of Dnote", - Long: "Print the version number of Dnote", - Run: func(cmd *cobra.Command, args []string) { - fmt.Printf("dnote %s\n", ctx.Version) - }, - } - - return cmd -} diff --git a/cli/cmd/view/view.go b/cli/cmd/view/view.go deleted file mode 100644 index b8a55caa..00000000 --- a/cli/cmd/view/view.go +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package view - -import ( - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/pkg/errors" - "github.com/spf13/cobra" - - "github.com/dnote/dnote/cli/cmd/cat" - "github.com/dnote/dnote/cli/cmd/ls" -) - -var example = ` - * View all books - dnote view - - * List notes in a book - dnote view javascript - - * View a particular note in a book - dnote view javascript 0 - ` - -func preRun(cmd *cobra.Command, args []string) error { - if len(args) > 2 { - return errors.New("Incorrect number of argument") - } - - return nil -} - -// NewCmd returns a new view command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { - cmd := &cobra.Command{ - Use: "view ", - Aliases: []string{"v"}, - Short: "List books, notes or view a content", - Example: example, - RunE: newRun(ctx), - PreRunE: preRun, - } - - return cmd -} - -func newRun(ctx infra.DnoteCtx) core.RunEFunc { - return func(cmd *cobra.Command, args []string) error { - var run core.RunEFunc - - if len(args) <= 1 { - run = ls.NewRun(ctx) - } else if len(args) == 2 { - run = cat.NewRun(ctx) - } else { - return errors.New("Incorrect number of arguments") - } - - return run(cmd, args) - } -} diff --git a/cli/core/core.go b/cli/core/core.go deleted file mode 100644 index db33ddf6..00000000 --- a/cli/core/core.go +++ /dev/null @@ -1,358 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package core - -import ( - "database/sql" - "fmt" - "io/ioutil" - "os" - "os/exec" - "strconv" - "strings" - "time" - - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" - "github.com/satori/go.uuid" - "github.com/spf13/cobra" - "gopkg.in/yaml.v2" -) - -var ( - // ConfigFilename is the name of the config file - ConfigFilename = "dnoterc" - // TmpContentFilename is the name of the temporary file that holds editor input - TmpContentFilename = "DNOTE_TMPCONTENT.md" -) - -// RunEFunc is a function type of dnote commands -type RunEFunc func(*cobra.Command, []string) error - -// GetConfigPath returns the path to the dnote config file -func GetConfigPath(ctx infra.DnoteCtx) string { - return fmt.Sprintf("%s/%s", ctx.DnoteDir, ConfigFilename) -} - -// GetDnoteTmpContentPath returns the path to the temporary file containing -// content being added or edited -func GetDnoteTmpContentPath(ctx infra.DnoteCtx) string { - return fmt.Sprintf("%s/%s", ctx.DnoteDir, TmpContentFilename) -} - -// GetBookUUID returns a uuid of a book given a label -func GetBookUUID(ctx infra.DnoteCtx, label string) (string, error) { - db := ctx.DB - - var ret string - err := db.QueryRow("SELECT uuid FROM books WHERE label = ?", label).Scan(&ret) - if err == sql.ErrNoRows { - return ret, errors.Errorf("book '%s' not found", label) - } else if err != nil { - return ret, errors.Wrap(err, "querying the book") - } - - return ret, nil -} - -// getEditorCommand returns the system's editor command with appropriate flags, -// if necessary, to make the command wait until editor is close to exit. -func getEditorCommand() string { - editor := os.Getenv("EDITOR") - - var ret string - - switch editor { - case "atom": - ret = "atom -w" - case "subl": - ret = "subl -n -w" - case "mate": - ret = "mate -w" - case "vim": - ret = "vim" - case "nano": - ret = "nano" - case "emacs": - ret = "emacs" - case "nvim": - ret = "nvim" - default: - ret = "vi" - } - - return ret -} - -// InitFiles creates, if necessary, the dnote directory and files inside -func InitFiles(ctx infra.DnoteCtx) error { - if err := initDnoteDir(ctx); err != nil { - return errors.Wrap(err, "creating the dnote dir") - } - if err := initConfigFile(ctx); err != nil { - return errors.Wrap(err, "generating the config file") - } - - return nil -} - -// initConfigFile populates a new config file if it does not exist yet -func initConfigFile(ctx infra.DnoteCtx) error { - path := GetConfigPath(ctx) - - if utils.FileExists(path) { - return nil - } - - editor := getEditorCommand() - - config := infra.Config{ - Editor: editor, - } - - b, err := yaml.Marshal(config) - if err != nil { - return errors.Wrap(err, "marshalling config into YAML") - } - - err = ioutil.WriteFile(path, b, 0644) - if err != nil { - return errors.Wrap(err, "writing the config file") - } - - return nil -} - -// initDnoteDir initializes dnote directory if it does not exist yet -func initDnoteDir(ctx infra.DnoteCtx) error { - path := ctx.DnoteDir - - if utils.FileExists(path) { - return nil - } - - if err := os.MkdirAll(path, 0755); err != nil { - return errors.Wrap(err, "Failed to create dnote directory") - } - - return nil -} - -// WriteConfig writes the config to the config file -func WriteConfig(ctx infra.DnoteCtx, config infra.Config) error { - d, err := yaml.Marshal(config) - if err != nil { - return errors.Wrap(err, "marhsalling config") - } - - configPath := GetConfigPath(ctx) - - err = ioutil.WriteFile(configPath, d, 0644) - if err != nil { - errors.Wrap(err, "writing the config file") - } - - return nil -} - -// LogAction logs action and updates the last_action -func LogAction(tx *sql.Tx, schema int, actionType, data string, timestamp int64) error { - uuid := uuid.NewV4().String() - - _, err := tx.Exec(`INSERT INTO actions (uuid, schema, type, data, timestamp) - VALUES (?, ?, ?, ?, ?)`, uuid, schema, actionType, data, timestamp) - if err != nil { - return errors.Wrap(err, "inserting an action") - } - - _, err = tx.Exec("UPDATE system SET value = ? WHERE key = ?", timestamp, "last_action") - if err != nil { - return errors.Wrap(err, "updating last_action") - } - - return nil -} - -// ReadConfig reads the config file -func ReadConfig(ctx infra.DnoteCtx) (infra.Config, error) { - var ret infra.Config - - configPath := GetConfigPath(ctx) - b, err := ioutil.ReadFile(configPath) - if err != nil { - return ret, errors.Wrap(err, "reading config file") - } - - err = yaml.Unmarshal(b, &ret) - if err != nil { - return ret, errors.Wrap(err, "unmarshalling config") - } - - return ret, nil -} - -// SanitizeContent sanitizes note content -func SanitizeContent(s string) string { - var ret string - - ret = strings.Trim(s, " ") - - // Remove newline at the end of the file because POSIX defines a line as - // characters followed by a newline - ret = strings.TrimSuffix(ret, "\n") - ret = strings.TrimSuffix(ret, "\r\n") - - return ret -} - -func newEditorCmd(ctx infra.DnoteCtx, fpath string) (*exec.Cmd, error) { - config, err := ReadConfig(ctx) - if err != nil { - return nil, errors.Wrap(err, "reading config") - } - - args := strings.Fields(config.Editor) - args = append(args, fpath) - - return exec.Command(args[0], args[1:]...), nil -} - -// GetEditorInput gets the user input by launching a text editor and waiting for -// it to exit -func GetEditorInput(ctx infra.DnoteCtx, fpath string, content *string) error { - if !utils.FileExists(fpath) { - f, err := os.Create(fpath) - if err != nil { - return errors.Wrap(err, "creating a temporary content file") - } - err = f.Close() - if err != nil { - return errors.Wrap(err, "closing the temporary content file") - } - } - - cmd, err := newEditorCmd(ctx, fpath) - if err != nil { - return errors.Wrap(err, "creating an editor command") - } - - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - err = cmd.Start() - if err != nil { - return errors.Wrapf(err, "launching an editor") - } - - err = cmd.Wait() - if err != nil { - return errors.Wrap(err, "waiting for the editor") - } - - b, err := ioutil.ReadFile(fpath) - if err != nil { - return errors.Wrap(err, "reading the temporary content file") - } - - err = os.Remove(fpath) - if err != nil { - return errors.Wrap(err, "removing the temporary content file") - } - - raw := string(b) - c := SanitizeContent(raw) - - *content = c - - return nil -} - -func initSystemKV(db *infra.DB, key string, val string) error { - var count int - if err := db.QueryRow("SELECT count(*) FROM system WHERE key = ?", key).Scan(&count); err != nil { - return errors.Wrapf(err, "counting %s", key) - } - - if count > 0 { - return nil - } - - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (?, ?)", key, val); err != nil { - db.Rollback() - return errors.Wrapf(err, "inserting %s %s", key, val) - - } - - return nil -} - -// InitSystem inserts system data if missing -func InitSystem(ctx infra.DnoteCtx) error { - db := ctx.DB - - tx, err := db.Begin() - if err != nil { - return errors.Wrap(err, "beginning a transaction") - } - - nowStr := strconv.FormatInt(time.Now().Unix(), 10) - if err := initSystemKV(tx, infra.SystemLastUpgrade, nowStr); err != nil { - return errors.Wrapf(err, "initializing system config for %s", infra.SystemLastUpgrade) - } - if err := initSystemKV(tx, infra.SystemLastMaxUSN, "0"); err != nil { - return errors.Wrapf(err, "initializing system config for %s", infra.SystemLastMaxUSN) - } - if err := initSystemKV(tx, infra.SystemLastSyncAt, "0"); err != nil { - return errors.Wrapf(err, "initializing system config for %s", infra.SystemLastSyncAt) - } - - tx.Commit() - - return nil -} - -// GetValidSession returns a session key from the local storage if one exists and is not expired -// If one does not exist or is expired, it prints out an instruction and returns false -func GetValidSession(ctx infra.DnoteCtx) (string, bool, error) { - db := ctx.DB - - var sessionKey string - var sessionKeyExpires int64 - - if err := GetSystem(db, infra.SystemSessionKey, &sessionKey); err != nil { - return "", false, errors.Wrap(err, "getting session key") - } - if err := GetSystem(db, infra.SystemSessionKeyExpiry, &sessionKeyExpires); err != nil { - return "", false, errors.Wrap(err, "getting session key expiry") - } - - if sessionKey == "" { - log.Error("login required. please run `dnote login`\n") - return "", false, nil - } - if sessionKeyExpires < time.Now().Unix() { - log.Error("sesison expired. please run `dnote login`\n") - return "", false, nil - } - - return sessionKey, true, nil -} diff --git a/cli/core/core_test.go b/cli/core/core_test.go deleted file mode 100644 index 2d084e86..00000000 --- a/cli/core/core_test.go +++ /dev/null @@ -1,95 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package core - -import ( - "testing" - - "github.com/dnote/dnote/cli/testutils" - "github.com/pkg/errors" -) - -func TestInitSystemKV(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - var originalCount int - testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - if err := initSystemKV(tx, "testKey", "testVal"); err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "executing")) - } - - tx.Commit() - - // Test - var count int - testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &count) - testutils.AssertEqual(t, count, originalCount+1, "system count mismatch") - - var val string - testutils.MustScan(t, "getting system value", - db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val) - testutils.AssertEqual(t, val, "testVal", "system value mismatch") -} - -func TestInitSystemKV_existing(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "inserting a system config", db, "INSERT INTO system (key, value) VALUES (?, ?)", "testKey", "testVal") - - var originalCount int - testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - if err := initSystemKV(tx, "testKey", "newTestVal"); err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "executing")) - } - - tx.Commit() - - // Test - var count int - testutils.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &count) - testutils.AssertEqual(t, count, originalCount, "system count mismatch") - - var val string - testutils.MustScan(t, "getting system value", - db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val) - testutils.AssertEqual(t, val, "testVal", "system value should not have been updated") -} diff --git a/cli/core/models_test.go b/cli/core/models_test.go deleted file mode 100644 index bb3c8369..00000000 --- a/cli/core/models_test.go +++ /dev/null @@ -1,901 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package core - -import ( - "fmt" - "testing" - - "github.com/dnote/dnote/cli/testutils" - "github.com/pkg/errors" -) - -func TestNewNote(t *testing.T) { - testCases := []struct { - uuid string - bookUUID string - body string - addedOn int64 - editedOn int64 - usn int - public bool - deleted bool - dirty bool - }{ - { - uuid: "n1-uuid", - bookUUID: "b1-uuid", - body: "n1-body", - addedOn: 1542058875, - editedOn: 0, - usn: 0, - public: false, - deleted: false, - dirty: false, - }, - { - uuid: "n2-uuid", - bookUUID: "b2-uuid", - body: "n2-body", - addedOn: 1542058875, - editedOn: 1542058876, - usn: 1008, - public: true, - deleted: true, - dirty: true, - }, - } - - for idx, tc := range testCases { - got := NewNote(tc.uuid, tc.bookUUID, tc.body, tc.addedOn, tc.editedOn, tc.usn, tc.public, tc.deleted, tc.dirty) - - testutils.AssertEqual(t, got.UUID, tc.uuid, fmt.Sprintf("UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.BookUUID, tc.bookUUID, fmt.Sprintf("BookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Body, tc.body, fmt.Sprintf("Body mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.AddedOn, tc.addedOn, fmt.Sprintf("AddedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.EditedOn, tc.editedOn, fmt.Sprintf("EditedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.USN, tc.usn, fmt.Sprintf("USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Public, tc.public, fmt.Sprintf("Public mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Deleted, tc.deleted, fmt.Sprintf("Deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Dirty, tc.dirty, fmt.Sprintf("Dirty mismatch for test case %d", idx)) - } -} - -func TestNoteInsert(t *testing.T) { - testCases := []struct { - uuid string - bookUUID string - body string - addedOn int64 - editedOn int64 - usn int - public bool - deleted bool - dirty bool - }{ - { - uuid: "n1-uuid", - bookUUID: "b1-uuid", - body: "n1-body", - addedOn: 1542058875, - editedOn: 0, - usn: 0, - public: false, - deleted: false, - dirty: false, - }, - { - uuid: "n2-uuid", - bookUUID: "b2-uuid", - body: "n2-body", - addedOn: 1542058875, - editedOn: 1542058876, - usn: 1008, - public: true, - deleted: true, - dirty: true, - }, - } - - for idx, tc := range testCases { - func() { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - n := Note{ - UUID: tc.uuid, - BookUUID: tc.bookUUID, - Body: tc.body, - AddedOn: tc.addedOn, - EditedOn: tc.editedOn, - USN: tc.usn, - Public: tc.public, - Deleted: tc.deleted, - Dirty: tc.dirty, - } - - // execute - db := ctx.DB - - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - if err := n.Insert(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var uuid, bookUUID, body string - var addedOn, editedOn int64 - var usn int - var public, deleted, dirty bool - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), - &uuid, &bookUUID, &body, &addedOn, &editedOn, &usn, &public, &deleted, &dirty) - - testutils.AssertEqual(t, uuid, tc.uuid, fmt.Sprintf("uuid mismatch for test case %d", idx)) - testutils.AssertEqual(t, bookUUID, tc.bookUUID, fmt.Sprintf("bookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, body, tc.body, fmt.Sprintf("body mismatch for test case %d", idx)) - testutils.AssertEqual(t, addedOn, tc.addedOn, fmt.Sprintf("addedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, editedOn, tc.editedOn, fmt.Sprintf("editedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, usn, tc.usn, fmt.Sprintf("usn mismatch for test case %d", idx)) - testutils.AssertEqual(t, public, tc.public, fmt.Sprintf("public mismatch for test case %d", idx)) - testutils.AssertEqual(t, deleted, tc.deleted, fmt.Sprintf("deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, dirty, tc.dirty, fmt.Sprintf("dirty mismatch for test case %d", idx)) - }() - } -} - -func TestNoteUpdate(t *testing.T) { - testCases := []struct { - uuid string - bookUUID string - body string - addedOn int64 - editedOn int64 - usn int - public bool - deleted bool - dirty bool - newBookUUID string - newBody string - newEditedOn int64 - newUSN int - newPublic bool - newDeleted bool - newDirty bool - }{ - { - uuid: "n1-uuid", - bookUUID: "b1-uuid", - body: "n1-body", - addedOn: 1542058875, - editedOn: 0, - usn: 0, - public: false, - deleted: false, - dirty: false, - newBookUUID: "b1-uuid", - newBody: "n1-body edited", - newEditedOn: 1542058879, - newUSN: 0, - newPublic: false, - newDeleted: false, - newDirty: false, - }, - { - uuid: "n1-uuid", - bookUUID: "b1-uuid", - body: "n1-body", - addedOn: 1542058875, - editedOn: 0, - usn: 0, - public: false, - deleted: false, - dirty: true, - newBookUUID: "b2-uuid", - newBody: "n1-body", - newEditedOn: 1542058879, - newUSN: 0, - newPublic: true, - newDeleted: false, - newDirty: false, - }, - { - uuid: "n1-uuid", - bookUUID: "b1-uuid", - body: "n1-body", - addedOn: 1542058875, - editedOn: 0, - usn: 10, - public: false, - deleted: false, - dirty: false, - newBookUUID: "", - newBody: "", - newEditedOn: 1542058879, - newUSN: 151, - newPublic: false, - newDeleted: true, - newDirty: false, - }, - { - uuid: "n1-uuid", - bookUUID: "b1-uuid", - body: "n1-body", - addedOn: 1542058875, - editedOn: 0, - usn: 0, - public: false, - deleted: false, - dirty: false, - newBookUUID: "", - newBody: "", - newEditedOn: 1542058879, - newUSN: 15, - newPublic: false, - newDeleted: true, - newDirty: false, - }, - } - - for idx, tc := range testCases { - func() { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - n1 := Note{ - UUID: tc.uuid, - BookUUID: tc.bookUUID, - Body: tc.body, - AddedOn: tc.addedOn, - EditedOn: tc.editedOn, - USN: tc.usn, - Public: tc.public, - Deleted: tc.deleted, - Dirty: tc.dirty, - } - n2 := Note{ - UUID: "n2-uuid", - BookUUID: "b10-uuid", - Body: "n2 body", - AddedOn: 1542058875, - EditedOn: 0, - USN: 39, - Public: false, - Deleted: false, - Dirty: false, - } - - db := ctx.DB - testutils.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Public, n1.Deleted, n1.Dirty) - testutils.MustExec(t, fmt.Sprintf("inserting n2 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Public, n2.Deleted, n2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - n1.BookUUID = tc.newBookUUID - n1.Body = tc.newBody - n1.EditedOn = tc.newEditedOn - n1.USN = tc.newUSN - n1.Public = tc.newPublic - n1.Deleted = tc.newDeleted - n1.Dirty = tc.newDirty - - if err := n1.Update(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var n1Record, n2Record Note - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), - &n1Record.UUID, &n1Record.BookUUID, &n1Record.Body, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.USN, &n1Record.Public, &n1Record.Deleted, &n1Record.Dirty) - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) - - testutils.AssertEqual(t, n1Record.UUID, n1.UUID, fmt.Sprintf("n1 uuid mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.BookUUID, tc.newBookUUID, fmt.Sprintf("n1 bookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Body, tc.newBody, fmt.Sprintf("n1 body mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.AddedOn, n1.AddedOn, fmt.Sprintf("n1 addedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.EditedOn, tc.newEditedOn, fmt.Sprintf("n1 editedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.USN, tc.newUSN, fmt.Sprintf("n1 usn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Public, tc.newPublic, fmt.Sprintf("n1 public mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Deleted, tc.newDeleted, fmt.Sprintf("n1 deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, n1Record.Dirty, tc.newDirty, fmt.Sprintf("n1 dirty mismatch for test case %d", idx)) - - testutils.AssertEqual(t, n2Record.UUID, n2.UUID, fmt.Sprintf("n2 uuid mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.BookUUID, n2.BookUUID, fmt.Sprintf("n2 bookUUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.Body, n2.Body, fmt.Sprintf("n2 body mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.AddedOn, n2.AddedOn, fmt.Sprintf("n2 addedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.EditedOn, n2.EditedOn, fmt.Sprintf("n2 editedOn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.USN, n2.USN, fmt.Sprintf("n2 usn mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.Public, n2.Public, fmt.Sprintf("n2 public mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.Deleted, n2.Deleted, fmt.Sprintf("n2 deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, n2Record.Dirty, n2.Dirty, fmt.Sprintf("n2 dirty mismatch for test case %d", idx)) - }() - } -} - -func TestNoteUpdateUUID(t *testing.T) { - testCases := []struct { - newUUID string - }{ - { - newUUID: "n1-new-uuid", - }, - { - newUUID: "n2-new-uuid", - }, - } - - for idx, tc := range testCases { - t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - n1 := Note{ - UUID: "n1-uuid", - BookUUID: "b1-uuid", - AddedOn: 1542058874, - Body: "n1-body", - USN: 1, - Deleted: true, - Dirty: false, - } - n2 := Note{ - UUID: "n2-uuid", - BookUUID: "b1-uuid", - AddedOn: 1542058874, - Body: "n2-body", - USN: 1, - Deleted: true, - Dirty: false, - } - - db := ctx.DB - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.Body, n1.AddedOn, n1.USN, n1.Deleted, n1.Dirty) - testutils.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.Body, n2.AddedOn, n2.USN, n2.Deleted, n2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - if err := n1.UpdateUUID(tx, tc.newUUID); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var n1Record, n2Record Note - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, body, usn, deleted, dirty FROM notes WHERE body = ?", "n1-body"), - &n1Record.UUID, &n1Record.Body, &n1Record.USN, &n1Record.Deleted, &n1Record.Dirty) - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, body, usn, deleted, dirty FROM notes WHERE body = ?", "n2-body"), - &n2Record.UUID, &n2Record.Body, &n2Record.USN, &n2Record.Deleted, &n2Record.Dirty) - - testutils.AssertEqual(t, n1.UUID, tc.newUUID, "n1 original reference uuid mismatch") - testutils.AssertEqual(t, n1Record.UUID, tc.newUUID, "n1 uuid mismatch") - testutils.AssertEqual(t, n2Record.UUID, n2.UUID, "n2 uuid mismatch") - }) - } -} - -func TestNoteExpunge(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - n1 := Note{ - UUID: "n1-uuid", - BookUUID: "b9-uuid", - Body: "n1 body", - AddedOn: 1542058874, - EditedOn: 0, - USN: 22, - Public: false, - Deleted: false, - Dirty: false, - } - n2 := Note{ - UUID: "n2-uuid", - BookUUID: "b10-uuid", - Body: "n2 body", - AddedOn: 1542058875, - EditedOn: 0, - USN: 39, - Public: false, - Deleted: false, - Dirty: false, - } - - db := ctx.DB - testutils.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Public, n1.Deleted, n1.Dirty) - testutils.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Public, n2.Deleted, n2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := n1.Expunge(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var noteCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch") - - var n2Record Note - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), - &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Public, &n2Record.Deleted, &n2Record.Dirty) - - testutils.AssertEqual(t, n2Record.UUID, n2.UUID, "n2 uuid mismatch") - testutils.AssertEqual(t, n2Record.BookUUID, n2.BookUUID, "n2 bookUUID mismatch") - testutils.AssertEqual(t, n2Record.Body, n2.Body, "n2 body mismatch") - testutils.AssertEqual(t, n2Record.AddedOn, n2.AddedOn, "n2 addedOn mismatch") - testutils.AssertEqual(t, n2Record.EditedOn, n2.EditedOn, "n2 editedOn mismatch") - testutils.AssertEqual(t, n2Record.USN, n2.USN, "n2 usn mismatch") - testutils.AssertEqual(t, n2Record.Public, n2.Public, "n2 public mismatch") - testutils.AssertEqual(t, n2Record.Deleted, n2.Deleted, "n2 deleted mismatch") - testutils.AssertEqual(t, n2Record.Dirty, n2.Dirty, "n2 dirty mismatch") -} - -func TestNewBook(t *testing.T) { - testCases := []struct { - uuid string - label string - usn int - deleted bool - dirty bool - }{ - { - uuid: "b1-uuid", - label: "b1-label", - usn: 0, - deleted: false, - dirty: false, - }, - { - uuid: "b2-uuid", - label: "b2-label", - usn: 1008, - deleted: false, - dirty: true, - }, - } - - for idx, tc := range testCases { - got := NewBook(tc.uuid, tc.label, tc.usn, tc.deleted, tc.dirty) - - testutils.AssertEqual(t, got.UUID, tc.uuid, fmt.Sprintf("UUID mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Label, tc.label, fmt.Sprintf("Label mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.USN, tc.usn, fmt.Sprintf("USN mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Deleted, tc.deleted, fmt.Sprintf("Deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, got.Dirty, tc.dirty, fmt.Sprintf("Dirty mismatch for test case %d", idx)) - } -} - -func TestBookInsert(t *testing.T) { - testCases := []struct { - uuid string - label string - usn int - deleted bool - dirty bool - }{ - { - uuid: "b1-uuid", - label: "b1-label", - usn: 10808, - deleted: false, - dirty: false, - }, - { - uuid: "b1-uuid", - label: "b1-label", - usn: 10808, - deleted: false, - dirty: true, - }, - } - - for idx, tc := range testCases { - func() { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - b := Book{ - UUID: tc.uuid, - Label: tc.label, - USN: tc.usn, - Dirty: tc.dirty, - Deleted: tc.deleted, - } - - // execute - db := ctx.DB - - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - if err := b.Insert(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var uuid, label string - var usn int - var deleted, dirty bool - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", tc.uuid), - &uuid, &label, &usn, &deleted, &dirty) - - testutils.AssertEqual(t, uuid, tc.uuid, fmt.Sprintf("uuid mismatch for test case %d", idx)) - testutils.AssertEqual(t, label, tc.label, fmt.Sprintf("label mismatch for test case %d", idx)) - testutils.AssertEqual(t, usn, tc.usn, fmt.Sprintf("usn mismatch for test case %d", idx)) - testutils.AssertEqual(t, deleted, tc.deleted, fmt.Sprintf("deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, dirty, tc.dirty, fmt.Sprintf("dirty mismatch for test case %d", idx)) - }() - } -} - -func TestBookUpdate(t *testing.T) { - testCases := []struct { - uuid string - label string - usn int - deleted bool - dirty bool - newLabel string - newUSN int - newDeleted bool - newDirty bool - }{ - { - uuid: "b1-uuid", - label: "b1-label", - usn: 0, - deleted: false, - dirty: false, - newLabel: "b1-label-edited", - newUSN: 0, - newDeleted: false, - newDirty: true, - }, - { - uuid: "b1-uuid", - label: "b1-label", - usn: 0, - deleted: false, - dirty: false, - newLabel: "", - newUSN: 10, - newDeleted: true, - newDirty: false, - }, - } - - for idx, tc := range testCases { - func() { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - b1 := Book{ - UUID: "b1-uuid", - Label: "b1-label", - USN: 1, - Deleted: true, - Dirty: false, - } - b2 := Book{ - UUID: "b2-uuid", - Label: "b2-label", - USN: 1, - Deleted: true, - Dirty: false, - } - - db := ctx.DB - testutils.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1.UUID, b1.Label, b1.USN, b1.Deleted, b1.Dirty) - testutils.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2.UUID, b2.Label, b2.USN, b2.Deleted, b2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) - } - - b1.Label = tc.newLabel - b1.USN = tc.newUSN - b1.Deleted = tc.newDeleted - b1.Dirty = tc.newDirty - - if err := b1.Update(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) - } - - tx.Commit() - - // test - var b1Record, b2Record Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", tc.uuid), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Deleted, &b1Record.Dirty) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", b2.UUID), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Deleted, &b2Record.Dirty) - - testutils.AssertEqual(t, b1Record.UUID, b1.UUID, fmt.Sprintf("b1 uuid mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Label, tc.newLabel, fmt.Sprintf("b1 label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.USN, tc.newUSN, fmt.Sprintf("b1 usn mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Deleted, tc.newDeleted, fmt.Sprintf("b1 deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, b1Record.Dirty, tc.newDirty, fmt.Sprintf("b1 dirty mismatch for test case %d", idx)) - - testutils.AssertEqual(t, b2Record.UUID, b2.UUID, fmt.Sprintf("b2 uuid mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Label, b2.Label, fmt.Sprintf("b2 label mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.USN, b2.USN, fmt.Sprintf("b2 usn mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Deleted, b2.Deleted, fmt.Sprintf("b2 deleted mismatch for test case %d", idx)) - testutils.AssertEqual(t, b2Record.Dirty, b2.Dirty, fmt.Sprintf("b2 dirty mismatch for test case %d", idx)) - }() - } -} - -func TestBookUpdateUUID(t *testing.T) { - testCases := []struct { - newUUID string - }{ - { - newUUID: "b1-new-uuid", - }, - { - newUUID: "b2-new-uuid", - }, - } - - for idx, tc := range testCases { - t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) { - - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - b1 := Book{ - UUID: "b1-uuid", - Label: "b1-label", - USN: 1, - Deleted: true, - Dirty: false, - } - b2 := Book{ - UUID: "b2-uuid", - Label: "b2-label", - USN: 1, - Deleted: true, - Dirty: false, - } - - db := ctx.DB - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1.UUID, b1.Label, b1.USN, b1.Deleted, b1.Dirty) - testutils.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2.UUID, b2.Label, b2.USN, b2.Deleted, b2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - if err := b1.UpdateUUID(tx, tc.newUUID); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var b1Record, b2Record Book - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE label = ?", "b1-label"), - &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Deleted, &b1Record.Dirty) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE label = ?", "b2-label"), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Deleted, &b2Record.Dirty) - - testutils.AssertEqual(t, b1.UUID, tc.newUUID, "b1 original reference uuid mismatch") - testutils.AssertEqual(t, b1Record.UUID, tc.newUUID, "b1 uuid mismatch") - testutils.AssertEqual(t, b2Record.UUID, b2.UUID, "b2 uuid mismatch") - }) - } -} - -func TestBookExpunge(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - b1 := Book{ - UUID: "b1-uuid", - Label: "b1-label", - USN: 1, - Deleted: true, - Dirty: false, - } - b2 := Book{ - UUID: "b2-uuid", - Label: "b2-label", - USN: 1, - Deleted: true, - Dirty: false, - } - - db := ctx.DB - testutils.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1.UUID, b1.Label, b1.USN, b1.Deleted, b1.Dirty) - testutils.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2.UUID, b2.Label, b2.USN, b2.Deleted, b2.Dirty) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := b1.Expunge(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing").Error()) - } - - tx.Commit() - - // test - var bookCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - - var b2Record Book - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", "b2-uuid"), - &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Deleted, &b2Record.Dirty) - - testutils.AssertEqual(t, b2Record.UUID, b2.UUID, "b2 uuid mismatch") - testutils.AssertEqual(t, b2Record.Label, b2.Label, "b2 label mismatch") - testutils.AssertEqual(t, b2Record.USN, b2.USN, "b2 usn mismatch") - testutils.AssertEqual(t, b2Record.Deleted, b2.Deleted, "b2 deleted mismatch") - testutils.AssertEqual(t, b2Record.Dirty, b2.Dirty, "b2 dirty mismatch") -} - -// TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete -func TestNoteFTS(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - // execute - insert - n := Note{ - UUID: "n1-uuid", - BookUUID: "b1-uuid", - Body: "foo bar", - AddedOn: 1542058875, - EditedOn: 0, - USN: 0, - Public: false, - Deleted: false, - Dirty: false, - } - db := ctx.DB - - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := n.Insert(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "inserting").Error()) - } - - tx.Commit() - - // test - var noteCount, noteFtsCount, noteSearchCount int - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) - testutils.MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "foo"), ¬eSearchCount) - - testutils.AssertEqual(t, noteCount, 1, "noteCount mismatch") - testutils.AssertEqual(t, noteFtsCount, 1, "noteFtsCount mismatch") - testutils.AssertEqual(t, noteSearchCount, 1, "noteSearchCount mismatch") - - // execute - update - tx, err = db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - n.Body = "baz quz" - if err := n.Update(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "updating").Error()) - } - - tx.Commit() - - // test - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) - testutils.AssertEqual(t, noteCount, 1, "noteCount mismatch") - testutils.AssertEqual(t, noteFtsCount, 1, "noteFtsCount mismatch") - - testutils.MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "foo"), ¬eSearchCount) - testutils.AssertEqual(t, noteSearchCount, 0, "noteSearchCount for foo mismatch") - testutils.MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "baz"), ¬eSearchCount) - testutils.AssertEqual(t, noteSearchCount, 1, "noteSearchCount for baz mismatch") - - // execute - delete - tx, err = db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := n.Expunge(tx); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "expunging").Error()) - } - - tx.Commit() - - // test - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) - - testutils.AssertEqual(t, noteCount, 0, "noteCount mismatch") - testutils.AssertEqual(t, noteFtsCount, 0, "noteFtsCount mismatch") -} diff --git a/cli/core/operations.go b/cli/core/operations.go deleted file mode 100644 index 73ae8401..00000000 --- a/cli/core/operations.go +++ /dev/null @@ -1,103 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package core - -import ( - "encoding/base64" - - "github.com/dnote/dnote/cli/infra" - "github.com/pkg/errors" -) - -// InsertSystem inserets a system configuration -func InsertSystem(db *infra.DB, key, val string) error { - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", key, val); err != nil { - return errors.Wrap(err, "saving system config") - } - - return nil -} - -// UpsertSystem inserts or updates a system configuration -func UpsertSystem(db *infra.DB, key, val string) error { - var count int - if err := db.QueryRow("SELECT count(*) FROM system WHERE key = ?", key).Scan(&count); err != nil { - return errors.Wrap(err, "counting system record") - } - - if count == 0 { - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", key, val); err != nil { - return errors.Wrap(err, "saving system config") - } - } else { - if _, err := db.Exec("UPDATE system SET value = ? WHERE key = ?", val, key); err != nil { - return errors.Wrap(err, "updating system config") - } - } - - return nil -} - -// UpdateSystem updates a system configuration -func UpdateSystem(db *infra.DB, key, val interface{}) error { - if _, err := db.Exec("UPDATE system SET value = ? WHERE key = ?", val, key); err != nil { - return errors.Wrap(err, "updating system config") - } - - return nil -} - -// GetSystem scans the given system configuration record onto the destination -func GetSystem(db *infra.DB, key string, dest interface{}) error { - if err := db.QueryRow("SELECT value FROM system WHERE key = ?", key).Scan(dest); err != nil { - return errors.Wrap(err, "finding system configuration record") - } - - return nil -} - -// DeleteSystem delets the given system record -func DeleteSystem(db *infra.DB, key string) error { - if _, err := db.Exec("DELETE FROM system WHERE key = ?", key); err != nil { - return errors.Wrap(err, "deleting system config") - } - - return nil -} - -// GetCipherKey retrieves the cipher key and decode the base64 into bytes. -func GetCipherKey(ctx infra.DnoteCtx) ([]byte, error) { - db, err := ctx.DB.Begin() - if err != nil { - return nil, errors.Wrap(err, "beginning transaction") - } - - var cipherKeyB64 string - err = GetSystem(db, infra.SystemCipherKey, &cipherKeyB64) - if err != nil { - return []byte{}, errors.Wrap(err, "getting enc key") - } - - cipherKey, err := base64.StdEncoding.DecodeString(cipherKeyB64) - if err != nil { - return nil, errors.Wrap(err, "decoding cipherKey from base64") - } - - return cipherKey, nil -} diff --git a/cli/core/operations_test.go b/cli/core/operations_test.go deleted file mode 100644 index 82c742ab..00000000 --- a/cli/core/operations_test.go +++ /dev/null @@ -1,239 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package core - -import ( - "fmt" - "testing" - - "github.com/dnote/dnote/cli/testutils" - "github.com/pkg/errors" -) - -func TestInsertSystem(t *testing.T) { - testCases := []struct { - key string - val string - }{ - { - key: "foo", - val: "1558089284", - }, - { - key: "baz", - val: "quz", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - // execute - db := ctx.DB - - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := InsertSystem(tx, tc.key, tc.val); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) - } - - tx.Commit() - - // test - var key, val string - testutils.MustScan(t, "getting the saved record", - db.QueryRow("SELECT key, value FROM system WHERE key = ?", tc.key), &key, &val) - - testutils.AssertEqual(t, key, tc.key, "key mismatch for test case") - testutils.AssertEqual(t, val, tc.val, "val mismatch for test case") - }) - } -} - -func TestUpsertSystem(t *testing.T) { - testCases := []struct { - key string - val string - countDelta int - }{ - { - key: "foo", - val: "1558089284", - countDelta: 1, - }, - { - key: "baz", - val: "quz2", - countDelta: 0, - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz") - - var initialSystemCount int - testutils.MustScan(t, "counting records", db.QueryRow("SELECT count(*) FROM system"), &initialSystemCount) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := UpsertSystem(tx, tc.key, tc.val); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) - } - - tx.Commit() - - // test - var key, val string - testutils.MustScan(t, "getting the saved record", - db.QueryRow("SELECT key, value FROM system WHERE key = ?", tc.key), &key, &val) - var systemCount int - testutils.MustScan(t, "counting records", - db.QueryRow("SELECT count(*) FROM system"), &systemCount) - - testutils.AssertEqual(t, key, tc.key, "key mismatch") - testutils.AssertEqual(t, val, tc.val, "val mismatch") - testutils.AssertEqual(t, systemCount, initialSystemCount+tc.countDelta, "count mismatch") - }) - } -} - -func TestGetSystem(t *testing.T) { - t.Run(fmt.Sprintf("get string value"), func(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - // execute - db := ctx.DB - testutils.MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "bar") - - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - var dest string - if err := GetSystem(tx, "foo", &dest); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) - } - tx.Commit() - - // test - testutils.AssertEqual(t, dest, "bar", "dest mismatch") - }) - - t.Run(fmt.Sprintf("get int64 value"), func(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - // execute - db := ctx.DB - testutils.MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", 1234) - - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - var dest int64 - if err := GetSystem(tx, "foo", &dest); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) - } - tx.Commit() - - // test - testutils.AssertEqual(t, dest, int64(1234), "dest mismatch") - }) -} - -func TestUpdateSystem(t *testing.T) { - testCases := []struct { - key string - val string - countDelta int - }{ - { - key: "foo", - val: "1558089284", - }, - { - key: "foo", - val: "bar", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("update %s %s", tc.key, tc.val), func(t *testing.T) { - // Setup - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "fuz") - testutils.MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz") - - var initialSystemCount int - testutils.MustScan(t, "counting records", db.QueryRow("SELECT count(*) FROM system"), &initialSystemCount) - - // execute - tx, err := db.Begin() - if err != nil { - t.Fatalf(errors.Wrap(err, "beginning a transaction").Error()) - } - - if err := UpdateSystem(tx, tc.key, tc.val); err != nil { - tx.Rollback() - t.Fatalf(errors.Wrap(err, "executing for test case").Error()) - } - - tx.Commit() - - // test - var key, val string - testutils.MustScan(t, "getting the saved record", - db.QueryRow("SELECT key, value FROM system WHERE key = ?", tc.key), &key, &val) - var systemCount int - testutils.MustScan(t, "counting records", - db.QueryRow("SELECT count(*) FROM system"), &systemCount) - - testutils.AssertEqual(t, key, tc.key, "key mismatch") - testutils.AssertEqual(t, val, tc.val, "val mismatch") - testutils.AssertEqual(t, systemCount, initialSystemCount, "count mismatch") - }) - } -} diff --git a/cli/core/upgrade.go b/cli/core/upgrade.go deleted file mode 100644 index de3a3bab..00000000 --- a/cli/core/upgrade.go +++ /dev/null @@ -1,117 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package core - -import ( - "context" - "fmt" - "time" - - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/utils" - "github.com/google/go-github/github" - "github.com/pkg/errors" -) - -// upgradeInterval is 3 weeks -var upgradeInterval int64 = 86400 * 7 * 3 - -// shouldCheckUpdate checks if update should be checked -func shouldCheckUpdate(ctx infra.DnoteCtx) (bool, error) { - db := ctx.DB - - var lastUpgrade int64 - err := db.QueryRow("SELECT value FROM system WHERE key = ?", infra.SystemLastUpgrade).Scan(&lastUpgrade) - if err != nil { - return false, errors.Wrap(err, "getting last_udpate") - } - - now := time.Now().Unix() - - return now-lastUpgrade > upgradeInterval, nil -} - -func touchLastUpgrade(ctx infra.DnoteCtx) error { - db := ctx.DB - - now := time.Now().Unix() - _, err := db.Exec("UPDATE system SET value = ? WHERE key = ?", now, infra.SystemLastUpgrade) - if err != nil { - return errors.Wrap(err, "updating last_upgrade") - } - - return nil -} - -func checkVersion(ctx infra.DnoteCtx) error { - log.Infof("current version is %s\n", ctx.Version) - - // Fetch the latest version - gh := github.NewClient(nil) - releases, _, err := gh.Repositories.ListReleases(context.Background(), "dnote", "cli", nil) - if err != nil { - return errors.Wrap(err, "fetching releases") - } - - latest := releases[0] - latestVersion := (*latest.TagName)[1:] - - log.Infof("latest version is %s\n", latestVersion) - - if latestVersion == ctx.Version { - log.Success("you are up-to-date\n\n") - } else { - log.Infof("to upgrade, see https://github.com/dnote/dnote/cli/blob/master/README.md\n") - } - - return nil -} - -// CheckUpdate triggers update if needed -func CheckUpdate(ctx infra.DnoteCtx) error { - shouldCheck, err := shouldCheckUpdate(ctx) - if err != nil { - return errors.Wrap(err, "checking if dnote should check update") - } - if !shouldCheck { - return nil - } - - err = touchLastUpgrade(ctx) - if err != nil { - return errors.Wrap(err, "updating the last upgrade timestamp") - } - - fmt.Printf("\n") - willCheck, err := utils.AskConfirmation("check for upgrade?", true) - if err != nil { - return errors.Wrap(err, "getting user confirmation") - } - if !willCheck { - return nil - } - - err = checkVersion(ctx) - if err != nil { - return errors.Wrap(err, "checking version") - } - - return nil -} diff --git a/cli/crypt/utils.go b/cli/crypt/utils.go deleted file mode 100644 index 61f00800..00000000 --- a/cli/crypt/utils.go +++ /dev/null @@ -1,125 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -// Package crypt provides cryptographic funcitonalities -package crypt - -import ( - "crypto/aes" - "crypto/cipher" - "crypto/rand" - "crypto/sha256" - "encoding/base64" - "io" - - "github.com/dnote/dnote/cli/log" - "github.com/pkg/errors" - "golang.org/x/crypto/hkdf" - "golang.org/x/crypto/pbkdf2" -) - -var aesGcmNonceSize = 12 - -func runHkdf(secret, salt, info []byte) ([]byte, error) { - r := hkdf.New(sha256.New, secret, salt, info) - - ret := make([]byte, 32) - _, err := io.ReadFull(r, ret) - if err != nil { - return []byte{}, errors.Wrap(err, "reading key bytes") - } - - return ret, nil -} - -// MakeKeys derives, from the given credential, a key set comprising of an encryption key -// and an authentication key -func MakeKeys(password, email []byte, iteration int) ([]byte, []byte, error) { - masterKey := pbkdf2.Key([]byte(password), []byte(email), iteration, 32, sha256.New) - log.Debug("email: %s, password: %s", email, password) - - authKey, err := runHkdf(masterKey, email, []byte("auth")) - if err != nil { - return nil, nil, errors.Wrap(err, "deriving auth key") - } - - return masterKey, authKey, nil -} - -// AesGcmEncrypt encrypts the plaintext using AES in a GCM mode. It returns -// a ciphertext prepended by a 12 byte pseudo-random nonce, encoded in base64. -func AesGcmEncrypt(key, plaintext []byte) (string, error) { - if key == nil { - return "", errors.New("no key provided") - } - - block, err := aes.NewCipher(key) - if err != nil { - return "", errors.Wrap(err, "initializing aes") - } - - aesgcm, err := cipher.NewGCM(block) - if err != nil { - return "", errors.Wrap(err, "initializing gcm") - } - - nonce := make([]byte, aesGcmNonceSize) - if _, err := io.ReadFull(rand.Reader, nonce); err != nil { - return "", errors.Wrap(err, "generating nonce") - } - - ciphertext := aesgcm.Seal(nonce, nonce, []byte(plaintext), nil) - cipherKeyB64 := base64.StdEncoding.EncodeToString(ciphertext) - - return cipherKeyB64, nil -} - -// AesGcmDecrypt decrypts the encrypted data using AES in a GCM mode. The data should be -// a base64 encoded string in the format of 12 byte nonce followed by a ciphertext. -func AesGcmDecrypt(key []byte, dataB64 string) ([]byte, error) { - if key == nil { - return nil, errors.New("no key provided") - } - - data, err := base64.StdEncoding.DecodeString(dataB64) - if err != nil { - return nil, errors.Wrap(err, "decoding base64 data") - } - - block, err := aes.NewCipher(key) - if err != nil { - return nil, errors.Wrap(err, "initializing aes") - } - - aesgcm, err := cipher.NewGCM(block) - if err != nil { - return nil, errors.Wrap(err, "initializing gcm") - } - - if len(data) < aesGcmNonceSize { - return nil, errors.Wrap(err, "malformed data") - } - - nonce, ciphertext := data[:aesGcmNonceSize], data[aesGcmNonceSize:] - plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil) - if err != nil { - return nil, errors.Wrap(err, "decrypting") - } - - return plaintext, nil -} diff --git a/cli/crypt/utils_test.go b/cli/crypt/utils_test.go deleted file mode 100644 index 729ddb5c..00000000 --- a/cli/crypt/utils_test.go +++ /dev/null @@ -1,118 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package crypt - -import ( - "crypto/aes" - "crypto/cipher" - "encoding/base64" - "fmt" - "testing" - - "github.com/dnote/dnote/cli/testutils" - "github.com/pkg/errors" -) - -func TestAesGcmEncrypt(t *testing.T) { - testCases := []struct { - key []byte - plaintext []byte - }{ - { - key: []byte("AES256Key-32Characters1234567890"), - plaintext: []byte("foo bar baz quz"), - }, - { - key: []byte("AES256Key-32Charactersabcdefghij"), - plaintext: []byte("1234 foo 5678 bar 7890 baz"), - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("key %s plaintext %s", tc.key, tc.plaintext), func(t *testing.T) { - // encrypt - dataB64, err := AesGcmEncrypt(tc.key, tc.plaintext) - if err != nil { - t.Fatal(errors.Wrap(err, "performing encryption")) - } - - // test that data can be decrypted - data, err := base64.StdEncoding.DecodeString(dataB64) - if err != nil { - t.Fatal(errors.Wrap(err, "decoding data from base64")) - } - - nonce, ciphertext := data[:12], data[12:] - - fmt.Println(string(data)) - - block, err := aes.NewCipher([]byte(tc.key)) - if err != nil { - t.Fatal(errors.Wrap(err, "initializing aes")) - } - - aesgcm, err := cipher.NewGCM(block) - if err != nil { - t.Fatal(errors.Wrap(err, "initializing gcm")) - } - - plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil) - if err != nil { - t.Fatal(errors.Wrap(err, "decode")) - } - - testutils.AssertDeepEqual(t, plaintext, tc.plaintext, "plaintext mismatch") - }) - } -} - -func TestAesGcmDecrypt(t *testing.T) { - testCases := []struct { - key []byte - ciphertextB64 string - expectedPlaintext string - }{ - { - key: []byte("AES256Key-32Characters1234567890"), - ciphertextB64: "M2ov9hWMQ52v1S/zigwX3bJt4cVCV02uiRm/grKqN/rZxNkJrD7vK4Ii0g==", - expectedPlaintext: "foo bar baz quz", - }, - { - key: []byte("AES256Key-32Characters1234567890"), - ciphertextB64: "M4csFKUIUbD1FBEzLgHjscoKgN0lhMGJ0n2nKWiCkE/qSKlRP7kS", - expectedPlaintext: "foo\n1\nbar\n2", - }, - { - key: []byte("AES256Key-32Characters1234567890"), - ciphertextB64: "pe/fnw73MR1clmVIlRSJ5gDwBdnPly/DF7DsR5dJVz4dHZlv0b10WzvJEGOCHZEr+Q==", - expectedPlaintext: "föo\nbār\nbåz & qūz", - }, - } - - for _, tc := range testCases { - t.Run(fmt.Sprintf("key %s ciphertext %s", tc.key, tc.ciphertextB64), func(t *testing.T) { - plaintext, err := AesGcmDecrypt(tc.key, tc.ciphertextB64) - if err != nil { - t.Fatal(errors.Wrap(err, "performing decryption")) - } - - testutils.AssertDeepEqual(t, plaintext, []byte(tc.expectedPlaintext), "plaintext mismatch") - }) - } -} diff --git a/cli/infra/main.go b/cli/infra/main.go deleted file mode 100644 index c64179c3..00000000 --- a/cli/infra/main.go +++ /dev/null @@ -1,225 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -// Package infra defines dnote structure -package infra - -import ( - "database/sql" - "encoding/base64" - "fmt" - "os" - "os/user" - - // use sqlite - _ "github.com/mattn/go-sqlite3" - - "github.com/pkg/errors" -) - -var ( - // DnoteDirName is the name of the directory containing dnote files - DnoteDirName = ".dnote" - - // SystemSchema is the key for schema in the system table - SystemSchema = "schema" - // SystemRemoteSchema is the key for remote schema in the system table - SystemRemoteSchema = "remote_schema" - // SystemLastSyncAt is the timestamp of the server at the last sync - SystemLastSyncAt = "last_sync_time" - // SystemLastMaxUSN is the user's max_usn from the server at the alst sync - SystemLastMaxUSN = "last_max_usn" - // SystemLastUpgrade is the timestamp at which the system more recently checked for an upgrade - SystemLastUpgrade = "last_upgrade" - // SystemCipherKey is the encryption key - SystemCipherKey = "enc_key" - // SystemSessionKey is the session key - SystemSessionKey = "session_token" - // SystemSessionKeyExpiry is the timestamp at which the session key will expire - SystemSessionKeyExpiry = "session_token_expiry" -) - -// DnoteCtx is a context holding the information of the current runtime -type DnoteCtx struct { - HomeDir string - DnoteDir string - APIEndpoint string - Version string - DB *DB - SessionKey string - SessionKeyExpiry int64 - CipherKey []byte -} - -// Config holds dnote configuration -type Config struct { - Editor string -} - -// NewCtx returns a new dnote context -func NewCtx(apiEndpoint, versionTag string) (DnoteCtx, error) { - homeDir, err := getHomeDir() - if err != nil { - return DnoteCtx{}, errors.Wrap(err, "Failed to get home dir") - } - dnoteDir := getDnoteDir(homeDir) - - dnoteDBPath := fmt.Sprintf("%s/dnote.db", dnoteDir) - db, err := OpenDB(dnoteDBPath) - if err != nil { - return DnoteCtx{}, errors.Wrap(err, "conntecting to db") - } - - ret := DnoteCtx{ - HomeDir: homeDir, - DnoteDir: dnoteDir, - APIEndpoint: apiEndpoint, - Version: versionTag, - DB: db, - } - - return ret, nil -} - -// SetupCtx populates context and returns a new context -func SetupCtx(ctx DnoteCtx) (DnoteCtx, error) { - db := ctx.DB - - var sessionKey, cipherKeyB64 string - var sessionKeyExpiry int64 - - err := db.QueryRow("SELECT value FROM system WHERE key = ?", SystemSessionKey).Scan(&sessionKey) - if err != nil && err != sql.ErrNoRows { - return ctx, errors.Wrap(err, "finding sesison key") - } - err = db.QueryRow("SELECT value FROM system WHERE key = ?", SystemCipherKey).Scan(&cipherKeyB64) - if err != nil && err != sql.ErrNoRows { - return ctx, errors.Wrap(err, "finding sesison key") - } - err = db.QueryRow("SELECT value FROM system WHERE key = ?", SystemSessionKeyExpiry).Scan(&sessionKeyExpiry) - if err != nil && err != sql.ErrNoRows { - return ctx, errors.Wrap(err, "finding sesison key expiry") - } - - cipherKey, err := base64.StdEncoding.DecodeString(cipherKeyB64) - if err != nil { - return ctx, errors.Wrap(err, "decoding cipherKey from base64") - } - - ret := DnoteCtx{ - HomeDir: ctx.HomeDir, - DnoteDir: ctx.DnoteDir, - APIEndpoint: ctx.APIEndpoint, - Version: ctx.Version, - DB: ctx.DB, - SessionKey: sessionKey, - SessionKeyExpiry: sessionKeyExpiry, - CipherKey: cipherKey, - } - - return ret, nil -} - -func getDnoteDir(homeDir string) string { - var ret string - - dnoteDirEnv := os.Getenv("DNOTE_DIR") - if dnoteDirEnv == "" { - ret = fmt.Sprintf("%s/%s", homeDir, DnoteDirName) - } else { - ret = dnoteDirEnv - } - - return ret -} - -func getHomeDir() (string, error) { - homeDirEnv := os.Getenv("DNOTE_HOME_DIR") - if homeDirEnv != "" { - return homeDirEnv, nil - } - - usr, err := user.Current() - if err != nil { - return "", errors.Wrap(err, "Failed to get current user") - } - - return usr.HomeDir, nil -} - -// InitDB initializes the database. -// Ideally this process must be a part of migration sequence. But it is performed -// seaprately because it is a prerequisite for legacy migration. -func InitDB(ctx DnoteCtx) error { - db := ctx.DB - - _, err := db.Exec(`CREATE TABLE IF NOT EXISTS notes - ( - id integer PRIMARY KEY AUTOINCREMENT, - uuid text NOT NULL, - book_uuid text NOT NULL, - content text NOT NULL, - added_on integer NOT NULL, - edited_on integer DEFAULT 0, - public bool DEFAULT false - )`) - if err != nil { - return errors.Wrap(err, "creating notes table") - } - - _, err = db.Exec(`CREATE TABLE IF NOT EXISTS books - ( - uuid text PRIMARY KEY, - label text NOT NULL - )`) - if err != nil { - return errors.Wrap(err, "creating books table") - } - - _, err = db.Exec(`CREATE TABLE IF NOT EXISTS system - ( - key string NOT NULL, - value text NOT NULL - )`) - if err != nil { - return errors.Wrap(err, "creating system table") - } - - _, err = db.Exec(`CREATE TABLE IF NOT EXISTS actions - ( - uuid text PRIMARY KEY, - schema integer NOT NULL, - type text NOT NULL, - data text NOT NULL, - timestamp integer NOT NULL - )`) - if err != nil { - return errors.Wrap(err, "creating actions table") - } - - _, err = db.Exec(` - CREATE UNIQUE INDEX IF NOT EXISTS idx_books_label ON books(label); - CREATE UNIQUE INDEX IF NOT EXISTS idx_notes_uuid ON notes(uuid); - CREATE UNIQUE INDEX IF NOT EXISTS idx_books_uuid ON books(uuid); - CREATE INDEX IF NOT EXISTS idx_notes_book_uuid ON notes(book_uuid);`) - if err != nil { - return errors.Wrap(err, "creating indices") - } - - return nil -} diff --git a/cli/log/output.go b/cli/log/output.go deleted file mode 100644 index 3bc0844b..00000000 --- a/cli/log/output.go +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package log - -import ( - "fmt" -) - -// PrintContent prints the note content with an appropriate format. -func PrintContent(content string) { - fmt.Printf("\n-----------------------content-----------------------\n") - fmt.Printf("%s", content) - fmt.Printf("\n-----------------------------------------------------\n") -} diff --git a/cli/main.go b/cli/main.go deleted file mode 100644 index 212345d1..00000000 --- a/cli/main.go +++ /dev/null @@ -1,80 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package main - -import ( - "os" - - "github.com/dnote/dnote/cli/cmd/root" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - _ "github.com/mattn/go-sqlite3" - "github.com/pkg/errors" - - // commands - "github.com/dnote/dnote/cli/cmd/add" - "github.com/dnote/dnote/cli/cmd/cat" - "github.com/dnote/dnote/cli/cmd/edit" - "github.com/dnote/dnote/cli/cmd/find" - "github.com/dnote/dnote/cli/cmd/login" - "github.com/dnote/dnote/cli/cmd/logout" - "github.com/dnote/dnote/cli/cmd/ls" - "github.com/dnote/dnote/cli/cmd/remove" - "github.com/dnote/dnote/cli/cmd/sync" - "github.com/dnote/dnote/cli/cmd/version" - "github.com/dnote/dnote/cli/cmd/view" -) - -// apiEndpoint and versionTag are populated during link time -var apiEndpoint string -var versionTag = "master" - -func main() { - ctx, err := infra.NewCtx(apiEndpoint, versionTag) - if err != nil { - panic(errors.Wrap(err, "initializing context")) - } - defer ctx.DB.Close() - - if err := root.Prepare(ctx); err != nil { - panic(errors.Wrap(err, "preparing dnote run")) - } - - ctx, err = infra.SetupCtx(ctx) - if err != nil { - panic(errors.Wrap(err, "setting up context")) - } - - root.Register(remove.NewCmd(ctx)) - root.Register(edit.NewCmd(ctx)) - root.Register(login.NewCmd(ctx)) - root.Register(logout.NewCmd(ctx)) - root.Register(add.NewCmd(ctx)) - root.Register(ls.NewCmd(ctx)) - root.Register(sync.NewCmd(ctx)) - root.Register(version.NewCmd(ctx)) - root.Register(cat.NewCmd(ctx)) - root.Register(view.NewCmd(ctx)) - root.Register(find.NewCmd(ctx)) - - if err := root.Execute(); err != nil { - log.Errorf("%s\n", err.Error()) - os.Exit(1) - } -} diff --git a/cli/main_test.go b/cli/main_test.go deleted file mode 100644 index f34a7a78..00000000 --- a/cli/main_test.go +++ /dev/null @@ -1,339 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package main - -import ( - "fmt" - "log" - "os" - "os/exec" - "testing" - - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/testutils" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" -) - -var binaryName = "test-dnote" - -func TestMain(m *testing.M) { - if err := exec.Command("go", "build", "--tags", "fts5", "-o", binaryName).Run(); err != nil { - log.Print(errors.Wrap(err, "building a binary").Error()) - os.Exit(1) - } - - os.Exit(m.Run()) -} - -func TestInit(t *testing.T) { - // Set up - ctx := testutils.InitEnv(t, "./tmp", "./testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - // Execute - testutils.RunDnoteCmd(t, ctx, binaryName) - - // Test - if !utils.FileExists(fmt.Sprintf("%s", ctx.DnoteDir)) { - t.Errorf("dnote directory was not initialized") - } - if !utils.FileExists(fmt.Sprintf("%s/%s", ctx.DnoteDir, core.ConfigFilename)) { - t.Errorf("config file was not initialized") - } - - db := ctx.DB - - var notesTableCount, booksTableCount, systemTableCount int - testutils.MustScan(t, "counting notes", - db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", "table", "notes"), ¬esTableCount) - testutils.MustScan(t, "counting books", - db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", "table", "books"), &booksTableCount) - testutils.MustScan(t, "counting system", - db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", "table", "system"), &systemTableCount) - - testutils.AssertEqual(t, notesTableCount, 1, "notes table count mismatch") - testutils.AssertEqual(t, booksTableCount, 1, "books table count mismatch") - testutils.AssertEqual(t, systemTableCount, 1, "system table count mismatch") - - // test that all default system configurations are generated - var lastUpgrade, lastMaxUSN, lastSyncAt string - testutils.MustScan(t, "scanning last upgrade", - db.QueryRow("SELECT value FROM system WHERE key = ?", infra.SystemLastUpgrade), &lastUpgrade) - testutils.MustScan(t, "scanning last max usn", - db.QueryRow("SELECT value FROM system WHERE key = ?", infra.SystemLastMaxUSN), &lastMaxUSN) - testutils.MustScan(t, "scanning last sync at", - db.QueryRow("SELECT value FROM system WHERE key = ?", infra.SystemLastSyncAt), &lastSyncAt) - - testutils.AssertNotEqual(t, lastUpgrade, "", "last upgrade should not be empty") - testutils.AssertNotEqual(t, lastMaxUSN, "", "last max usn should not be empty") - testutils.AssertNotEqual(t, lastSyncAt, "", "last sync at should not be empty") -} - -func TestAddNote_NewBook_BodyFlag(t *testing.T) { - // Set up - ctx := testutils.InitEnv(t, "./tmp", "./testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - // Execute - testutils.RunDnoteCmd(t, ctx, binaryName, "add", "js", "-c", "foo") - - // Test - db := ctx.DB - - var noteCount, bookCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - testutils.AssertEqualf(t, noteCount, 1, "note count mismatch") - - var book core.Book - testutils.MustScan(t, "getting book", db.QueryRow("SELECT uuid, dirty FROM books where label = ?", "js"), &book.UUID, &book.Dirty) - var note core.Note - testutils.MustScan(t, "getting note", - db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes where book_uuid = ?", book.UUID), ¬e.UUID, ¬e.Body, ¬e.AddedOn, ¬e.Dirty) - - testutils.AssertEqual(t, book.Dirty, true, "Book dirty mismatch") - - testutils.AssertNotEqual(t, note.UUID, "", "Note should have UUID") - testutils.AssertEqual(t, note.Body, "foo", "Note body mismatch") - testutils.AssertEqual(t, note.Dirty, true, "Note dirty mismatch") - testutils.AssertNotEqual(t, note.AddedOn, int64(0), "Note added_on mismatch") -} - -func TestAddNote_ExistingBook_BodyFlag(t *testing.T) { - // Set up - ctx := testutils.InitEnv(t, "./tmp", "./testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.Setup3(t, ctx) - - // Execute - testutils.RunDnoteCmd(t, ctx, binaryName, "add", "js", "-c", "foo") - - // Test - db := ctx.DB - - var noteCount, bookCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - testutils.AssertEqualf(t, noteCount, 2, "note count mismatch") - - var n1, n2 core.Note - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Dirty) - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes WHERE book_uuid = ? AND body = ?", "js-book-uuid", "foo"), &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Dirty) - - var book core.Book - testutils.MustScan(t, "getting book", db.QueryRow("SELECT dirty FROM books where label = ?", "js"), &book.Dirty) - - testutils.AssertEqual(t, book.Dirty, false, "Book dirty mismatch") - - testutils.AssertNotEqual(t, n1.UUID, "", "n1 should have UUID") - testutils.AssertEqual(t, n1.Body, "Booleans have toString()", "n1 body mismatch") - testutils.AssertEqual(t, n1.AddedOn, int64(1515199943), "n1 added_on mismatch") - testutils.AssertEqual(t, n1.Dirty, false, "n1 dirty mismatch") - - testutils.AssertNotEqual(t, n2.UUID, "", "n2 should have UUID") - testutils.AssertEqual(t, n2.Body, "foo", "n2 body mismatch") - testutils.AssertEqual(t, n2.Dirty, true, "n2 dirty mismatch") -} - -func TestEditNote_BodyFlag(t *testing.T) { - // Set up - ctx := testutils.InitEnv(t, "./tmp", "./testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.Setup4(t, ctx) - - // Execute - testutils.RunDnoteCmd(t, ctx, binaryName, "edit", "js", "2", "-c", "foo bar") - - // Test - db := ctx.DB - - var noteCount, bookCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - - testutils.AssertEqualf(t, bookCount, 1, "book count mismatch") - testutils.AssertEqualf(t, noteCount, 2, "note count mismatch") - - var n1, n2 core.Note - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes where book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Dirty) - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes where book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Dirty) - - testutils.AssertEqual(t, n1.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n1 should have UUID") - testutils.AssertEqual(t, n1.Body, "Booleans have toString()", "n1 body mismatch") - testutils.AssertEqual(t, n1.Dirty, false, "n1 dirty mismatch") - - testutils.AssertEqual(t, n2.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "Note should have UUID") - testutils.AssertEqual(t, n2.Body, "foo bar", "Note body mismatch") - testutils.AssertEqual(t, n2.Dirty, true, "n2 dirty mismatch") - testutils.AssertNotEqual(t, n2.EditedOn, 0, "Note edited_on mismatch") -} - -func TestRemoveNote(t *testing.T) { - // Set up - ctx := testutils.InitEnv(t, "./tmp", "./testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.Setup2(t, ctx) - - // Execute - testutils.WaitDnoteCmd(t, ctx, testutils.UserConfirm, binaryName, "remove", "js", "1") - - // Test - db := ctx.DB - - var noteCount, bookCount, jsNoteCount, linuxNoteCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount) - testutils.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount) - - testutils.AssertEqualf(t, bookCount, 2, "book count mismatch") - testutils.AssertEqualf(t, noteCount, 3, "note count mismatch") - testutils.AssertEqual(t, jsNoteCount, 2, "js book should have 2 notes") - testutils.AssertEqual(t, linuxNoteCount, 1, "linux book book should have 1 note") - - var b1, b2 core.Book - var n1, n2, n3 core.Note - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"), - &b1.Label, &b1.Deleted, &b1.USN) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"), - &b2.Label, &b2.Deleted, &b2.USN) - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), - &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN) - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), - &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN) - testutils.MustScan(t, "getting n3", - db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"), - &n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN) - - testutils.AssertEqual(t, b1.Label, "js", "b1 label mismatch") - testutils.AssertEqual(t, b1.Deleted, false, "b1 deleted mismatch") - testutils.AssertEqual(t, b1.Dirty, false, "b1 Dirty mismatch") - testutils.AssertEqual(t, b1.USN, 111, "b1 usn mismatch") - - testutils.AssertEqual(t, b2.Label, "linux", "b2 label mismatch") - testutils.AssertEqual(t, b2.Deleted, false, "b2 deleted mismatch") - testutils.AssertEqual(t, b2.Dirty, false, "b2 Dirty mismatch") - testutils.AssertEqual(t, b2.USN, 122, "b2 usn mismatch") - - testutils.AssertEqual(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID") - testutils.AssertEqual(t, n1.Body, "", "n1 body mismatch") - testutils.AssertEqual(t, n1.Deleted, true, "n1 deleted mismatch") - testutils.AssertEqual(t, n1.Dirty, true, "n1 Dirty mismatch") - testutils.AssertEqual(t, n1.USN, 11, "n1 usn mismatch") - - testutils.AssertEqual(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID") - testutils.AssertEqual(t, n2.Body, "n2 body", "n2 body mismatch") - testutils.AssertEqual(t, n2.Deleted, false, "n2 deleted mismatch") - testutils.AssertEqual(t, n2.Dirty, false, "n2 Dirty mismatch") - testutils.AssertEqual(t, n2.USN, 12, "n2 usn mismatch") - - testutils.AssertEqual(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID") - testutils.AssertEqual(t, n3.Body, "n3 body", "n3 body mismatch") - testutils.AssertEqual(t, n3.Deleted, false, "n3 deleted mismatch") - testutils.AssertEqual(t, n3.Dirty, false, "n3 Dirty mismatch") - testutils.AssertEqual(t, n3.USN, 13, "n3 usn mismatch") -} - -func TestRemoveBook(t *testing.T) { - // Set up - ctx := testutils.InitEnv(t, "./tmp", "./testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.Setup2(t, ctx) - - // Execute - testutils.WaitDnoteCmd(t, ctx, testutils.UserConfirm, binaryName, "remove", "-b", "js") - - // Test - db := ctx.DB - - var noteCount, bookCount, jsNoteCount, linuxNoteCount int - testutils.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) - testutils.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) - testutils.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount) - testutils.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount) - - testutils.AssertEqualf(t, bookCount, 2, "book count mismatch") - testutils.AssertEqualf(t, noteCount, 3, "note count mismatch") - testutils.AssertEqual(t, jsNoteCount, 2, "js book should have 2 notes") - testutils.AssertEqual(t, linuxNoteCount, 1, "linux book book should have 1 note") - - var b1, b2 core.Book - var n1, n2, n3 core.Note - testutils.MustScan(t, "getting b1", - db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"), - &b1.Label, &b1.Dirty, &b1.Deleted, &b1.USN) - testutils.MustScan(t, "getting b2", - db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"), - &b2.Label, &b2.Dirty, &b2.Deleted, &b2.USN) - testutils.MustScan(t, "getting n1", - db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), - &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN) - testutils.MustScan(t, "getting n2", - db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), - &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN) - testutils.MustScan(t, "getting n3", - db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"), - &n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN) - - testutils.AssertNotEqual(t, b1.Label, "js", "b1 label mismatch") - testutils.AssertEqual(t, b1.Dirty, true, "b1 Dirty mismatch") - testutils.AssertEqual(t, b1.Deleted, true, "b1 deleted mismatch") - testutils.AssertEqual(t, b1.USN, 111, "b1 usn mismatch") - - testutils.AssertEqual(t, b2.Label, "linux", "b2 label mismatch") - testutils.AssertEqual(t, b2.Dirty, false, "b2 Dirty mismatch") - testutils.AssertEqual(t, b2.Deleted, false, "b2 deleted mismatch") - testutils.AssertEqual(t, b2.USN, 122, "b2 usn mismatch") - - testutils.AssertEqual(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID") - testutils.AssertEqual(t, n1.Body, "", "n1 body mismatch") - testutils.AssertEqual(t, n1.Dirty, true, "n1 Dirty mismatch") - testutils.AssertEqual(t, n1.Deleted, true, "n1 deleted mismatch") - testutils.AssertEqual(t, n1.USN, 11, "n1 usn mismatch") - - testutils.AssertEqual(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID") - testutils.AssertEqual(t, n2.Body, "", "n2 body mismatch") - testutils.AssertEqual(t, n2.Dirty, true, "n2 Dirty mismatch") - testutils.AssertEqual(t, n2.Deleted, true, "n2 deleted mismatch") - testutils.AssertEqual(t, n2.USN, 12, "n2 usn mismatch") - - testutils.AssertEqual(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID") - testutils.AssertEqual(t, n3.Body, "n3 body", "n3 body mismatch") - testutils.AssertEqual(t, n3.Dirty, false, "n3 Dirty mismatch") - testutils.AssertEqual(t, n3.Deleted, false, "n3 deleted mismatch") - testutils.AssertEqual(t, n3.USN, 13, "n3 usn mismatch") -} diff --git a/cli/migrate/legacy_test.go b/cli/migrate/legacy_test.go deleted file mode 100644 index 13100036..00000000 --- a/cli/migrate/legacy_test.go +++ /dev/null @@ -1,608 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package migrate - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "os" - "path/filepath" - "reflect" - "testing" - - "github.com/dnote/dnote/cli/testutils" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" - "gopkg.in/yaml.v2" -) - -func TestMigrateToV1(t *testing.T) { - - t.Run("yaml exists", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - yamlPath, err := filepath.Abs(filepath.Join(ctx.HomeDir, ".dnote-yaml-archived")) - if err != nil { - panic(errors.Wrap(err, "Failed to get absolute YAML path").Error()) - } - ioutil.WriteFile(yamlPath, []byte{}, 0644) - - // execute - if err := migrateToV1(ctx); err != nil { - t.Fatal(errors.Wrapf(err, "Failed to migrate").Error()) - } - - // test - if utils.FileExists(yamlPath) { - t.Fatal("YAML archive file has not been deleted") - } - }) - - t.Run("yaml does not exist", func(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - yamlPath, err := filepath.Abs(filepath.Join(ctx.HomeDir, ".dnote-yaml-archived")) - if err != nil { - panic(errors.Wrap(err, "Failed to get absolute YAML path").Error()) - } - - // execute - if err := migrateToV1(ctx); err != nil { - t.Fatal(errors.Wrapf(err, "Failed to migrate").Error()) - } - - // test - if utils.FileExists(yamlPath) { - t.Fatal("YAML archive file must not exist") - } - }) -} - -func TestMigrateToV2(t *testing.T) { - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.CopyFixture(ctx, "./fixtures/legacy-2-pre-dnote.json", "dnote") - - // execute - if err := migrateToV2(ctx); err != nil { - t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) - } - - // test - b := testutils.ReadFile(ctx, "dnote") - - var postDnote migrateToV2PostDnote - if err := json.Unmarshal(b, &postDnote); err != nil { - t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) - } - - for _, book := range postDnote { - testutils.AssertNotEqual(t, book.Name, "", "Book name was not populated") - - for _, note := range book.Notes { - if len(note.UUID) == 8 { - t.Errorf("Note UUID was not migrated. It has length of %d", len(note.UUID)) - } - - testutils.AssertNotEqual(t, note.AddedOn, int64(0), "AddedOn was not carried over") - testutils.AssertEqual(t, note.EditedOn, int64(0), "EditedOn was not created properly") - } - } -} - -func TestMigrateToV3(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.CopyFixture(ctx, "./fixtures/legacy-3-pre-dnote.json", "dnote") - - // execute - if err := migrateToV3(ctx); err != nil { - t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) - } - - // test - b := testutils.ReadFile(ctx, "dnote") - var postDnote migrateToV3Dnote - if err := json.Unmarshal(b, &postDnote); err != nil { - t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) - } - - b = testutils.ReadFile(ctx, "actions") - var actions []migrateToV3Action - if err := json.Unmarshal(b, &actions); err != nil { - t.Fatal(errors.Wrap(err, "Failed to unmarshal the actions").Error()) - } - - testutils.AssertEqual(t, len(actions), 6, "actions length mismatch") - - for _, book := range postDnote { - for _, note := range book.Notes { - testutils.AssertNotEqual(t, note.AddedOn, int64(0), "AddedOn was not carried over") - } - } -} - -func TestMigrateToV4(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - defer os.Setenv("EDITOR", "") - - testutils.CopyFixture(ctx, "./fixtures/legacy-4-pre-dnoterc.yaml", "dnoterc") - - // execute - os.Setenv("EDITOR", "vim") - if err := migrateToV4(ctx); err != nil { - t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) - } - - // test - b := testutils.ReadFile(ctx, "dnoterc") - var config migrateToV4PostConfig - if err := yaml.Unmarshal(b, &config); err != nil { - t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) - } - - testutils.AssertEqual(t, config.APIKey, "Oev6e1082ORasdf9rjkfjkasdfjhgei", "api key mismatch") - testutils.AssertEqual(t, config.Editor, "vim", "editor mismatch") -} - -func TestMigrateToV5(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.CopyFixture(ctx, "./fixtures/legacy-5-pre-actions.json", "actions") - - // execute - if err := migrateToV5(ctx); err != nil { - t.Fatal(errors.Wrap(err, "migrating").Error()) - } - - // test - var oldActions []migrateToV5PreAction - testutils.ReadJSON("./fixtures/legacy-5-pre-actions.json", &oldActions) - - b := testutils.ReadFile(ctx, "actions") - var migratedActions []migrateToV5PostAction - if err := json.Unmarshal(b, &migratedActions); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling migrated actions").Error()) - } - - if len(oldActions) != len(migratedActions) { - t.Fatalf("There were %d actions but after migration there were %d", len(oldActions), len(migratedActions)) - } - - for idx := range migratedActions { - migrated := migratedActions[idx] - old := oldActions[idx] - - testutils.AssertNotEqual(t, migrated.UUID, "", fmt.Sprintf("uuid mismatch for migrated item with index %d", idx)) - testutils.AssertEqual(t, migrated.Schema, 1, fmt.Sprintf("schema mismatch for migrated item with index %d", idx)) - testutils.AssertEqual(t, migrated.Timestamp, old.Timestamp, fmt.Sprintf("timestamp mismatch for migrated item with index %d", idx)) - testutils.AssertEqual(t, migrated.Type, old.Type, fmt.Sprintf("timestamp mismatch for migrated item with index %d", idx)) - - switch migrated.Type { - case migrateToV5ActionAddNote: - var oldData, migratedData migrateToV5AddNoteData - if err := json.Unmarshal(old.Data, &oldData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) - } - if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) - } - - testutils.AssertEqual(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) - testutils.AssertEqual(t, oldData.Content, migratedData.Content, fmt.Sprintf("data content mismatch for item idx %d", idx)) - testutils.AssertEqual(t, oldData.NoteUUID, migratedData.NoteUUID, fmt.Sprintf("data note_uuid mismatch for item idx %d", idx)) - case migrateToV5ActionRemoveNote: - var oldData, migratedData migrateToV5RemoveNoteData - if err := json.Unmarshal(old.Data, &oldData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) - } - if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) - } - - testutils.AssertEqual(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) - testutils.AssertEqual(t, oldData.NoteUUID, migratedData.NoteUUID, fmt.Sprintf("data note_uuid mismatch for item idx %d", idx)) - case migrateToV5ActionAddBook: - var oldData, migratedData migrateToV5AddBookData - if err := json.Unmarshal(old.Data, &oldData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) - } - if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) - } - - testutils.AssertEqual(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) - case migrateToV5ActionRemoveBook: - var oldData, migratedData migrateToV5RemoveBookData - if err := json.Unmarshal(old.Data, &oldData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) - } - if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) - } - - testutils.AssertEqual(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) - case migrateToV5ActionEditNote: - var oldData migrateToV5PreEditNoteData - var migratedData migrateToV5PostEditNoteData - if err := json.Unmarshal(old.Data, &oldData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) - } - if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) - } - - testutils.AssertEqual(t, oldData.NoteUUID, migratedData.NoteUUID, fmt.Sprintf("data note_uuid mismatch for item idx %d", idx)) - testutils.AssertEqual(t, oldData.Content, migratedData.Content, fmt.Sprintf("data content mismatch for item idx %d", idx)) - testutils.AssertEqual(t, oldData.BookName, migratedData.FromBook, "book_name should have been renamed to from_book") - testutils.AssertEqual(t, migratedData.ToBook, "", "to_book should be empty") - } - } -} - -func TestMigrateToV6(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.CopyFixture(ctx, "./fixtures/legacy-6-pre-dnote.json", "dnote") - - // execute - if err := migrateToV6(ctx); err != nil { - t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) - } - - // test - b := testutils.ReadFile(ctx, "dnote") - var got migrateToV6PostDnote - if err := json.Unmarshal(b, &got); err != nil { - t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) - } - - b = testutils.ReadFileAbs("./fixtures/legacy-6-post-dnote.json") - var expected migrateToV6PostDnote - if err := json.Unmarshal(b, &expected); err != nil { - t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) - } - - if ok := reflect.DeepEqual(expected, got); !ok { - t.Errorf("Payload does not match.\nActual: %+v\nExpected: %+v", got, expected) - } -} - -func TestMigrateToV7(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", true) - defer testutils.TeardownEnv(ctx) - - testutils.CopyFixture(ctx, "./fixtures/legacy-7-pre-actions.json", "actions") - - // execute - if err := migrateToV7(ctx); err != nil { - t.Fatal(errors.Wrap(err, "migrating").Error()) - } - - // test - b := testutils.ReadFile(ctx, "actions") - var got []migrateToV7Action - if err := json.Unmarshal(b, &got); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling the result").Error()) - } - - b2 := testutils.ReadFileAbs("./fixtures/legacy-7-post-actions.json") - var expected []migrateToV7Action - if err := json.Unmarshal(b, &expected); err != nil { - t.Fatal(errors.Wrap(err, "unmarshalling the result into Dnote").Error()) - } - - ok, err := testutils.IsEqualJSON(b, b2) - if err != nil { - t.Fatal(errors.Wrap(err, "comparing JSON").Error()) - } - - if !ok { - t.Errorf("Result does not match.\nActual: %+v\nExpected: %+v", got, expected) - } -} - -func TestMigrateToV8(t *testing.T) { - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-1-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - // set up - testutils.CopyFixture(ctx, "./fixtures/legacy-8-actions.json", "actions") - testutils.CopyFixture(ctx, "./fixtures/legacy-8-dnote.json", "dnote") - testutils.CopyFixture(ctx, "./fixtures/legacy-8-dnoterc.yaml", "dnoterc") - testutils.CopyFixture(ctx, "./fixtures/legacy-8-schema.yaml", "schema") - testutils.CopyFixture(ctx, "./fixtures/legacy-8-timestamps.yaml", "timestamps") - - // execute - if err := migrateToV8(ctx); err != nil { - t.Fatal(errors.Wrap(err, "migrating").Error()) - } - - // test - - // 1. test if files are migrated - dnoteFilePath := fmt.Sprintf("%s/dnote", ctx.DnoteDir) - dnotercPath := fmt.Sprintf("%s/dnoterc", ctx.DnoteDir) - schemaFilePath := fmt.Sprintf("%s/schema", ctx.DnoteDir) - timestampFilePath := fmt.Sprintf("%s/timestamps", ctx.DnoteDir) - if ok := utils.FileExists(dnoteFilePath); ok { - t.Errorf("%s still exists", dnoteFilePath) - } - if ok := utils.FileExists(schemaFilePath); ok { - t.Errorf("%s still exists", dnoteFilePath) - } - if ok := utils.FileExists(timestampFilePath); ok { - t.Errorf("%s still exists", dnoteFilePath) - } - if ok := utils.FileExists(dnotercPath); !ok { - t.Errorf("%s should exist", dnotercPath) - } - - // 2. test if notes and books are migrated - db := ctx.DB - - var bookCount, noteCount int - err := db.QueryRow("SELECT count(*) FROM books").Scan(&bookCount) - if err != nil { - panic(errors.Wrap(err, "counting books")) - } - err = db.QueryRow("SELECT count(*) FROM notes").Scan(¬eCount) - if err != nil { - panic(errors.Wrap(err, "counting notes")) - } - testutils.AssertEqual(t, bookCount, 2, "book count mismatch") - testutils.AssertEqual(t, noteCount, 3, "note count mismatch") - - type bookInfo struct { - label string - uuid string - } - type noteInfo struct { - id int - uuid string - bookUUID string - content string - addedOn int64 - editedOn int64 - public bool - } - - var b1, b2 bookInfo - var n1, n2, n3 noteInfo - err = db.QueryRow("SELECT label, uuid FROM books WHERE label = ?", "js").Scan(&b1.label, &b1.uuid) - if err != nil { - panic(errors.Wrap(err, "finding book 1")) - } - err = db.QueryRow("SELECT label, uuid FROM books WHERE label = ?", "css").Scan(&b2.label, &b2.uuid) - if err != nil { - panic(errors.Wrap(err, "finding book 2")) - } - err = db.QueryRow("SELECT id, uuid, book_uuid, content, added_on, edited_on, public FROM notes WHERE uuid = ?", "d69edb54-5b31-4cdd-a4a5-34f0a0bfa153").Scan(&n1.id, &n1.uuid, &n1.bookUUID, &n1.content, &n1.addedOn, &n1.editedOn, &n1.public) - if err != nil { - panic(errors.Wrap(err, "finding note 1")) - } - err = db.QueryRow("SELECT id, uuid, book_uuid, content, added_on, edited_on, public FROM notes WHERE uuid = ?", "35cbcab1-6a2a-4cc8-97e0-e73bbbd54626").Scan(&n2.id, &n2.uuid, &n2.bookUUID, &n2.content, &n2.addedOn, &n2.editedOn, &n2.public) - if err != nil { - panic(errors.Wrap(err, "finding note 2")) - } - err = db.QueryRow("SELECT id, uuid, book_uuid, content, added_on, edited_on, public FROM notes WHERE uuid = ?", "7c1fcfb2-de8b-4350-88f0-fb3cbaf6630a").Scan(&n3.id, &n3.uuid, &n3.bookUUID, &n3.content, &n3.addedOn, &n3.editedOn, &n3.public) - if err != nil { - panic(errors.Wrap(err, "finding note 3")) - } - - testutils.AssertNotEqual(t, b1.uuid, "", "book 1 uuid should have been generated") - testutils.AssertEqual(t, b1.label, "js", "book 1 label mismatch") - testutils.AssertNotEqual(t, b2.uuid, "", "book 2 uuid should have been generated") - testutils.AssertEqual(t, b2.label, "css", "book 2 label mismatch") - - testutils.AssertEqual(t, n1.uuid, "d69edb54-5b31-4cdd-a4a5-34f0a0bfa153", "note 1 uuid mismatch") - testutils.AssertNotEqual(t, n1.id, 0, "note 1 id should have been generated") - testutils.AssertEqual(t, n1.bookUUID, b2.uuid, "note 1 book_uuid mismatch") - testutils.AssertEqual(t, n1.content, "css test 1", "note 1 content mismatch") - testutils.AssertEqual(t, n1.addedOn, int64(1536977237), "note 1 added_on mismatch") - testutils.AssertEqual(t, n1.editedOn, int64(1536977253), "note 1 edited_on mismatch") - testutils.AssertEqual(t, n1.public, false, "note 1 public mismatch") - - testutils.AssertEqual(t, n2.uuid, "35cbcab1-6a2a-4cc8-97e0-e73bbbd54626", "note 2 uuid mismatch") - testutils.AssertNotEqual(t, n2.id, 0, "note 2 id should have been generated") - testutils.AssertEqual(t, n2.bookUUID, b1.uuid, "note 2 book_uuid mismatch") - testutils.AssertEqual(t, n2.content, "js test 1", "note 2 content mismatch") - testutils.AssertEqual(t, n2.addedOn, int64(1536977229), "note 2 added_on mismatch") - testutils.AssertEqual(t, n2.editedOn, int64(0), "note 2 edited_on mismatch") - testutils.AssertEqual(t, n2.public, false, "note 2 public mismatch") - - testutils.AssertEqual(t, n3.uuid, "7c1fcfb2-de8b-4350-88f0-fb3cbaf6630a", "note 3 uuid mismatch") - testutils.AssertNotEqual(t, n3.id, 0, "note 3 id should have been generated") - testutils.AssertEqual(t, n3.bookUUID, b1.uuid, "note 3 book_uuid mismatch") - testutils.AssertEqual(t, n3.content, "js test 2", "note 3 content mismatch") - testutils.AssertEqual(t, n3.addedOn, int64(1536977230), "note 3 added_on mismatch") - testutils.AssertEqual(t, n3.editedOn, int64(0), "note 3 edited_on mismatch") - testutils.AssertEqual(t, n3.public, false, "note 3 public mismatch") - - // 3. test if actions are migrated - var actionCount int - err = db.QueryRow("SELECT count(*) FROM actions").Scan(&actionCount) - if err != nil { - panic(errors.Wrap(err, "counting actions")) - } - - testutils.AssertEqual(t, actionCount, 11, "action count mismatch") - - type actionInfo struct { - uuid string - schema int - actionType string - data string - timestamp int - } - - var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11 actionInfo - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "6145c1b7-f286-4d9f-b0f6-00d274baefc6").Scan(&a1.uuid, &a1.schema, &a1.actionType, &a1.data, &a1.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a1")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "c048a56b-179c-4f31-9995-81e9b32b7dd6").Scan(&a2.uuid, &a2.schema, &a2.actionType, &a2.data, &a2.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a2")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "f557ef48-c304-47dc-adfb-46b7306e701f").Scan(&a3.uuid, &a3.schema, &a3.actionType, &a3.data, &a3.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a3")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "8d79db34-343d-4331-ae5b-24743f17ca7f").Scan(&a4.uuid, &a4.schema, &a4.actionType, &a4.data, &a4.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a4")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "b9c1ed4a-e6b3-41f2-983b-593ec7b8b7a1").Scan(&a5.uuid, &a5.schema, &a5.actionType, &a5.data, &a5.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a5")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "06ed7ef0-f171-4bd7-ae8e-97b5d06a4c49").Scan(&a6.uuid, &a6.schema, &a6.actionType, &a6.data, &a6.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a6")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "7f173cef-1688-4177-a373-145fcd822b2f").Scan(&a7.uuid, &a7.schema, &a7.actionType, &a7.data, &a7.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a7")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "64352e08-aa7a-45f4-b760-b3f38b5e11fa").Scan(&a8.uuid, &a8.schema, &a8.actionType, &a8.data, &a8.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a8")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "82e20a12-bda8-45f7-ac42-b453b6daa5ec").Scan(&a9.uuid, &a9.schema, &a9.actionType, &a9.data, &a9.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a9")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "a29055f4-ace4-44fd-8800-3396edbccaef").Scan(&a10.uuid, &a10.schema, &a10.actionType, &a10.data, &a10.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a10")) - } - err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "871a5562-1bd0-43c1-b550-5bbb727ac7c4").Scan(&a11.uuid, &a11.schema, &a11.actionType, &a11.data, &a11.timestamp) - if err != nil { - panic(errors.Wrap(err, "finding a11")) - } - - testutils.AssertEqual(t, a1.uuid, "6145c1b7-f286-4d9f-b0f6-00d274baefc6", "action 1 uuid mismatch") - testutils.AssertEqual(t, a1.schema, 1, "action 1 schema mismatch") - testutils.AssertEqual(t, a1.actionType, "add_book", "action 1 type mismatch") - testutils.AssertEqual(t, a1.data, `{"book_name":"js"}`, "action 1 data mismatch") - testutils.AssertEqual(t, a1.timestamp, 1536977229, "action 1 timestamp mismatch") - - testutils.AssertEqual(t, a2.uuid, "c048a56b-179c-4f31-9995-81e9b32b7dd6", "action 2 uuid mismatch") - testutils.AssertEqual(t, a2.schema, 2, "action 2 schema mismatch") - testutils.AssertEqual(t, a2.actionType, "add_note", "action 2 type mismatch") - testutils.AssertEqual(t, a2.data, `{"note_uuid":"35cbcab1-6a2a-4cc8-97e0-e73bbbd54626","book_name":"js","content":"js test 1","public":false}`, "action 2 data mismatch") - testutils.AssertEqual(t, a2.timestamp, 1536977229, "action 2 timestamp mismatch") - - testutils.AssertEqual(t, a3.uuid, "f557ef48-c304-47dc-adfb-46b7306e701f", "action 3 uuid mismatch") - testutils.AssertEqual(t, a3.schema, 2, "action 3 schema mismatch") - testutils.AssertEqual(t, a3.actionType, "add_note", "action 3 type mismatch") - testutils.AssertEqual(t, a3.data, `{"note_uuid":"7c1fcfb2-de8b-4350-88f0-fb3cbaf6630a","book_name":"js","content":"js test 2","public":false}`, "action 3 data mismatch") - testutils.AssertEqual(t, a3.timestamp, 1536977230, "action 3 timestamp mismatch") - - testutils.AssertEqual(t, a4.uuid, "8d79db34-343d-4331-ae5b-24743f17ca7f", "action 4 uuid mismatch") - testutils.AssertEqual(t, a4.schema, 2, "action 4 schema mismatch") - testutils.AssertEqual(t, a4.actionType, "add_note", "action 4 type mismatch") - testutils.AssertEqual(t, a4.data, `{"note_uuid":"b23a88ba-b291-4294-9795-86b394db5dcf","book_name":"js","content":"js test 3","public":false}`, "action 4 data mismatch") - testutils.AssertEqual(t, a4.timestamp, 1536977234, "action 4 timestamp mismatch") - - testutils.AssertEqual(t, a5.uuid, "b9c1ed4a-e6b3-41f2-983b-593ec7b8b7a1", "action 5 uuid mismatch") - testutils.AssertEqual(t, a5.schema, 1, "action 5 schema mismatch") - testutils.AssertEqual(t, a5.actionType, "add_book", "action 5 type mismatch") - testutils.AssertEqual(t, a5.data, `{"book_name":"css"}`, "action 5 data mismatch") - testutils.AssertEqual(t, a5.timestamp, 1536977237, "action 5 timestamp mismatch") - - testutils.AssertEqual(t, a6.uuid, "06ed7ef0-f171-4bd7-ae8e-97b5d06a4c49", "action 6 uuid mismatch") - testutils.AssertEqual(t, a6.schema, 2, "action 6 schema mismatch") - testutils.AssertEqual(t, a6.actionType, "add_note", "action 6 type mismatch") - testutils.AssertEqual(t, a6.data, `{"note_uuid":"d69edb54-5b31-4cdd-a4a5-34f0a0bfa153","book_name":"css","content":"js test 3","public":false}`, "action 6 data mismatch") - testutils.AssertEqual(t, a6.timestamp, 1536977237, "action 6 timestamp mismatch") - - testutils.AssertEqual(t, a7.uuid, "7f173cef-1688-4177-a373-145fcd822b2f", "action 7 uuid mismatch") - testutils.AssertEqual(t, a7.schema, 2, "action 7 schema mismatch") - testutils.AssertEqual(t, a7.actionType, "edit_note", "action 7 type mismatch") - testutils.AssertEqual(t, a7.data, `{"note_uuid":"d69edb54-5b31-4cdd-a4a5-34f0a0bfa153","from_book":"css","to_book":null,"content":"css test 1","public":null}`, "action 7 data mismatch") - testutils.AssertEqual(t, a7.timestamp, 1536977253, "action 7 timestamp mismatch") - - testutils.AssertEqual(t, a8.uuid, "64352e08-aa7a-45f4-b760-b3f38b5e11fa", "action 8 uuid mismatch") - testutils.AssertEqual(t, a8.schema, 1, "action 8 schema mismatch") - testutils.AssertEqual(t, a8.actionType, "add_book", "action 8 type mismatch") - testutils.AssertEqual(t, a8.data, `{"book_name":"sql"}`, "action 8 data mismatch") - testutils.AssertEqual(t, a8.timestamp, 1536977261, "action 8 timestamp mismatch") - - testutils.AssertEqual(t, a9.uuid, "82e20a12-bda8-45f7-ac42-b453b6daa5ec", "action 9 uuid mismatch") - testutils.AssertEqual(t, a9.schema, 2, "action 9 schema mismatch") - testutils.AssertEqual(t, a9.actionType, "add_note", "action 9 type mismatch") - testutils.AssertEqual(t, a9.data, `{"note_uuid":"2f47d390-685b-4b84-89ac-704c6fb8d3fb","book_name":"sql","content":"blah","public":false}`, "action 9 data mismatch") - testutils.AssertEqual(t, a9.timestamp, 1536977261, "action 9 timestamp mismatch") - - testutils.AssertEqual(t, a10.uuid, "a29055f4-ace4-44fd-8800-3396edbccaef", "action 10 uuid mismatch") - testutils.AssertEqual(t, a10.schema, 1, "action 10 schema mismatch") - testutils.AssertEqual(t, a10.actionType, "remove_book", "action 10 type mismatch") - testutils.AssertEqual(t, a10.data, `{"book_name":"sql"}`, "action 10 data mismatch") - testutils.AssertEqual(t, a10.timestamp, 1536977268, "action 10 timestamp mismatch") - - testutils.AssertEqual(t, a11.uuid, "871a5562-1bd0-43c1-b550-5bbb727ac7c4", "action 11 uuid mismatch") - testutils.AssertEqual(t, a11.schema, 1, "action 11 schema mismatch") - testutils.AssertEqual(t, a11.actionType, "remove_note", "action 11 type mismatch") - testutils.AssertEqual(t, a11.data, `{"note_uuid":"b23a88ba-b291-4294-9795-86b394db5dcf","book_name":"js"}`, "action 11 data mismatch") - testutils.AssertEqual(t, a11.timestamp, 1536977274, "action 11 timestamp mismatch") - - // 3. test if system is migrated - var systemCount int - err = db.QueryRow("SELECT count(*) FROM system").Scan(&systemCount) - if err != nil { - panic(errors.Wrap(err, "counting system")) - } - - testutils.AssertEqual(t, systemCount, 3, "action count mismatch") - - var lastUpgrade, lastAction, bookmark int - err = db.QueryRow("SELECT value FROM system WHERE key = ?", "last_upgrade").Scan(&lastUpgrade) - if err != nil { - panic(errors.Wrap(err, "finding last_upgrade")) - } - err = db.QueryRow("SELECT value FROM system WHERE key = ?", "last_action").Scan(&lastAction) - if err != nil { - panic(errors.Wrap(err, "finding last_action")) - } - err = db.QueryRow("SELECT value FROM system WHERE key = ?", "bookmark").Scan(&bookmark) - if err != nil { - panic(errors.Wrap(err, "finding bookmark")) - } - - testutils.AssertEqual(t, lastUpgrade, 1536977220, "last_upgrade mismatch") - testutils.AssertEqual(t, lastAction, 1536977274, "last_action mismatch") - testutils.AssertEqual(t, bookmark, 9, "bookmark mismatch") -} diff --git a/cli/migrate/migrate_test.go b/cli/migrate/migrate_test.go deleted file mode 100644 index 3173d256..00000000 --- a/cli/migrate/migrate_test.go +++ /dev/null @@ -1,965 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package migrate - -import ( - "encoding/json" - "fmt" - "net/http" - "net/http/httptest" - "testing" - "time" - - "github.com/dnote/actions" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/testutils" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" -) - -func TestExecute_bump_schema(t *testing.T) { - testCases := []struct { - schemaKey string - }{ - { - schemaKey: infra.SystemSchema, - }, - { - schemaKey: infra.SystemRemoteSchema, - }, - } - - for _, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 8) - - m1 := migration{ - name: "noop", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - return nil - }, - } - m2 := migration{ - name: "noop", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - return nil - }, - } - - // execute - err := execute(ctx, m1, tc.schemaKey) - if err != nil { - t.Fatal(errors.Wrap(err, "failed to execute")) - } - err = execute(ctx, m2, tc.schemaKey) - if err != nil { - t.Fatal(errors.Wrap(err, "failed to execute")) - } - - // test - var schema int - testutils.MustScan(t, "getting schema", db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) - testutils.AssertEqual(t, schema, 10, "schema was not incremented properly") - }() - } -} - -func TestRun_nonfresh(t *testing.T) { - testCases := []struct { - mode int - schemaKey string - }{ - { - mode: LocalMode, - schemaKey: infra.SystemSchema, - }, - { - mode: RemoteMode, - schemaKey: infra.SystemRemoteSchema, - }, - } - - for _, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 2) - testutils.MustExec(t, "creating a temporary table for testing", db, - "CREATE TABLE migrate_run_test ( name string )") - - sequence := []migration{ - migration{ - name: "v1", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v1 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v1") - return nil - }, - }, - migration{ - name: "v2", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v2 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v2") - return nil - }, - }, - migration{ - name: "v3", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v3 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v3") - return nil - }, - }, - migration{ - name: "v4", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v4 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v4") - return nil - }, - }, - } - - // execute - err := Run(ctx, sequence, tc.mode) - if err != nil { - t.Fatal(errors.Wrap(err, "failed to run")) - } - - // test - var schema int - testutils.MustScan(t, fmt.Sprintf("getting schema for %s", tc.schemaKey), db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) - testutils.AssertEqual(t, schema, 4, fmt.Sprintf("schema was not updated for %s", tc.schemaKey)) - - var testRunCount int - testutils.MustScan(t, "counting test runs", db.QueryRow("SELECT count(*) FROM migrate_run_test"), &testRunCount) - testutils.AssertEqual(t, testRunCount, 2, "test run count mismatch") - - var testRun1, testRun2 string - testutils.MustScan(t, "finding test run 1", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v3"), &testRun1) - testutils.MustScan(t, "finding test run 2", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v4"), &testRun2) - }() - } - -} - -func TestRun_fresh(t *testing.T) { - testCases := []struct { - mode int - schemaKey string - }{ - { - mode: LocalMode, - schemaKey: infra.SystemSchema, - }, - { - mode: RemoteMode, - schemaKey: infra.SystemRemoteSchema, - }, - } - - for _, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "creating a temporary table for testing", db, - "CREATE TABLE migrate_run_test ( name string )") - - sequence := []migration{ - migration{ - name: "v1", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v1 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v1") - return nil - }, - }, - migration{ - name: "v2", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v2 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v2") - return nil - }, - }, - migration{ - name: "v3", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v3 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v3") - return nil - }, - }, - } - - // execute - err := Run(ctx, sequence, tc.mode) - if err != nil { - t.Fatal(errors.Wrap(err, "failed to run")) - } - - // test - var schema int - testutils.MustScan(t, "getting schema", db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) - testutils.AssertEqual(t, schema, 3, "schema was not updated") - - var testRunCount int - testutils.MustScan(t, "counting test runs", db.QueryRow("SELECT count(*) FROM migrate_run_test"), &testRunCount) - testutils.AssertEqual(t, testRunCount, 3, "test run count mismatch") - - var testRun1, testRun2, testRun3 string - testutils.MustScan(t, "finding test run 1", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v1"), &testRun1) - testutils.MustScan(t, "finding test run 2", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v2"), &testRun2) - testutils.MustScan(t, "finding test run 2", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v3"), &testRun3) - }() - } -} - -func TestRun_up_to_date(t *testing.T) { - testCases := []struct { - mode int - schemaKey string - }{ - { - mode: LocalMode, - schemaKey: infra.SystemSchema, - }, - { - mode: RemoteMode, - schemaKey: infra.SystemRemoteSchema, - }, - } - - for _, tc := range testCases { - func() { - // set up - ctx := testutils.InitEnv(t, "../tmp", "../testutils/fixtures/schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - testutils.MustExec(t, "creating a temporary table for testing", db, - "CREATE TABLE migrate_run_test ( name string )") - - testutils.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 3) - - sequence := []migration{ - migration{ - name: "v1", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v1 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v1") - return nil - }, - }, - migration{ - name: "v2", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v2 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v2") - return nil - }, - }, - migration{ - name: "v3", - run: func(ctx infra.DnoteCtx, db *infra.DB) error { - testutils.MustExec(t, "marking v3 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v3") - return nil - }, - }, - } - - // execute - err := Run(ctx, sequence, tc.mode) - if err != nil { - t.Fatal(errors.Wrap(err, "failed to run")) - } - - // test - var schema int - testutils.MustScan(t, "getting schema", db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) - testutils.AssertEqual(t, schema, 3, "schema was not updated") - - var testRunCount int - testutils.MustScan(t, "counting test runs", db.QueryRow("SELECT count(*) FROM migrate_run_test"), &testRunCount) - testutils.AssertEqual(t, testRunCount, 0, "test run count mismatch") - }() - } -} - -func TestLocalMigration1(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-1-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) - a1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 1, "add_book", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.EditNoteDataV1{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: "", Content: "note 1"}) - a2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 1, "edit_note", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.EditNoteDataV1{NoteUUID: "note-2-uuid", FromBook: "js", ToBook: "", Content: "note 2"}) - a3UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 1, "edit_note", string(data), 1537829463) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm1.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var actionCount int - testutils.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount) - testutils.AssertEqual(t, actionCount, 3, "action count mismatch") - - var a1, a2, a3 actions.Action - testutils.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID), - &a1.Schema, &a1.Type, &a1.Data, &a1.Timestamp) - testutils.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID), - &a2.Schema, &a2.Type, &a2.Data, &a2.Timestamp) - testutils.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID), - &a3.Schema, &a3.Type, &a3.Data, &a3.Timestamp) - - var a1Data actions.AddBookDataV1 - var a2Data, a3Data actions.EditNoteDataV3 - testutils.MustUnmarshalJSON(t, a1.Data, &a1Data) - testutils.MustUnmarshalJSON(t, a2.Data, &a2Data) - testutils.MustUnmarshalJSON(t, a3.Data, &a3Data) - - testutils.AssertEqual(t, a1.Schema, 1, "a1 schema mismatch") - testutils.AssertEqual(t, a1.Type, "add_book", "a1 type mismatch") - testutils.AssertEqual(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch") - testutils.AssertEqual(t, a1Data.BookName, "js", "a1 data book_name mismatch") - - testutils.AssertEqual(t, a2.Schema, 3, "a2 schema mismatch") - testutils.AssertEqual(t, a2.Type, "edit_note", "a2 type mismatch") - testutils.AssertEqual(t, a2.Timestamp, int64(1537829463), "a2 timestamp mismatch") - testutils.AssertEqual(t, a2Data.NoteUUID, "note-1-uuid", "a2 data note_uuid mismatch") - testutils.AssertEqual(t, a2Data.BookName, (*string)(nil), "a2 data book_name mismatch") - testutils.AssertEqual(t, *a2Data.Content, "note 1", "a2 data content mismatch") - testutils.AssertEqual(t, *a2Data.Public, false, "a2 data public mismatch") - - testutils.AssertEqual(t, a3.Schema, 3, "a3 schema mismatch") - testutils.AssertEqual(t, a3.Type, "edit_note", "a3 type mismatch") - testutils.AssertEqual(t, a3.Timestamp, int64(1537829463), "a3 timestamp mismatch") - testutils.AssertEqual(t, a3Data.NoteUUID, "note-2-uuid", "a3 data note_uuid mismatch") - testutils.AssertEqual(t, a3Data.BookName, (*string)(nil), "a3 data book_name mismatch") - testutils.AssertEqual(t, *a3Data.Content, "note 2", "a3 data content mismatch") - testutils.AssertEqual(t, *a3Data.Public, false, "a3 data public mismatch") -} - -func TestLocalMigration2(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-1-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - c1 := "note 1 - v1" - c2 := "note 1 - v2" - css := "css" - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") - - data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false}) - a1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 2, "add_note", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.EditNoteDataV2{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: nil, Content: &c1, Public: nil}) - a2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 2, "edit_note", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.EditNoteDataV2{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: &css, Content: &c2, Public: nil}) - a3UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 2, "edit_note", string(data), 1537829463) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm2.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var actionCount int - testutils.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount) - testutils.AssertEqual(t, actionCount, 3, "action count mismatch") - - var a1, a2, a3 actions.Action - testutils.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID), - &a1.Schema, &a1.Type, &a1.Data, &a1.Timestamp) - testutils.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID), - &a2.Schema, &a2.Type, &a2.Data, &a2.Timestamp) - testutils.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID), - &a3.Schema, &a3.Type, &a3.Data, &a3.Timestamp) - - var a1Data actions.AddNoteDataV2 - var a2Data, a3Data actions.EditNoteDataV3 - testutils.MustUnmarshalJSON(t, a1.Data, &a1Data) - testutils.MustUnmarshalJSON(t, a2.Data, &a2Data) - testutils.MustUnmarshalJSON(t, a3.Data, &a3Data) - - testutils.AssertEqual(t, a1.Schema, 2, "a1 schema mismatch") - testutils.AssertEqual(t, a1.Type, "add_note", "a1 type mismatch") - testutils.AssertEqual(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch") - testutils.AssertEqual(t, a1Data.NoteUUID, "note-1-uuid", "a1 data note_uuid mismatch") - testutils.AssertEqual(t, a1Data.BookName, "js", "a1 data book_name mismatch") - testutils.AssertEqual(t, a1Data.Public, false, "a1 data public mismatch") - - testutils.AssertEqual(t, a2.Schema, 3, "a2 schema mismatch") - testutils.AssertEqual(t, a2.Type, "edit_note", "a2 type mismatch") - testutils.AssertEqual(t, a2.Timestamp, int64(1537829463), "a2 timestamp mismatch") - testutils.AssertEqual(t, a2Data.NoteUUID, "note-1-uuid", "a2 data note_uuid mismatch") - testutils.AssertEqual(t, a2Data.BookName, (*string)(nil), "a2 data book_name mismatch") - testutils.AssertEqual(t, *a2Data.Content, c1, "a2 data content mismatch") - testutils.AssertEqual(t, a2Data.Public, (*bool)(nil), "a2 data public mismatch") - - testutils.AssertEqual(t, a3.Schema, 3, "a3 schema mismatch") - testutils.AssertEqual(t, a3.Type, "edit_note", "a3 type mismatch") - testutils.AssertEqual(t, a3.Timestamp, int64(1537829463), "a3 timestamp mismatch") - testutils.AssertEqual(t, a3Data.NoteUUID, "note-1-uuid", "a3 data note_uuid mismatch") - testutils.AssertEqual(t, *a3Data.BookName, "css", "a3 data book_name mismatch") - testutils.AssertEqual(t, *a3Data.Content, c2, "a3 data content mismatch") - testutils.AssertEqual(t, a3Data.Public, (*bool)(nil), "a3 data public mismatch") -} - -func TestLocalMigration3(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-1-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false}) - a1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 2, "add_note", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.RemoveNoteDataV1{NoteUUID: "note-1-uuid", BookName: "js"}) - a2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 1, "remove_note", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.RemoveNoteDataV1{NoteUUID: "note-2-uuid", BookName: "js"}) - a3UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 1, "remove_note", string(data), 1537829463) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm3.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var actionCount int - testutils.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount) - testutils.AssertEqual(t, actionCount, 3, "action count mismatch") - - var a1, a2, a3 actions.Action - testutils.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID), - &a1.Schema, &a1.Type, &a1.Data, &a1.Timestamp) - testutils.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID), - &a2.Schema, &a2.Type, &a2.Data, &a2.Timestamp) - testutils.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID), - &a3.Schema, &a3.Type, &a3.Data, &a3.Timestamp) - - var a1Data actions.AddNoteDataV2 - var a2Data, a3Data actions.RemoveNoteDataV2 - testutils.MustUnmarshalJSON(t, a1.Data, &a1Data) - testutils.MustUnmarshalJSON(t, a2.Data, &a2Data) - testutils.MustUnmarshalJSON(t, a3.Data, &a3Data) - - testutils.AssertEqual(t, a1.Schema, 2, "a1 schema mismatch") - testutils.AssertEqual(t, a1.Type, "add_note", "a1 type mismatch") - testutils.AssertEqual(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch") - testutils.AssertEqual(t, a1Data.NoteUUID, "note-1-uuid", "a1 data note_uuid mismatch") - testutils.AssertEqual(t, a1Data.BookName, "js", "a1 data book_name mismatch") - testutils.AssertEqual(t, a1Data.Content, "note 1", "a1 data content mismatch") - testutils.AssertEqual(t, a1Data.Public, false, "a1 data public mismatch") - - testutils.AssertEqual(t, a2.Schema, 2, "a2 schema mismatch") - testutils.AssertEqual(t, a2.Type, "remove_note", "a2 type mismatch") - testutils.AssertEqual(t, a2.Timestamp, int64(1537829463), "a2 timestamp mismatch") - testutils.AssertEqual(t, a2Data.NoteUUID, "note-1-uuid", "a2 data note_uuid mismatch") - - testutils.AssertEqual(t, a3.Schema, 2, "a3 schema mismatch") - testutils.AssertEqual(t, a3.Type, "remove_note", "a3 type mismatch") - testutils.AssertEqual(t, a3.Timestamp, int64(1537829463), "a3 timestamp mismatch") - testutils.AssertEqual(t, a3Data.NoteUUID, "note-2-uuid", "a3 data note_uuid mismatch") -} - -func TestLocalMigration4(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-1-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n1UUID, b1UUID, "n1 content", time.Now().UnixNano()) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm4.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var n1Dirty, b1Dirty bool - var n1Deleted, b1Deleted bool - var n1USN, b1USN int - testutils.MustScan(t, "scanning the newly added dirty flag of n1", db.QueryRow("SELECT dirty, deleted, usn FROM notes WHERE uuid = ?", n1UUID), &n1Dirty, &n1Deleted, &n1USN) - testutils.MustScan(t, "scanning the newly added dirty flag of b1", db.QueryRow("SELECT dirty, deleted, usn FROM books WHERE uuid = ?", b1UUID), &b1Dirty, &b1Deleted, &b1USN) - - testutils.AssertEqual(t, n1Dirty, false, "n1 dirty flag should be false by default") - testutils.AssertEqual(t, b1Dirty, false, "b1 dirty flag should be false by default") - - testutils.AssertEqual(t, n1Deleted, false, "n1 deleted flag should be false by default") - testutils.AssertEqual(t, b1Deleted, false, "b1 deleted flag should be false by default") - - testutils.AssertEqual(t, n1USN, 0, "n1 usn flag should be 0 by default") - testutils.AssertEqual(t, b1USN, 0, "b1 usn flag should be 0 by default") -} - -func TestLocalMigration5(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-5-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") - b2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting js book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "js") - - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n1UUID, b1UUID, "n1 content", time.Now().UnixNano()) - n2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n2UUID, b1UUID, "n2 content", time.Now().UnixNano()) - n3UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n3UUID, b1UUID, "n3 content", time.Now().UnixNano()) - - data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) - testutils.MustExec(t, "inserting a1", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a1-uuid", 1, "add_book", string(data), 1537829463) - - data = testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: n1UUID, BookName: "css", Content: "n1 content", Public: false}) - testutils.MustExec(t, "inserting a2", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a2-uuid", 1, "add_note", string(data), 1537829463) - - updatedContent := "updated content" - data = testutils.MustMarshalJSON(t, actions.EditNoteDataV3{NoteUUID: n2UUID, BookName: (*string)(nil), Content: &updatedContent, Public: (*bool)(nil)}) - testutils.MustExec(t, "inserting a3", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a3-uuid", 1, "edit_note", string(data), 1537829463) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm5.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var b1Dirty, b2Dirty, n1Dirty, n2Dirty, n3Dirty bool - testutils.MustScan(t, "scanning the newly added dirty flag of b1", db.QueryRow("SELECT dirty FROM books WHERE uuid = ?", b1UUID), &b1Dirty) - testutils.MustScan(t, "scanning the newly added dirty flag of b2", db.QueryRow("SELECT dirty FROM books WHERE uuid = ?", b2UUID), &b2Dirty) - testutils.MustScan(t, "scanning the newly added dirty flag of n1", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n1UUID), &n1Dirty) - testutils.MustScan(t, "scanning the newly added dirty flag of n2", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n2UUID), &n2Dirty) - testutils.MustScan(t, "scanning the newly added dirty flag of n3", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n3UUID), &n3Dirty) - - testutils.AssertEqual(t, b1Dirty, false, "b1 dirty flag should be false by default") - testutils.AssertEqual(t, b2Dirty, true, "b2 dirty flag should be false by default") - testutils.AssertEqual(t, n1Dirty, true, "n1 dirty flag should be false by default") - testutils.AssertEqual(t, n2Dirty, true, "n2 dirty flag should be false by default") - testutils.AssertEqual(t, n3Dirty, false, "n3 dirty flag should be false by default") -} - -func TestLocalMigration6(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-5-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) - a1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting action", db, - "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 1, "add_book", string(data), 1537829463) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm5.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var count int - err = db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name = ?;", "actions").Scan(&count) - testutils.AssertEqual(t, count, 0, "actions table should have been deleted") -} - -func TestLocalMigration7_trash(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-7-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting trash book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "trash") - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm7.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var b1Label string - var b1Dirty bool - testutils.MustScan(t, "scanning b1 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) - testutils.AssertEqual(t, b1Label, "trash (2)", "b1 label was not migrated") - testutils.AssertEqual(t, b1Dirty, true, "b1 was not marked dirty") -} - -func TestLocalMigration7_conflicts(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-7-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts") - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm7.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var b1Label string - var b1Dirty bool - testutils.MustScan(t, "scanning b1 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) - testutils.AssertEqual(t, b1Label, "conflicts (2)", "b1 label was not migrated") - testutils.AssertEqual(t, b1Dirty, true, "b1 was not marked dirty") -} - -func TestLocalMigration7_conflicts_dup(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-7-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts") - b2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "conflicts (2)") - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm7.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var b1Label, b2Label string - var b1Dirty, b2Dirty bool - testutils.MustScan(t, "scanning b1 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) - testutils.MustScan(t, "scanning b2 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b2UUID), &b2Label, &b2Dirty) - testutils.AssertEqual(t, b1Label, "conflicts (3)", "b1 label was not migrated") - testutils.AssertEqual(t, b2Label, "conflicts (2)", "b1 label was not migrated") - testutils.AssertEqual(t, b1Dirty, true, "b1 was not marked dirty") - testutils.AssertEqual(t, b2Dirty, false, "b2 should not have been marked dirty") -} - -func TestLocalMigration8(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-8-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") - - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting n1", db, `INSERT INTO notes - (id, uuid, book_uuid, content, added_on, edited_on, public, dirty, usn, deleted) VALUES - (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 1, n1UUID, b1UUID, "n1 Body", 1, 2, true, true, 20, false) - n2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting n2", db, `INSERT INTO notes - (id, uuid, book_uuid, content, added_on, edited_on, public, dirty, usn, deleted) VALUES - (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 2, n2UUID, b1UUID, "", 3, 4, false, true, 21, true) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm8.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - var n1BookUUID, n1Body string - var n1AddedOn, n1EditedOn int64 - var n1USN int - var n1Public, n1Dirty, n1Deleted bool - testutils.MustScan(t, "scanning n1", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n1UUID), &n1BookUUID, &n1Body, &n1AddedOn, &n1EditedOn, &n1USN, &n1Public, &n1Dirty, &n1Deleted) - - var n2BookUUID, n2Body string - var n2AddedOn, n2EditedOn int64 - var n2USN int - var n2Public, n2Dirty, n2Deleted bool - testutils.MustScan(t, "scanning n2", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n2UUID), &n2BookUUID, &n2Body, &n2AddedOn, &n2EditedOn, &n2USN, &n2Public, &n2Dirty, &n2Deleted) - - testutils.AssertEqual(t, n1BookUUID, b1UUID, "n1 BookUUID mismatch") - testutils.AssertEqual(t, n1Body, "n1 Body", "n1 Body mismatch") - testutils.AssertEqual(t, n1AddedOn, int64(1), "n1 AddedOn mismatch") - testutils.AssertEqual(t, n1EditedOn, int64(2), "n1 EditedOn mismatch") - testutils.AssertEqual(t, n1USN, 20, "n1 USN mismatch") - testutils.AssertEqual(t, n1Public, true, "n1 Public mismatch") - testutils.AssertEqual(t, n1Dirty, true, "n1 Dirty mismatch") - testutils.AssertEqual(t, n1Deleted, false, "n1 Deleted mismatch") - - testutils.AssertEqual(t, n2BookUUID, b1UUID, "n2 BookUUID mismatch") - testutils.AssertEqual(t, n2Body, "", "n2 Body mismatch") - testutils.AssertEqual(t, n2AddedOn, int64(3), "n2 AddedOn mismatch") - testutils.AssertEqual(t, n2EditedOn, int64(4), "n2 EditedOn mismatch") - testutils.AssertEqual(t, n2USN, 21, "n2 USN mismatch") - testutils.AssertEqual(t, n2Public, false, "n2 Public mismatch") - testutils.AssertEqual(t, n2Dirty, true, "n2 Dirty mismatch") - testutils.AssertEqual(t, n2Deleted, true, "n2 Deleted mismatch") -} - -func TestLocalMigration9(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/local-9-pre-schema.sql", false) - defer testutils.TeardownEnv(ctx) - - db := ctx.DB - - b1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") - - n1UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting n1", db, `INSERT INTO notes - (uuid, book_uuid, body, added_on, edited_on, public, dirty, usn, deleted) VALUES - (?, ?, ?, ?, ?, ?, ?, ?, ?)`, n1UUID, b1UUID, "n1 Body", 1, 2, true, true, 20, false) - n2UUID := utils.GenerateUUID() - testutils.MustExec(t, "inserting n2", db, `INSERT INTO notes - (uuid, book_uuid, body, added_on, edited_on, public, dirty, usn, deleted) VALUES - (?, ?, ?, ?, ?, ?, ?, ?, ?)`, n2UUID, b1UUID, "n2 Body", 3, 4, false, true, 21, false) - - // Execute - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = lm9.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // Test - - // assert that note_fts was populated with correct values - var noteFtsCount int - testutils.MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts;"), ¬eFtsCount) - testutils.AssertEqual(t, noteFtsCount, 2, "noteFtsCount mismatch") - - var resCount int - testutils.MustScan(t, "counting result", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "n1"), &resCount) - testutils.AssertEqual(t, resCount, 1, "noteFtsCount mismatch") -} - -func TestRemoteMigration1(t *testing.T) { - // set up - ctx := testutils.InitEnv(t, "../tmp", "./fixtures/remote-1-pre-schema.sql", false) - testutils.Login(t, &ctx) - defer testutils.TeardownEnv(ctx) - - JSBookUUID := "existing-js-book-uuid" - CSSBookUUID := "existing-css-book-uuid" - linuxBookUUID := "existing-linux-book-uuid" - newJSBookUUID := "new-js-book-uuid" - newCSSBookUUID := "new-css-book-uuid" - - server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.String() == "/v1/books" { - res := []struct { - UUID string `json:"uuid"` - Label string `json:"label"` - }{ - { - UUID: newJSBookUUID, - Label: "js", - }, - { - UUID: newCSSBookUUID, - Label: "css", - }, - // book that only exists on the server. client must ignore. - { - UUID: "golang-book-uuid", - Label: "golang", - }, - } - - if err := json.NewEncoder(w).Encode(res); err != nil { - t.Fatal(errors.Wrap(err, "encoding response")) - } - } - })) - defer server.Close() - - ctx.APIEndpoint = server.URL - - db := ctx.DB - - testutils.MustExec(t, "inserting js book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", JSBookUUID, "js") - testutils.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", CSSBookUUID, "css") - testutils.MustExec(t, "inserting linux book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", linuxBookUUID, "linux") - testutils.MustExec(t, "inserting sessionKey", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemSessionKey, "someSessionKey") - testutils.MustExec(t, "inserting sessionKeyExpiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemSessionKeyExpiry, time.Now().Add(24*time.Hour).Unix()) - - tx, err := db.Begin() - if err != nil { - t.Fatal(errors.Wrap(err, "beginning a transaction")) - } - - err = rm1.run(ctx, tx) - if err != nil { - tx.Rollback() - t.Fatal(errors.Wrap(err, "failed to run")) - } - - tx.Commit() - - // test - var postJSBookUUID, postCSSBookUUID, postLinuxBookUUID string - testutils.MustScan(t, "getting js book uuid", db.QueryRow("SELECT uuid FROM books WHERE label = ?", "js"), &postJSBookUUID) - testutils.MustScan(t, "getting css book uuid", db.QueryRow("SELECT uuid FROM books WHERE label = ?", "css"), &postCSSBookUUID) - testutils.MustScan(t, "getting linux book uuid", db.QueryRow("SELECT uuid FROM books WHERE label = ?", "linux"), &postLinuxBookUUID) - - testutils.AssertEqual(t, postJSBookUUID, newJSBookUUID, "js book uuid was not updated correctly") - testutils.AssertEqual(t, postCSSBookUUID, newCSSBookUUID, "css book uuid was not updated correctly") - testutils.AssertEqual(t, postLinuxBookUUID, linuxBookUUID, "linux book uuid changed") -} diff --git a/cli/scripts/build.sh b/cli/scripts/build.sh deleted file mode 100755 index bb2423af..00000000 --- a/cli/scripts/build.sh +++ /dev/null @@ -1,106 +0,0 @@ -#!/bin/bash -# -# build.sh compiles dnote binary for target platforms -# it is resonsible for creating distributable files that can -# be released by a human or a script -# use: ./scripts/build.sh 0.4.8 - -set -eu - -version="$1" -projectDir="$GOPATH/src/github.com/dnote/dnote" -basedir="$GOPATH/src/github.com/dnote/dnote/cli" -TMP="$basedir/build" - -command_exists () { - command -v "$1" >/dev/null 2>&1; -} - -if ! command_exists shasum; then - echo "please install shasum" - exit 1 -fi -if [ $# -eq 0 ]; then - echo "no version specified." - exit 1 -fi -if [[ $1 == v* ]]; then - echo "do not prefix version with v" - exit 1 -fi - -build() { - # init build dir - rm -rf "$TMP" - mkdir "$TMP" - - # fetch tool - go get -u github.com/karalabe/xgo - - pushd "$basedir" - - # build linux - xgo --targets="linux/amd64"\ - --tags "linux fts5"\ - -ldflags "-X main.apiEndpoint=https://api.dnote.io -X main.versionTag=$version" . - mkdir "$TMP/linux" - mv cli-linux-amd64 "$TMP/linux/dnote" - - # build darwin - xgo --targets="darwin/amd64"\ - --tags "darwin fts5"\ - -ldflags "-X main.apiEndpoint=https://api.dnote.io -X main.versionTag=$version" . - mkdir "$TMP/darwin" - mv cli-darwin-10.6-amd64 "$TMP/darwin/dnote" - - # build windows - xgo --targets="windows/amd64"\ - --tags "fts5"\ - -ldflags "-X main.apiEndpoint=https://api.dnote.io -X main.versionTag=$version" . - mkdir "$TMP/windows" - mv cli-windows-4.0-amd64.exe "$TMP/windows/dnote.exe" - - popd -} - -get_buildname() { - os=$1 - - echo "dnote_${version}_${os}_amd64" -} - -calc_checksum() { - os=$1 - - pushd "$TMP/$os" - - buildname=$(get_buildname "$os") - mv dnote "$buildname" - shasum -a 256 "$buildname" >> "$TMP/dnote_${version}_checksums.txt" - mv "$buildname" dnote - - popd -} - -build_tarball() { - os=$1 - buildname=$(get_buildname "$os") - - pushd "$TMP/$os" - - cp "$projectDir/licenses/GPLv3.txt" . - cp "$basedir/README.md" . - tar -zcvf "../${buildname}.tar.gz" ./* - - popd -} - -build - -calc_checksum darwin -calc_checksum linux - -build_tarball windows -build_tarball darwin -build_tarball linux - diff --git a/cli/scripts/dump_schema.sh b/cli/scripts/dump_schema.sh deleted file mode 100755 index e3224df9..00000000 --- a/cli/scripts/dump_schema.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/bash -# dump_schema.sh dumps the current system's dnote schema to testutils package -# to be used while setting up tests - -sqlite3 ~/.dnote/dnote.db .schema > ./testutils/fixtures/schema.sql diff --git a/cli/scripts/release.sh b/cli/scripts/release.sh deleted file mode 100755 index 160a5fa2..00000000 --- a/cli/scripts/release.sh +++ /dev/null @@ -1,58 +0,0 @@ -#!/bin/bash -# -# release.sh releases the tarballs and checksum in the build directory -# to GitHub and brew. It is important to build those files using build.sh -# use: ./scripts/release.sh v0.4.8 - -set -eu - -homebrewRepoDir="$GOPATH"/src/github.com/dnote/homebrew-dnote - -command_exists () { - command -v "$1" >/dev/null 2>&1; -} - -if [ $# -eq 0 ]; then - echo "no version specified." - exit 1 -fi -if [[ $1 == v* ]]; then - echo "do not prefix version with v" - exit 1 -fi - -if ! command_exists hub; then - echo "please install hub" - exit 1 -fi - -if [ ! -d "$homebrewRepoDir" ]; then - echo "homebrew-dnote not found locally. did you clone it?" - exit 1 -fi - -# 1. push tag -version=$1 -version_tag="cli-v$version" - -echo "* tagging and pushing the tag" -git tag -a "$version_tag" -m "Release $version_tag" -git push --tags - -# 2. release on GitHub -files=(./build/*.tar.gz ./build/*.txt) -file_args=() -for file in "${files[@]}"; do - file_args+=("--attach=$file") -done - -echo "* creating release" -set -x -hub release create \ - "${file_args[@]}" \ - --message="$version_tag"\ - "$version_tag" - -# 3. Release on Homebrew -homebrew_sha256=$(shasum -a 256 "./build/dnote_${version}_darwin_amd64.tar.gz" | cut -d ' ' -f 1) -(cd "$homebrewRepoDir" && ./release.sh "$version" "$homebrew_sha256") diff --git a/cli/scripts/test.sh b/cli/scripts/test.sh deleted file mode 100755 index 1af25c1b..00000000 --- a/cli/scripts/test.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash -# run_server_test.sh runs server test files sequentially -# https://stackoverflow.com/questions/23715302/go-how-to-run-tests-for-multiple-packages - -set -eux - -basePath="$GOPATH/src/github.com/dnote/dnote/cli" - -# clear tmp dir in case not properly torn down -rm -rf "$basePath/tmp" - -# run test -pushd "$basePath" - -go test -a ./... \ - -p 1\ - --tags "fts5" - -popd diff --git a/cli/testutils/main.go b/cli/testutils/main.go deleted file mode 100644 index 9ce69022..00000000 --- a/cli/testutils/main.go +++ /dev/null @@ -1,363 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -// Package testutils provides utilities used in tests -package testutils - -import ( - "bytes" - "database/sql" - "encoding/json" - "fmt" - "io" - "io/ioutil" - "os" - "os/exec" - "path/filepath" - "reflect" - "strings" - "testing" - "time" - - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/utils" - "github.com/pkg/errors" -) - -// InitEnv sets up a test env and returns a new dnote context -func InitEnv(t *testing.T, dnotehomePath string, fixturePath string, migrated bool) infra.DnoteCtx { - os.Setenv("DNOTE_HOME_DIR", dnotehomePath) - ctx, err := infra.NewCtx("", "") - if err != nil { - t.Fatal(errors.Wrap(err, "getting new ctx")) - } - - // set up directory - if err := os.MkdirAll(ctx.DnoteDir, 0755); err != nil { - t.Fatal(err) - } - - // set up db - b := ReadFileAbs(fixturePath) - setupSQL := string(b) - - db := ctx.DB - if _, err := db.Exec(setupSQL); err != nil { - t.Fatal(errors.Wrap(err, "running schema sql")) - } - - if migrated { - // mark migrations as done. When adding new migrations, bump the numbers here. - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", infra.SystemSchema, 9); err != nil { - t.Fatal(errors.Wrap(err, "inserting schema")) - } - - if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", infra.SystemRemoteSchema, 1); err != nil { - t.Fatal(errors.Wrap(err, "inserting remote schema")) - } - } - - return ctx -} - -// Login simulates a logged in user by inserting credentials in the local database -func Login(t *testing.T, ctx *infra.DnoteCtx) { - db := ctx.DB - - MustExec(t, "inserting sessionKey", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemSessionKey, "someSessionKey") - MustExec(t, "inserting sessionKeyExpiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemSessionKeyExpiry, time.Now().Add(24*time.Hour).Unix()) - MustExec(t, "inserting cipherKey", db, "INSERT INTO system (key, value) VALUES (?, ?)", infra.SystemCipherKey, "QUVTMjU2S2V5LTMyQ2hhcmFjdGVyczEyMzQ1Njc4OTA=") - - ctx.SessionKey = "someSessionKey" - ctx.SessionKeyExpiry = time.Now().Add(24 * time.Hour).Unix() - ctx.CipherKey = []byte("AES256Key-32Characters1234567890") -} - -// TeardownEnv cleans up the test env represented by the given context -func TeardownEnv(ctx infra.DnoteCtx) { - ctx.DB.Close() - - if err := os.RemoveAll(ctx.DnoteDir); err != nil { - panic(err) - } -} - -// CopyFixture writes the content of the given fixture to the filename inside the dnote dir -func CopyFixture(ctx infra.DnoteCtx, fixturePath string, filename string) { - fp, err := filepath.Abs(fixturePath) - if err != nil { - panic(err) - } - dp, err := filepath.Abs(filepath.Join(ctx.DnoteDir, filename)) - if err != nil { - panic(err) - } - - err = utils.CopyFile(fp, dp) - if err != nil { - panic(err) - } -} - -// WriteFile writes a file with the given content and filename inside the dnote dir -func WriteFile(ctx infra.DnoteCtx, content []byte, filename string) { - dp, err := filepath.Abs(filepath.Join(ctx.DnoteDir, filename)) - if err != nil { - panic(err) - } - - if err := ioutil.WriteFile(dp, content, 0644); err != nil { - panic(err) - } -} - -// ReadFile reads the content of the file with the given name in dnote dir -func ReadFile(ctx infra.DnoteCtx, filename string) []byte { - path := filepath.Join(ctx.DnoteDir, filename) - - b, err := ioutil.ReadFile(path) - if err != nil { - panic(err) - } - - return b -} - -// ReadFileAbs reads the content of the file with the given file path by resolving -// it as an absolute path -func ReadFileAbs(relpath string) []byte { - fp, err := filepath.Abs(relpath) - if err != nil { - panic(err) - } - - b, err := ioutil.ReadFile(fp) - if err != nil { - panic(err) - } - - return b -} - -func checkEqual(a interface{}, b interface{}, message string) (bool, string) { - if a == b { - return true, "" - } - - var m string - if len(message) == 0 { - m = fmt.Sprintf("%v != %v", a, b) - } else { - m = message - } - errorMessage := fmt.Sprintf("%s. Actual: %+v. Expected: %+v.", m, a, b) - - return false, errorMessage -} - -// AssertEqual errors a test if the actual does not match the expected -func AssertEqual(t *testing.T, a interface{}, b interface{}, message string) { - ok, m := checkEqual(a, b, message) - if !ok { - t.Error(m) - } -} - -// AssertEqualf fails a test if the actual does not match the expected -func AssertEqualf(t *testing.T, a interface{}, b interface{}, message string) { - ok, m := checkEqual(a, b, message) - if !ok { - t.Fatal(m) - } -} - -// AssertNotEqual fails a test if the actual matches the expected -func AssertNotEqual(t *testing.T, a interface{}, b interface{}, message string) { - if a != b { - return - } - if len(message) == 0 { - message = fmt.Sprintf("%v == %v", a, b) - } - t.Errorf("%s. Actual: %+v. Expected: %+v.", message, a, b) -} - -// AssertDeepEqual fails a test if the actual does not deeply equal the expected -func AssertDeepEqual(t *testing.T, a interface{}, b interface{}, message string) { - if reflect.DeepEqual(a, b) { - return - } - - if len(message) == 0 { - message = fmt.Sprintf("%v != %v", a, b) - } - t.Errorf("%s.\nActual: %+v.\nExpected: %+v.", message, a, b) -} - -// ReadJSON reads JSON fixture to the struct at the destination address -func ReadJSON(path string, destination interface{}) { - var dat []byte - dat, err := ioutil.ReadFile(path) - if err != nil { - panic(errors.Wrap(err, "Failed to load fixture payload")) - } - if err := json.Unmarshal(dat, destination); err != nil { - panic(errors.Wrap(err, "Failed to get event")) - } -} - -// IsEqualJSON deeply compares two JSON byte slices -func IsEqualJSON(s1, s2 []byte) (bool, error) { - var o1 interface{} - var o2 interface{} - - if err := json.Unmarshal(s1, &o1); err != nil { - return false, errors.Wrap(err, "unmarshalling first JSON") - } - if err := json.Unmarshal(s2, &o2); err != nil { - return false, errors.Wrap(err, "unmarshalling second JSON") - } - - return reflect.DeepEqual(o1, o2), nil -} - -// MustExec executes the given SQL query and fails a test if an error occurs -func MustExec(t *testing.T, message string, db *infra.DB, query string, args ...interface{}) sql.Result { - result, err := db.Exec(query, args...) - if err != nil { - t.Fatal(errors.Wrap(errors.Wrap(err, "executing sql"), message)) - } - - return result -} - -// MustScan scans the given row and fails a test in case of any errors -func MustScan(t *testing.T, message string, row *sql.Row, args ...interface{}) { - err := row.Scan(args...) - if err != nil { - t.Fatal(errors.Wrap(errors.Wrap(err, "scanning a row"), message)) - } -} - -// NewDnoteCmd returns a new Dnote command and a pointer to stderr -func NewDnoteCmd(ctx infra.DnoteCtx, binaryName string, arg ...string) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) { - var stderr, stdout bytes.Buffer - - binaryPath, err := filepath.Abs(binaryName) - if err != nil { - return &exec.Cmd{}, &stderr, &stdout, errors.Wrap(err, "getting the absolute path to the test binary") - } - - cmd := exec.Command(binaryPath, arg...) - cmd.Env = []string{fmt.Sprintf("DNOTE_DIR=%s", ctx.DnoteDir), fmt.Sprintf("DNOTE_HOME_DIR=%s", ctx.HomeDir)} - cmd.Stderr = &stderr - cmd.Stdout = &stdout - - return cmd, &stderr, &stdout, nil -} - -// RunDnoteCmd runs a dnote command -func RunDnoteCmd(t *testing.T, ctx infra.DnoteCtx, binaryName string, arg ...string) { - t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) - - cmd, stderr, stdout, err := NewDnoteCmd(ctx, binaryName, arg...) - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "getting command").Error()) - } - - cmd.Env = append(cmd.Env, "DNOTE_DEBUG=1") - - if err := cmd.Run(); err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrapf(err, "running command %s", stderr.String())) - } - - // Print stdout if and only if test fails later - t.Logf("\n%s", stdout) -} - -// WaitDnoteCmd runs a dnote command and waits until the command is exited -func WaitDnoteCmd(t *testing.T, ctx infra.DnoteCtx, runFunc func(io.WriteCloser) error, binaryName string, arg ...string) { - t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) - - cmd, stderr, stdout, err := NewDnoteCmd(ctx, binaryName, arg...) - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "getting command").Error()) - } - - stdin, err := cmd.StdinPipe() - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "getting stdin %s")) - } - defer stdin.Close() - - // Start the program - err = cmd.Start() - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "starting command")) - } - - err = runFunc(stdin) - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrap(err, "running with stdin")) - } - - err = cmd.Wait() - if err != nil { - t.Logf("\n%s", stdout) - t.Fatal(errors.Wrapf(err, "running command %s", stderr.String())) - } - - // Print stdout if and only if test fails later - t.Logf("\n%s", stdout) -} - -// UserConfirm simulates confirmation from the user by writing to stdin -func UserConfirm(stdin io.WriteCloser) error { - // confirm - if _, err := io.WriteString(stdin, "y\n"); err != nil { - return errors.Wrap(err, "indicating confirmation in stdin") - } - - return nil -} - -// MustMarshalJSON marshalls the given interface into JSON. -// If there is any error, it fails the test. -func MustMarshalJSON(t *testing.T, v interface{}) []byte { - b, err := json.Marshal(v) - if err != nil { - t.Fatalf("%s: marshalling data", t.Name()) - } - - return b -} - -// MustUnmarshalJSON marshalls the given interface into JSON. -// If there is any error, it fails the test. -func MustUnmarshalJSON(t *testing.T, data []byte, v interface{}) { - err := json.Unmarshal(data, v) - if err != nil { - t.Fatalf("%s: unmarshalling data", t.Name()) - } -} diff --git a/cli/testutils/setup.go b/cli/testutils/setup.go deleted file mode 100644 index 73b21d8a..00000000 --- a/cli/testutils/setup.go +++ /dev/null @@ -1,79 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package testutils - -import ( - "github.com/dnote/dnote/cli/infra" - "testing" -) - -// Setup1 sets up a dnote env #1 -// dnote4.json -func Setup1(t *testing.T, ctx infra.DnoteCtx) { - db := ctx.DB - - b1UUID := "js-book-uuid" - b2UUID := "linux-book-uuid" - - MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "js") - MustExec(t, "setting up book 2", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "linux") - - MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943) -} - -// Setup2 sets up a dnote env #2 -// dnote3.json -func Setup2(t *testing.T, ctx infra.DnoteCtx) { - db := ctx.DB - - b1UUID := "js-book-uuid" - b2UUID := "linux-book-uuid" - - MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b1UUID, "js", 111) - MustExec(t, "setting up book 2", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b2UUID, "linux", 122) - - MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "n1 body", 1515199951, 11) - MustExec(t, "setting up note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "n2 body", 1515199943, 12) - MustExec(t, "setting up note 3", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "3e065d55-6d47-42f2-a6bf-f5844130b2d2", b2UUID, "n3 body", 1515199961, 13) -} - -// Setup3 sets up a dnote env #1 -// dnote1.json -func Setup3(t *testing.T, ctx infra.DnoteCtx) { - db := ctx.DB - - b1UUID := "js-book-uuid" - - MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "js") - - MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943) -} - -// Setup4 sets up a dnote env #1 -// dnote2.json -func Setup4(t *testing.T, ctx infra.DnoteCtx) { - db := ctx.DB - - b1UUID := "js-book-uuid" - - MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "js") - - MustExec(t, "setting up note 1", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?, ?)", 1, "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943) - MustExec(t, "setting up note 2", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?, ?)", 2, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "Date object implements mathematical comparisons", 1515199951) -} diff --git a/cli/utils/utils.go b/cli/utils/utils.go deleted file mode 100644 index f808075c..00000000 --- a/cli/utils/utils.go +++ /dev/null @@ -1,250 +0,0 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd - * - * This file is part of Dnote CLI. - * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . - */ - -package utils - -import ( - "bufio" - "fmt" - "io" - "io/ioutil" - "net/http" - "os" - "path/filepath" - "strings" - "syscall" - - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/pkg/errors" - "github.com/satori/go.uuid" - "golang.org/x/crypto/ssh/terminal" -) - -// GenerateUUID returns a uid -func GenerateUUID() string { - return uuid.NewV4().String() -} - -func getInput() (string, error) { - reader := bufio.NewReader(os.Stdin) - input, err := reader.ReadString('\n') - if err != nil { - return "", errors.Wrap(err, "reading stdin") - } - - return strings.Trim(input, "\r\n"), nil -} - -// PromptInput prompts the user input and saves the result to the destination -func PromptInput(message string, dest *string) error { - log.Askf(message, false) - - input, err := getInput() - if err != nil { - return errors.Wrap(err, "getting user input") - } - - *dest = input - - return nil -} - -// PromptPassword prompts the user input a password and saves the result to the destination. -// The input is masked, meaning it is not echoed on the terminal. -func PromptPassword(message string, dest *string) error { - log.Askf(message, true) - - password, err := terminal.ReadPassword(int(syscall.Stdin)) - if err != nil { - return errors.Wrap(err, "getting user input") - } - - fmt.Println("") - - *dest = string(password) - - return nil -} - -// AskConfirmation prompts for user input to confirm a choice -func AskConfirmation(question string, optimistic bool) (bool, error) { - var choices string - if optimistic { - choices = "(Y/n)" - } else { - choices = "(y/N)" - } - - message := fmt.Sprintf("%s %s", question, choices) - - var input string - if err := PromptInput(message, &input); err != nil { - return false, errors.Wrap(err, "Failed to get user input") - } - - confirmed := input == "y" - - if optimistic { - confirmed = confirmed || input == "" - } - - return confirmed, nil -} - -// FileExists checks if the file exists at the given path -func FileExists(filepath string) bool { - _, err := os.Stat(filepath) - return !os.IsNotExist(err) -} - -// CopyDir copies a directory from src to dest, recursively copying nested -// directories -func CopyDir(src, dest string) error { - srcPath := filepath.Clean(src) - destPath := filepath.Clean(dest) - - fi, err := os.Stat(srcPath) - if err != nil { - return errors.Wrap(err, "getting the file info for the input") - } - if !fi.IsDir() { - return errors.New("source is not a directory") - } - - _, err = os.Stat(dest) - if err != nil && !os.IsNotExist(err) { - return errors.Wrap(err, "looking up the destination") - } - - err = os.MkdirAll(dest, fi.Mode()) - if err != nil { - return errors.Wrap(err, "creating destination") - } - - entries, err := ioutil.ReadDir(src) - if err != nil { - return errors.Wrap(err, "reading the directory listing for the input") - } - - for _, entry := range entries { - srcEntryPath := filepath.Join(srcPath, entry.Name()) - destEntryPath := filepath.Join(destPath, entry.Name()) - - if entry.IsDir() { - if err = CopyDir(srcEntryPath, destEntryPath); err != nil { - return errors.Wrapf(err, "copying %s", entry.Name()) - } - } else { - if err = CopyFile(srcEntryPath, destEntryPath); err != nil { - return errors.Wrapf(err, "copying %s", entry.Name()) - } - } - } - - return nil -} - -func getReq(ctx infra.DnoteCtx, path, method, body string) (*http.Request, error) { - endpoint := fmt.Sprintf("%s%s", ctx.APIEndpoint, path) - req, err := http.NewRequest(method, endpoint, strings.NewReader(body)) - if err != nil { - return nil, errors.Wrap(err, "constructing http request") - } - - req.Header.Set("CLI-Version", ctx.Version) - - return req, nil -} - -// DoAuthorizedReq does a http request to the given path in the api endpoint as a user, -// with the appropriate headers. The given path should include the preceding slash. -func DoAuthorizedReq(ctx infra.DnoteCtx, hc http.Client, method, path, body string) (*http.Response, error) { - if ctx.SessionKey == "" { - return nil, errors.New("no session key found") - } - - req, err := getReq(ctx, path, method, body) - if err != nil { - return nil, errors.Wrap(err, "getting request") - } - - credential := fmt.Sprintf("Bearer %s", ctx.SessionKey) - req.Header.Set("Authorization", credential) - - res, err := hc.Do(req) - if err != nil { - return res, errors.Wrap(err, "making http request") - } - - return res, nil -} - -// DoReq does a http request to the given path in the api endpoint -func DoReq(ctx infra.DnoteCtx, method, path, body string) (*http.Response, error) { - req, err := getReq(ctx, path, method, body) - if err != nil { - return nil, errors.Wrap(err, "getting request") - } - - hc := http.Client{} - res, err := hc.Do(req) - if err != nil { - return res, errors.Wrap(err, "making http request") - } - - return res, nil -} - -// CopyFile copies a file from the src to dest -func CopyFile(src, dest string) error { - in, err := os.Open(src) - if err != nil { - return errors.Wrap(err, "opening the input file") - } - defer in.Close() - - out, err := os.Create(dest) - if err != nil { - return errors.Wrap(err, "creating the output file") - } - - if _, err = io.Copy(out, in); err != nil { - return errors.Wrap(err, "copying the file content") - } - - if err = out.Sync(); err != nil { - return errors.Wrap(err, "flushing the output file to disk") - } - - fi, err := os.Stat(src) - if err != nil { - return errors.Wrap(err, "getting the file info for the input file") - } - - if err = os.Chmod(dest, fi.Mode()); err != nil { - return errors.Wrap(err, "copying permission to the output file") - } - - // Close the output file - if err = out.Close(); err != nil { - return errors.Wrap(err, "closing the output file") - } - - return nil -} diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..48dfc85c --- /dev/null +++ b/go.mod @@ -0,0 +1,43 @@ +module github.com/dnote/dnote + +go 1.25 + +require ( + github.com/dnote/actions v0.2.0 + github.com/fatih/color v1.18.0 + github.com/google/go-cmp v0.7.0 + github.com/google/go-github v17.0.0+incompatible + github.com/google/uuid v1.6.0 + github.com/gorilla/csrf v1.7.3 + github.com/gorilla/mux v1.8.1 + github.com/gorilla/schema v1.4.1 + github.com/mattn/go-sqlite3 v1.14.32 + github.com/pkg/errors v0.9.1 + github.com/radovskyb/watcher v1.0.7 + github.com/sergi/go-diff v1.3.1 + github.com/spf13/cobra v1.10.1 + golang.org/x/crypto v0.45.0 + golang.org/x/time v0.13.0 + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df + gopkg.in/yaml.v2 v2.4.0 + gorm.io/driver/sqlite v1.6.0 + gorm.io/gorm v1.30.0 +) + +require ( + github.com/google/go-querystring v1.1.0 // indirect + github.com/gorilla/securecookie v1.1.2 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/spf13/pflag v1.0.10 // indirect + github.com/stretchr/testify v1.8.1 // indirect + golang.org/x/sys v0.38.0 // indirect + golang.org/x/term v0.37.0 // indirect + golang.org/x/text v0.31.0 // indirect + gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect + gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..8b3c653a --- /dev/null +++ b/go.sum @@ -0,0 +1,103 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dnote/actions v0.2.0 h1:P1ut2/QRKwfAzIIB374vN9A4IanU94C/payEocvngYo= +github.com/dnote/actions v0.2.0/go.mod h1:bBIassLhppVQdbC3iaE92SHBpM1HOVe+xZoAlj9ROxw= +github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= +github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY= +github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= +github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0= +github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/csrf v1.7.3 h1:BHWt6FTLZAb2HtWT5KDBf6qgpZzvtbp9QWDRKZMXJC0= +github.com/gorilla/csrf v1.7.3/go.mod h1:F1Fj3KG23WYHE6gozCmBAezKookxbIvUJT+121wTuLk= +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +github.com/gorilla/schema v1.4.1 h1:jUg5hUjCSDZpNGLuXQOgIWGdlgrIdYvgQ0wZtdK1M3E= +github.com/gorilla/schema v1.4.1/go.mod h1:Dg5SSm5PV60mhF2NFaTV1xuYYj8tV8NOPRo4FggUMnM= +github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA= +github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs= +github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/radovskyb/watcher v1.0.7 h1:AYePLih6dpmS32vlHfhCeli8127LzkIgwJGcwwe8tUE= +github.com/radovskyb/watcher v1.0.7/go.mod h1:78okwvY5wPdzcb1UYnip1pvrZNIVEIh/Cm+ZuvsUYIg= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= +github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s= +github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk= +github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.45.0 h1:jMBrvKuj23MTlT0bQEOBcAE0mjg8mK9RXFhRH6nyF3Q= +golang.org/x/crypto v0.45.0/go.mod h1:XTGrrkGJve7CYK7J8PEww4aY7gM3qMCElcJQ8n8JdX4= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU= +golang.org/x/term v0.37.0/go.mod h1:5pB4lxRNYYVZuTLmy8oR2BH8dflOR+IbTYFD8fi3254= +golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= +golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= +golang.org/x/time v0.13.0 h1:eUlYslOIt32DgYD6utsuUeHs4d7AsEYLuIAdg7FlYgI= +golang.org/x/time v0.13.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ= +gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8= +gorm.io/gorm v1.30.0 h1:qbT5aPv1UH8gI99OsRlvDToLxW5zR7FzS9acZDOZcgs= +gorm.io/gorm v1.30.0/go.mod h1:8Z33v652h4//uMA76KjeDH8mJXPm1QNCYrMeatR0DOE= diff --git a/host/docker/.gitignore b/host/docker/.gitignore new file mode 100644 index 00000000..d10df00d --- /dev/null +++ b/host/docker/.gitignore @@ -0,0 +1,2 @@ +*.tar.gz +.env diff --git a/host/docker/Dockerfile b/host/docker/Dockerfile new file mode 100644 index 00000000..71b3d1fa --- /dev/null +++ b/host/docker/Dockerfile @@ -0,0 +1,40 @@ +FROM busybox:glibc + +ARG TARGETPLATFORM +ARG version + +RUN test -n "$TARGETPLATFORM" || (echo "TARGETPLATFORM is required" && exit 1) +RUN test -n "$version" || (echo "version is required" && exit 1) + +WORKDIR /tmp/tarballs + +# Copy all architecture tarballs +COPY dnote_server_*.tar.gz ./ + +# Select and extract the correct tarball based on target platform +RUN case "$TARGETPLATFORM" in \ + "linux/amd64") ARCH="amd64" ;; \ + "linux/arm64") ARCH="arm64" ;; \ + "linux/arm/v7") ARCH="arm" ;; \ + "linux/386") ARCH="386" ;; \ + *) echo "Unsupported platform: $TARGETPLATFORM" && exit 1 ;; \ + esac && \ + TARBALL="dnote_server_${version}_linux_${ARCH}.tar.gz" && \ + echo "Extracting $TARBALL for $TARGETPLATFORM" && \ + mkdir -p /dnote && \ + tar -xvzf "$TARBALL" -C /dnote + +WORKDIR /dnote + +# Set default database path for all processes (main server, docker exec, shells) +ENV DBPath=/data/dnote.db + +COPY entrypoint.sh . +ENTRYPOINT ["./entrypoint.sh"] + +CMD ./dnote-server start + +EXPOSE 3001 + +HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \ + CMD wget --no-verbose --tries=1 -O /dev/null http://localhost:3001/health || exit 1 diff --git a/host/docker/build.sh b/host/docker/build.sh new file mode 100755 index 00000000..add55578 --- /dev/null +++ b/host/docker/build.sh @@ -0,0 +1,85 @@ +#!/usr/bin/env bash +# Build Docker image for local testing +# +# Usage: +# # builds for host platform (auto-detected) +# ./build.sh 1.0.0 +# +# # builds arm64 +# ./build.sh 1.0.0 linux/arm64 +# +# # builds multiple platforms +# ./build.sh 1.0.0 "linux/amd64,linux/arm64,linux/arm/v7,linux/386" +set -eux + +version=$1 + +# Detect host platform if not specified +if [ -z "${2:-}" ]; then + HOST_ARCH=$(uname -m) + case "$HOST_ARCH" in + x86_64) platform="linux/amd64" ;; + aarch64|arm64) platform="linux/arm64" ;; + armv7l) platform="linux/arm/v7" ;; + i386|i686) platform="linux/386" ;; + *) + echo "Warning: Unsupported architecture: $HOST_ARCH, defaulting to linux/amd64" + platform="linux/amd64" + ;; + esac + echo "Auto-detected platform: $platform" +else + platform=$2 +fi + +dir=$(dirname "${BASH_SOURCE[0]}") +projectDir="$dir/../.." + +# Copy all Linux tarballs to Docker build context +cp "$projectDir/build/server/dnote_server_${version}_linux_amd64.tar.gz" "$dir/" +cp "$projectDir/build/server/dnote_server_${version}_linux_arm64.tar.gz" "$dir/" +cp "$projectDir/build/server/dnote_server_${version}_linux_arm.tar.gz" "$dir/" +cp "$projectDir/build/server/dnote_server_${version}_linux_386.tar.gz" "$dir/" + +# Count platforms (check for comma) +if [[ "$platform" == *","* ]]; then + echo "Building for multiple platforms: $platform" + + # Check if multiarch builder exists, create if not + if ! docker buildx ls | grep -q "multiarch"; then + echo "Creating multiarch builder for multi-platform builds..." + docker buildx create --name multiarch --use + docker buildx inspect --bootstrap + else + echo "Using existing multiarch builder" + docker buildx use multiarch + fi + echo "" + + docker buildx build \ + --platform "$platform" \ + -t dnote/dnote:"$version" \ + -t dnote/dnote:latest \ + --build-arg version="$version" \ + "$dir" + + # Switch back to default builder + docker buildx use default +else + echo "Building for single platform: $platform" + echo "Image will be loaded to local Docker daemon" + + docker buildx build \ + --platform "$platform" \ + -t dnote/dnote:"$version" \ + -t dnote/dnote:latest \ + --build-arg version="$version" \ + --load \ + "$dir" +fi + +echo "" +echo "Build complete!" +if [[ "$platform" != *","* ]]; then + echo "Test with: docker run --rm dnote/dnote:$version ./dnote-server version" +fi diff --git a/host/docker/compose.yml b/host/docker/compose.yml new file mode 100644 index 00000000..2869033a --- /dev/null +++ b/host/docker/compose.yml @@ -0,0 +1,9 @@ +services: + dnote: + image: dnote/dnote:latest + container_name: dnote + ports: + - 3001:3001 + volumes: + - ./dnote_data:/data + restart: unless-stopped diff --git a/host/docker/entrypoint.sh b/host/docker/entrypoint.sh new file mode 100755 index 00000000..0fee185c --- /dev/null +++ b/host/docker/entrypoint.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +# Set default DBPath to /data if not specified +export DBPath=${DBPath:-/data/dnote.db} + +exec "$@" diff --git a/cli/install.sh b/install.sh similarity index 74% rename from cli/install.sh rename to install.sh index e690d380..bb09bd8f 100755 --- a/cli/install.sh +++ b/install.sh @@ -2,7 +2,7 @@ # # This script installs Dnote into your PATH (/usr/bin/local) # Use it like this: -# $ curl https://raw.githubusercontent.com/dnote/dnote/master/cli/install.sh | sh +# $ curl https://raw.githubusercontent.com/dnote/dnote/master/install.sh | sh # set -eu @@ -68,8 +68,14 @@ uname_os() { uname_arch() { arch=$(uname -m) - case $arch in + case $arch in x86_64) arch="amd64" ;; + aarch64) arch="arm64" ;; + arm64) arch="arm64" ;; + armv7l) arch="arm" ;; + armv6l) arch="arm" ;; + armv5l) arch="arm" ;; + arm) arch="arm" ;; x86) arch="386" ;; i686) arch="386" ;; i386) arch="386" ;; @@ -85,8 +91,17 @@ check_platform() { found=1 case "$platform" in - darwin/amd64) found=0;; + # Linux linux/amd64) found=0 ;; + linux/arm64) found=0 ;; + linux/arm) found=0 ;; + # macOS + darwin/amd64) found=0 ;; + darwin/arm64) found=0 ;; + # Windows + windows/amd64) found=0 ;; + # FreeBSD + freebsd/amd64) found=0 ;; esac return $found @@ -113,23 +128,26 @@ hash_sha256() { } verify_checksum() { - binary_path=$1 - filename=$2 - checksums=$3 + filepath=$1 + checksums=$2 + + filename=$(basename "$filepath") want=$(grep "${filename}" "${checksums}" 2>/dev/null | cut -d ' ' -f 1) if [ -z "$want" ]; then print_error "unable to find checksum for '${filename}' in '${checksums}'" exit 1 fi - got=$(hash_sha256 "$binary_path") + got=$(hash_sha256 "$filepath") if [ "$want" != "$got" ]; then - print_error "checksum for '$binary_path' did not verify ${want} vs $got" + print_error "checksum for '$filepath' did not verify ${want} vs $got" exit 1 fi } install_dnote() { + sudo_cmd="" + os=$(uname_os) arch=$(uname_arch) @@ -146,10 +164,22 @@ install_dnote() { tmpdir="$(mktemp -d)" bindir=${bindir:-/usr/local/bin} + if hash sudo 2>/dev/null; then + sudo_cmd="sudo" + echo "You need a root privilege to install Dnote binary to $bindir" + + if ! is_command "$sudo_cmd"; then + print_error "command not found: sudo. You need a root privilege to continue the installation." + exit 1; + fi + fi + + # create destination directory if not exists + $sudo_cmd mkdir -p "$bindir" # get the latest version - resp=$(http_get "https://api.github.com/repos/$owner/$repo/tags") - version=$(echo "$resp" | tr ',' '\n' | grep -m 1 "\"name\": \"cli" | cut -f4 -d'"') + resp=$(http_get "https://api.github.com/repos/$owner/$repo/releases") + version=$(echo "$resp" | tr ',' '\n' | grep -m 1 "\"tag_name\": \"cli" | cut -f4 -d'"') if [ -z "$version" ]; then print_error "Error fetching latest version. Please try again." @@ -173,20 +203,19 @@ install_dnote() { print_step "Downloading the checksum file for v$version" http_download "$tmpdir/$checksum" "$checksum_url" + print_step "Comparing checksums for binaries." + verify_checksum "$tmpdir/$tarball" "$tmpdir/$checksum" + # unzip tar print_step "Inflating the binary." (cd "${tmpdir}" && tar -xzf "${tarball}") - print_step "Comparing checksums for binaries." - verify_checksum "${tmpdir}/${binary}" "$filename" "$tmpdir/$checksum" - - install -d "${bindir}" - install "${tmpdir}/${binary}" "${bindir}/" + $sudo_cmd install -d "${bindir}" + $sudo_cmd install "${tmpdir}/${binary}" "${bindir}/" print_success "dnote v${version} was successfully installed in $bindir." } - exit_error() { # shellcheck disable=SC2181 if [ "$?" -ne 0 ]; then diff --git a/licenses/AGPLv3.txt b/licenses/AGPLv3.txt deleted file mode 100644 index 0ad25db4..00000000 --- a/licenses/AGPLv3.txt +++ /dev/null @@ -1,661 +0,0 @@ - GNU AFFERO GENERAL PUBLIC LICENSE - Version 3, 19 November 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU Affero General Public License is a free, copyleft license for -software and other kinds of works, specifically designed to ensure -cooperation with the community in the case of network server software. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -our General Public Licenses are intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - Developers that use our General Public Licenses protect your rights -with two steps: (1) assert copyright on the software, and (2) offer -you this License which gives you legal permission to copy, distribute -and/or modify the software. - - A secondary benefit of defending all users' freedom is that -improvements made in alternate versions of the program, if they -receive widespread use, become available for other developers to -incorporate. Many developers of free software are heartened and -encouraged by the resulting cooperation. However, in the case of -software used on network servers, this result may fail to come about. -The GNU General Public License permits making a modified version and -letting the public access it on a server without ever releasing its -source code to the public. - - The GNU Affero General Public License is designed specifically to -ensure that, in such cases, the modified source code becomes available -to the community. It requires the operator of a network server to -provide the source code of the modified version running there to the -users of that server. Therefore, public use of a modified version, on -a publicly accessible server, gives the public access to the source -code of the modified version. - - An older license, called the Affero General Public License and -published by Affero, was designed to accomplish similar goals. This is -a different license, not a version of the Affero GPL, but Affero has -released a new version of the Affero GPL which permits relicensing under -this license. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU Affero General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Remote Network Interaction; Use with the GNU General Public License. - - Notwithstanding any other provision of this License, if you modify the -Program, your modified version must prominently offer all users -interacting with it remotely through a computer network (if your version -supports such interaction) an opportunity to receive the Corresponding -Source of your version by providing access to the Corresponding Source -from a network server at no charge, through some standard or customary -means of facilitating copying of software. This Corresponding Source -shall include the Corresponding Source for any work covered by version 3 -of the GNU General Public License that is incorporated pursuant to the -following paragraph. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the work with which it is combined will remain governed by version -3 of the GNU General Public License. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU Affero General Public License from time to time. Such new versions -will be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU Affero General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU Affero General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU Affero General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If your software can interact with users remotely through a computer -network, you should also make sure that it provides a way for users to -get its source. For example, if your program is a web application, its -interface could display a "Source" link that leads users to an archive -of the code. There are many ways you could offer source, and different -solutions will be better for different programs; see section 13 for the -specific requirements. - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU AGPL, see -. diff --git a/licenses/GPLv3.txt b/licenses/GPLv3.txt deleted file mode 100644 index f288702d..00000000 --- a/licenses/GPLv3.txt +++ /dev/null @@ -1,674 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 - - Copyright (C) 2007 Free Software Foundation, Inc. - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The GNU General Public License is a free, copyleft license for -software and other kinds of works. - - The licenses for most software and other practical works are designed -to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to -share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -them if you wish), that you receive source code or can get it if you -want it, that you can change the software or use pieces of it in new -free programs, and that you know you can do these things. - - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. - - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. - - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. - - The precise terms and conditions for copying, distribution and -modification follow. - - TERMS AND CONDITIONS - - 0. Definitions. - - "This License" refers to version 3 of the GNU General Public License. - - "Copyright" also means copyright-like laws that apply to other kinds of -works, such as semiconductor masks. - - "The Program" refers to any copyrightable work licensed under this -License. Each licensee is addressed as "you". "Licensees" and -"recipients" may be individuals or organizations. - - To "modify" a work means to copy from or adapt all or part of the work -in a fashion requiring copyright permission, other than the making of an -exact copy. The resulting work is called a "modified version" of the -earlier work or a work "based on" the earlier work. - - A "covered work" means either the unmodified Program or a work based -on the Program. - - To "propagate" a work means to do anything with it that, without -permission, would make you directly or secondarily liable for -infringement under applicable copyright law, except executing it on a -computer or modifying a private copy. Propagation includes copying, -distribution (with or without modification), making available to the -public, and in some countries other activities as well. - - To "convey" a work means any kind of propagation that enables other -parties to make or receive copies. Mere interaction with a user through -a computer network, with no transfer of a copy, is not conveying. - - An interactive user interface displays "Appropriate Legal Notices" -to the extent that it includes a convenient and prominently visible -feature that (1) displays an appropriate copyright notice, and (2) -tells the user that there is no warranty for the work (except to the -extent that warranties are provided), that licensees may convey the -work under this License, and how to view a copy of this License. If -the interface presents a list of user commands or options, such as a -menu, a prominent item in the list meets this criterion. - - 1. Source Code. - - The "source code" for a work means the preferred form of the work -for making modifications to it. "Object code" means any non-source -form of a work. - - A "Standard Interface" means an interface that either is an official -standard defined by a recognized standards body, or, in the case of -interfaces specified for a particular programming language, one that -is widely used among developers working in that language. - - The "System Libraries" of an executable work include anything, other -than the work as a whole, that (a) is included in the normal form of -packaging a Major Component, but which is not part of that Major -Component, and (b) serves only to enable use of the work with that -Major Component, or to implement a Standard Interface for which an -implementation is available to the public in source code form. A -"Major Component", in this context, means a major essential component -(kernel, window system, and so on) of the specific operating system -(if any) on which the executable work runs, or a compiler used to -produce the work, or an object code interpreter used to run it. - - The "Corresponding Source" for a work in object code form means all -the source code needed to generate, install, and (for an executable -work) run the object code and to modify the work, including scripts to -control those activities. However, it does not include the work's -System Libraries, or general-purpose tools or generally available free -programs which are used unmodified in performing those activities but -which are not part of the work. For example, Corresponding Source -includes interface definition files associated with source files for -the work, and the source code for shared libraries and dynamically -linked subprograms that the work is specifically designed to require, -such as by intimate data communication or control flow between those -subprograms and other parts of the work. - - The Corresponding Source need not include anything that users -can regenerate automatically from other parts of the Corresponding -Source. - - The Corresponding Source for a work in source code form is that -same work. - - 2. Basic Permissions. - - All rights granted under this License are granted for the term of -copyright on the Program, and are irrevocable provided the stated -conditions are met. This License explicitly affirms your unlimited -permission to run the unmodified Program. The output from running a -covered work is covered by this License only if the output, given its -content, constitutes a covered work. This License acknowledges your -rights of fair use or other equivalent, as provided by copyright law. - - You may make, run and propagate covered works that you do not -convey, without conditions so long as your license otherwise remains -in force. You may convey covered works to others for the sole purpose -of having them make modifications exclusively for you, or provide you -with facilities for running those works, provided that you comply with -the terms of this License in conveying all material for which you do -not control copyright. Those thus making or running the covered works -for you must do so exclusively on your behalf, under your direction -and control, on terms that prohibit them from making any copies of -your copyrighted material outside their relationship with you. - - Conveying under any other circumstances is permitted solely under -the conditions stated below. Sublicensing is not allowed; section 10 -makes it unnecessary. - - 3. Protecting Users' Legal Rights From Anti-Circumvention Law. - - No covered work shall be deemed part of an effective technological -measure under any applicable law fulfilling obligations under article -11 of the WIPO copyright treaty adopted on 20 December 1996, or -similar laws prohibiting or restricting circumvention of such -measures. - - When you convey a covered work, you waive any legal power to forbid -circumvention of technological measures to the extent such circumvention -is effected by exercising rights under this License with respect to -the covered work, and you disclaim any intention to limit operation or -modification of the work as a means of enforcing, against the work's -users, your or third parties' legal rights to forbid circumvention of -technological measures. - - 4. Conveying Verbatim Copies. - - You may convey verbatim copies of the Program's source code as you -receive it, in any medium, provided that you conspicuously and -appropriately publish on each copy an appropriate copyright notice; -keep intact all notices stating that this License and any -non-permissive terms added in accord with section 7 apply to the code; -keep intact all notices of the absence of any warranty; and give all -recipients a copy of this License along with the Program. - - You may charge any price or no price for each copy that you convey, -and you may offer support or warranty protection for a fee. - - 5. Conveying Modified Source Versions. - - You may convey a work based on the Program, or the modifications to -produce it from the Program, in the form of source code under the -terms of section 4, provided that you also meet all of these conditions: - - a) The work must carry prominent notices stating that you modified - it, and giving a relevant date. - - b) The work must carry prominent notices stating that it is - released under this License and any conditions added under section - 7. This requirement modifies the requirement in section 4 to - "keep intact all notices". - - c) You must license the entire work, as a whole, under this - License to anyone who comes into possession of a copy. This - License will therefore apply, along with any applicable section 7 - additional terms, to the whole of the work, and all its parts, - regardless of how they are packaged. This License gives no - permission to license the work in any other way, but it does not - invalidate such permission if you have separately received it. - - d) If the work has interactive user interfaces, each must display - Appropriate Legal Notices; however, if the Program has interactive - interfaces that do not display Appropriate Legal Notices, your - work need not make them do so. - - A compilation of a covered work with other separate and independent -works, which are not by their nature extensions of the covered work, -and which are not combined with it such as to form a larger program, -in or on a volume of a storage or distribution medium, is called an -"aggregate" if the compilation and its resulting copyright are not -used to limit the access or legal rights of the compilation's users -beyond what the individual works permit. Inclusion of a covered work -in an aggregate does not cause this License to apply to the other -parts of the aggregate. - - 6. Conveying Non-Source Forms. - - You may convey a covered work in object code form under the terms -of sections 4 and 5, provided that you also convey the -machine-readable Corresponding Source under the terms of this License, -in one of these ways: - - a) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by the - Corresponding Source fixed on a durable physical medium - customarily used for software interchange. - - b) Convey the object code in, or embodied in, a physical product - (including a physical distribution medium), accompanied by a - written offer, valid for at least three years and valid for as - long as you offer spare parts or customer support for that product - model, to give anyone who possesses the object code either (1) a - copy of the Corresponding Source for all the software in the - product that is covered by this License, on a durable physical - medium customarily used for software interchange, for a price no - more than your reasonable cost of physically performing this - conveying of source, or (2) access to copy the - Corresponding Source from a network server at no charge. - - c) Convey individual copies of the object code with a copy of the - written offer to provide the Corresponding Source. This - alternative is allowed only occasionally and noncommercially, and - only if you received the object code with such an offer, in accord - with subsection 6b. - - d) Convey the object code by offering access from a designated - place (gratis or for a charge), and offer equivalent access to the - Corresponding Source in the same way through the same place at no - further charge. You need not require recipients to copy the - Corresponding Source along with the object code. If the place to - copy the object code is a network server, the Corresponding Source - may be on a different server (operated by you or a third party) - that supports equivalent copying facilities, provided you maintain - clear directions next to the object code saying where to find the - Corresponding Source. Regardless of what server hosts the - Corresponding Source, you remain obligated to ensure that it is - available for as long as needed to satisfy these requirements. - - e) Convey the object code using peer-to-peer transmission, provided - you inform other peers where the object code and Corresponding - Source of the work are being offered to the general public at no - charge under subsection 6d. - - A separable portion of the object code, whose source code is excluded -from the Corresponding Source as a System Library, need not be -included in conveying the object code work. - - A "User Product" is either (1) a "consumer product", which means any -tangible personal property which is normally used for personal, family, -or household purposes, or (2) anything designed or sold for incorporation -into a dwelling. In determining whether a product is a consumer product, -doubtful cases shall be resolved in favor of coverage. For a particular -product received by a particular user, "normally used" refers to a -typical or common use of that class of product, regardless of the status -of the particular user or of the way in which the particular user -actually uses, or expects or is expected to use, the product. A product -is a consumer product regardless of whether the product has substantial -commercial, industrial or non-consumer uses, unless such uses represent -the only significant mode of use of the product. - - "Installation Information" for a User Product means any methods, -procedures, authorization keys, or other information required to install -and execute modified versions of a covered work in that User Product from -a modified version of its Corresponding Source. The information must -suffice to ensure that the continued functioning of the modified object -code is in no case prevented or interfered with solely because -modification has been made. - - If you convey an object code work under this section in, or with, or -specifically for use in, a User Product, and the conveying occurs as -part of a transaction in which the right of possession and use of the -User Product is transferred to the recipient in perpetuity or for a -fixed term (regardless of how the transaction is characterized), the -Corresponding Source conveyed under this section must be accompanied -by the Installation Information. But this requirement does not apply -if neither you nor any third party retains the ability to install -modified object code on the User Product (for example, the work has -been installed in ROM). - - The requirement to provide Installation Information does not include a -requirement to continue to provide support service, warranty, or updates -for a work that has been modified or installed by the recipient, or for -the User Product in which it has been modified or installed. Access to a -network may be denied when the modification itself materially and -adversely affects the operation of the network or violates the rules and -protocols for communication across the network. - - Corresponding Source conveyed, and Installation Information provided, -in accord with this section must be in a format that is publicly -documented (and with an implementation available to the public in -source code form), and must require no special password or key for -unpacking, reading or copying. - - 7. Additional Terms. - - "Additional permissions" are terms that supplement the terms of this -License by making exceptions from one or more of its conditions. -Additional permissions that are applicable to the entire Program shall -be treated as though they were included in this License, to the extent -that they are valid under applicable law. If additional permissions -apply only to part of the Program, that part may be used separately -under those permissions, but the entire Program remains governed by -this License without regard to the additional permissions. - - When you convey a copy of a covered work, you may at your option -remove any additional permissions from that copy, or from any part of -it. (Additional permissions may be written to require their own -removal in certain cases when you modify the work.) You may place -additional permissions on material, added by you to a covered work, -for which you have or can give appropriate copyright permission. - - Notwithstanding any other provision of this License, for material you -add to a covered work, you may (if authorized by the copyright holders of -that material) supplement the terms of this License with terms: - - a) Disclaiming warranty or limiting liability differently from the - terms of sections 15 and 16 of this License; or - - b) Requiring preservation of specified reasonable legal notices or - author attributions in that material or in the Appropriate Legal - Notices displayed by works containing it; or - - c) Prohibiting misrepresentation of the origin of that material, or - requiring that modified versions of such material be marked in - reasonable ways as different from the original version; or - - d) Limiting the use for publicity purposes of names of licensors or - authors of the material; or - - e) Declining to grant rights under trademark law for use of some - trade names, trademarks, or service marks; or - - f) Requiring indemnification of licensors and authors of that - material by anyone who conveys the material (or modified versions of - it) with contractual assumptions of liability to the recipient, for - any liability that these contractual assumptions directly impose on - those licensors and authors. - - All other non-permissive additional terms are considered "further -restrictions" within the meaning of section 10. If the Program as you -received it, or any part of it, contains a notice stating that it is -governed by this License along with a term that is a further -restriction, you may remove that term. If a license document contains -a further restriction but permits relicensing or conveying under this -License, you may add to a covered work material governed by the terms -of that license document, provided that the further restriction does -not survive such relicensing or conveying. - - If you add terms to a covered work in accord with this section, you -must place, in the relevant source files, a statement of the -additional terms that apply to those files, or a notice indicating -where to find the applicable terms. - - Additional terms, permissive or non-permissive, may be stated in the -form of a separately written license, or stated as exceptions; -the above requirements apply either way. - - 8. Termination. - - You may not propagate or modify a covered work except as expressly -provided under this License. Any attempt otherwise to propagate or -modify it is void, and will automatically terminate your rights under -this License (including any patent licenses granted under the third -paragraph of section 11). - - However, if you cease all violation of this License, then your -license from a particular copyright holder is reinstated (a) -provisionally, unless and until the copyright holder explicitly and -finally terminates your license, and (b) permanently, if the copyright -holder fails to notify you of the violation by some reasonable means -prior to 60 days after the cessation. - - Moreover, your license from a particular copyright holder is -reinstated permanently if the copyright holder notifies you of the -violation by some reasonable means, this is the first time you have -received notice of violation of this License (for any work) from that -copyright holder, and you cure the violation prior to 30 days after -your receipt of the notice. - - Termination of your rights under this section does not terminate the -licenses of parties who have received copies or rights from you under -this License. If your rights have been terminated and not permanently -reinstated, you do not qualify to receive new licenses for the same -material under section 10. - - 9. Acceptance Not Required for Having Copies. - - You are not required to accept this License in order to receive or -run a copy of the Program. Ancillary propagation of a covered work -occurring solely as a consequence of using peer-to-peer transmission -to receive a copy likewise does not require acceptance. However, -nothing other than this License grants you permission to propagate or -modify any covered work. These actions infringe copyright if you do -not accept this License. Therefore, by modifying or propagating a -covered work, you indicate your acceptance of this License to do so. - - 10. Automatic Licensing of Downstream Recipients. - - Each time you convey a covered work, the recipient automatically -receives a license from the original licensors, to run, modify and -propagate that work, subject to this License. You are not responsible -for enforcing compliance by third parties with this License. - - An "entity transaction" is a transaction transferring control of an -organization, or substantially all assets of one, or subdividing an -organization, or merging organizations. If propagation of a covered -work results from an entity transaction, each party to that -transaction who receives a copy of the work also receives whatever -licenses to the work the party's predecessor in interest had or could -give under the previous paragraph, plus a right to possession of the -Corresponding Source of the work from the predecessor in interest, if -the predecessor has it or can get it with reasonable efforts. - - You may not impose any further restrictions on the exercise of the -rights granted or affirmed under this License. For example, you may -not impose a license fee, royalty, or other charge for exercise of -rights granted under this License, and you may not initiate litigation -(including a cross-claim or counterclaim in a lawsuit) alleging that -any patent claim is infringed by making, using, selling, offering for -sale, or importing the Program or any portion of it. - - 11. Patents. - - A "contributor" is a copyright holder who authorizes use under this -License of the Program or a work on which the Program is based. The -work thus licensed is called the contributor's "contributor version". - - A contributor's "essential patent claims" are all patent claims -owned or controlled by the contributor, whether already acquired or -hereafter acquired, that would be infringed by some manner, permitted -by this License, of making, using, or selling its contributor version, -but do not include claims that would be infringed only as a -consequence of further modification of the contributor version. For -purposes of this definition, "control" includes the right to grant -patent sublicenses in a manner consistent with the requirements of -this License. - - Each contributor grants you a non-exclusive, worldwide, royalty-free -patent license under the contributor's essential patent claims, to -make, use, sell, offer for sale, import and otherwise run, modify and -propagate the contents of its contributor version. - - In the following three paragraphs, a "patent license" is any express -agreement or commitment, however denominated, not to enforce a patent -(such as an express permission to practice a patent or covenant not to -sue for patent infringement). To "grant" such a patent license to a -party means to make such an agreement or commitment not to enforce a -patent against the party. - - If you convey a covered work, knowingly relying on a patent license, -and the Corresponding Source of the work is not available for anyone -to copy, free of charge and under the terms of this License, through a -publicly available network server or other readily accessible means, -then you must either (1) cause the Corresponding Source to be so -available, or (2) arrange to deprive yourself of the benefit of the -patent license for this particular work, or (3) arrange, in a manner -consistent with the requirements of this License, to extend the patent -license to downstream recipients. "Knowingly relying" means you have -actual knowledge that, but for the patent license, your conveying the -covered work in a country, or your recipient's use of the covered work -in a country, would infringe one or more identifiable patents in that -country that you have reason to believe are valid. - - If, pursuant to or in connection with a single transaction or -arrangement, you convey, or propagate by procuring conveyance of, a -covered work, and grant a patent license to some of the parties -receiving the covered work authorizing them to use, propagate, modify -or convey a specific copy of the covered work, then the patent license -you grant is automatically extended to all recipients of the covered -work and works based on it. - - A patent license is "discriminatory" if it does not include within -the scope of its coverage, prohibits the exercise of, or is -conditioned on the non-exercise of one or more of the rights that are -specifically granted under this License. You may not convey a covered -work if you are a party to an arrangement with a third party that is -in the business of distributing software, under which you make payment -to the third party based on the extent of your activity of conveying -the work, and under which the third party grants, to any of the -parties who would receive the covered work from you, a discriminatory -patent license (a) in connection with copies of the covered work -conveyed by you (or copies made from those copies), or (b) primarily -for and in connection with specific products or compilations that -contain the covered work, unless you entered into that arrangement, -or that patent license was granted, prior to 28 March 2007. - - Nothing in this License shall be construed as excluding or limiting -any implied license or other defenses to infringement that may -otherwise be available to you under applicable patent law. - - 12. No Surrender of Others' Freedom. - - If conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot convey a -covered work so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you may -not convey it at all. For example, if you agree to terms that obligate you -to collect a royalty for further conveying from those to whom you convey -the Program, the only way you could satisfy both those terms and this -License would be to refrain entirely from conveying the Program. - - 13. Use with the GNU Affero General Public License. - - Notwithstanding any other provision of this License, you have -permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single -combined work, and to convey the resulting work. The terms of this -License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. - - 14. Revised Versions of this License. - - The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - - Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General -Public License "or any later version" applies to it, you have the -option of following the terms and conditions either of that numbered -version or of any later version published by the Free Software -Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published -by the Free Software Foundation. - - If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's -public statement of acceptance of a version permanently authorizes you -to choose that version for the Program. - - Later license versions may give you additional or different -permissions. However, no additional obligations are imposed on any -author or copyright holder as a result of your choosing to follow a -later version. - - 15. Disclaimer of Warranty. - - THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY -APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT -HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY -OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM -IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF -ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. Limitation of Liability. - - IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS -THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY -GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE -USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF -DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD -PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), -EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF -SUCH DAMAGES. - - 17. Interpretation of Sections 15 and 16. - - If the disclaimer of warranty and limitation of liability provided -above cannot be given local legal effect according to their terms, -reviewing courts shall apply local law that most closely approximates -an absolute waiver of all civil liability in connection with the -Program, unless a warranty or assumption of liability accompanies a -copy of the Program in return for a fee. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -state the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - -Also add information on how to contact you by electronic and paper mail. - - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". - - You should also get your employer (if you work as a programmer) or school, -if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. diff --git a/pkg/assert/assert.go b/pkg/assert/assert.go new file mode 100644 index 00000000..bb98fce0 --- /dev/null +++ b/pkg/assert/assert.go @@ -0,0 +1,145 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package assert provides functions to assert a condition in tests +package assert + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "reflect" + "runtime/debug" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/pkg/errors" +) + +func getErrorMessage(m string, a, b interface{}) string { + return fmt.Sprintf(`%s. +Actual: +======================== +%+v +======================== + +Expected: +======================== +%+v +======================== + +%s`, m, a, b, string(debug.Stack())) +} + +func checkEqual(a, b interface{}, message string) (bool, string) { + if a == b { + return true, "" + } + + var m string + if len(message) == 0 { + m = fmt.Sprintf("%v != %v", a, b) + } else { + m = message + } + errorMessage := getErrorMessage(m, a, b) + + return false, errorMessage +} + +// Equal errors a test if the actual does not match the expected +func Equal(t *testing.T, a, b interface{}, message string) { + ok, m := checkEqual(a, b, message) + if !ok { + t.Error(m) + } +} + +// Equalf fails a test if the actual does not match the expected +func Equalf(t *testing.T, a, b interface{}, message string) { + ok, m := checkEqual(a, b, message) + if !ok { + t.Fatal(m) + } +} + +// NotEqual fails a test if the actual matches the expected +func NotEqual(t *testing.T, a, b interface{}, message string) { + ok, m := checkEqual(a, b, message) + if ok { + t.Error(m) + } +} + +// NotEqualf fails a test if the actual matches the expected +func NotEqualf(t *testing.T, a, b interface{}, message string) { + ok, m := checkEqual(a, b, message) + if ok { + t.Fatal(m) + } +} + +// DeepEqual fails a test if the actual does not deeply equal the expected +func DeepEqual(t *testing.T, a, b interface{}, message string) { + if cmp.Equal(a, b) { + return + } + + if len(message) == 0 { + message = fmt.Sprintf("%v != %v", a, b) + } + + errorMessage := getErrorMessage(message, a, b) + errorMessage = fmt.Sprintf("%v\n%v", errorMessage, cmp.Diff(a, b)) + t.Error(errorMessage) +} + +// EqualJSON asserts that two JSON strings are equal +func EqualJSON(t *testing.T, a, b, message string) { + var o1 interface{} + var o2 interface{} + + err := json.Unmarshal([]byte(a), &o1) + if err != nil { + panic(fmt.Errorf("Error mashalling string 1 :: %s", err.Error())) + } + err = json.Unmarshal([]byte(b), &o2) + if err != nil { + panic(fmt.Errorf("Error mashalling string 2 :: %s", err.Error())) + } + + if reflect.DeepEqual(o1, o2) { + return + } + + if len(message) == 0 { + message = fmt.Sprintf("%v != %v", a, b) + } + t.Errorf("%s.\nActual: %+v.\nExpected: %+v.", message, a, b) +} + +// StatusCodeEquals asserts that the reponse's status code is equal to the +// expected +func StatusCodeEquals(t *testing.T, res *http.Response, expected int, message string) { + if res.StatusCode != expected { + body, err := io.ReadAll(res.Body) + if err != nil { + t.Fatal(errors.Wrap(err, "reading body")) + } + + t.Errorf("status code mismatch. %s: got %v want %v. Message was: '%s'", message, res.StatusCode, expected, string(body)) + } +} diff --git a/pkg/assert/prompt.go b/pkg/assert/prompt.go new file mode 100644 index 00000000..c21e6a42 --- /dev/null +++ b/pkg/assert/prompt.go @@ -0,0 +1,84 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package assert + +import ( + "bufio" + "io" + "strings" + "time" + + "github.com/pkg/errors" +) + +// WaitForPrompt waits for an expected prompt to appear in stdout with a timeout. +// Returns an error if the prompt is not found within the timeout period. +// Handles prompts with or without newlines by reading character by character. +func WaitForPrompt(stdout io.Reader, expectedPrompt string, timeout time.Duration) error { + type result struct { + found bool + err error + } + resultCh := make(chan result, 1) + + go func() { + reader := bufio.NewReader(stdout) + var buffer strings.Builder + found := false + + for { + b, err := reader.ReadByte() + if err != nil { + resultCh <- result{found: found, err: err} + return + } + + buffer.WriteByte(b) + if strings.Contains(buffer.String(), expectedPrompt) { + found = true + break + } + } + + resultCh <- result{found: found, err: nil} + }() + + select { + case res := <-resultCh: + if res.err != nil && res.err != io.EOF { + return errors.Wrap(res.err, "reading stdout") + } + if !res.found { + return errors.Errorf("expected prompt '%s' not found in stdout", expectedPrompt) + } + return nil + case <-time.After(timeout): + return errors.Errorf("timeout waiting for prompt '%s'", expectedPrompt) + } +} + +// RespondToPrompt is a helper that waits for a prompt and sends a response. +func RespondToPrompt(stdout io.Reader, stdin io.WriteCloser, expectedPrompt, response string, timeout time.Duration) error { + if err := WaitForPrompt(stdout, expectedPrompt, timeout); err != nil { + return err + } + + if _, err := io.WriteString(stdin, response); err != nil { + return errors.Wrap(err, "writing response to stdin") + } + + return nil +} diff --git a/cli/.gitignore b/pkg/cli/.gitignore similarity index 87% rename from cli/.gitignore rename to pkg/cli/.gitignore index 7de9e987..87f89ac4 100644 --- a/cli/.gitignore +++ b/pkg/cli/.gitignore @@ -1,6 +1,6 @@ main *.swo -tmp/ +tmp*/ /vendor test-dnote /dist diff --git a/cli/COMMANDS.md b/pkg/cli/COMMANDS.md similarity index 68% rename from cli/COMMANDS.md rename to pkg/cli/COMMANDS.md index ec26b444..c3402152 100644 --- a/cli/COMMANDS.md +++ b/pkg/cli/COMMANDS.md @@ -38,35 +38,41 @@ dnote view dnote view golang # See details of a note -dnote view golang 12 +dnote view 12 ``` ## dnote edit _alias: e_ -Edit a note. +Edit a note or a book. ```bash -# Launch a text editor to edit a note with the given index. -dnote edit linux 1 +# Launch a text editor to edit a note with the given id. +dnote edit 12 -# Edit a note with the given index in the specified book with a content. -dnote edit linux 1 -c "New Content" +# Edit a note with the given id in the specified book with a content. +dnote edit 12 -c "New Content" + +# Launch a text editor to edit a book name. +dnote edit js + +# Edit a book name by using a flag. +dnote edit js -n "javascript" ``` ## dnote remove -_alias: d_ +_alias: rm, d_ Remove either a note or a book. ```bash -# Remove the note with `index` in the specified book. -dnote remove JS 1 +# Remove a note with an id. +dnote remove 1 -# Remove the book with the `book name`. -dnote remove -b JS +# Remove a book with the `book name`. +dnote remove js ``` ## dnote find @@ -88,20 +94,14 @@ dnote find "merge sort" -b algorithm ## dnote sync -_Dnote Pro only_ - _alias: s_ -Sync notes with Dnote server. All your data is encrypted before being sent to the server. +Sync notes with Dnote server. ## dnote login -_Dnote Pro only_ - Start a login prompt. ## dnote logout -_Dnote Pro only_ - Log out of Dnote. diff --git a/pkg/cli/README.md b/pkg/cli/README.md new file mode 100644 index 00000000..d177e902 --- /dev/null +++ b/pkg/cli/README.md @@ -0,0 +1 @@ +This directory contains the command line interface project for Dnote. diff --git a/cli/client/client.go b/pkg/cli/client/client.go similarity index 52% rename from cli/client/client.go rename to pkg/cli/client/client.go index 739a52fd..2be0006d 100644 --- a/cli/client/client.go +++ b/pkg/cli/client/client.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package client provides interfaces for interacting with the Dnote server @@ -23,22 +20,190 @@ package client import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strconv" "strings" "time" - "github.com/dnote/dnote/cli/crypt" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/utils" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/log" "github.com/pkg/errors" + "golang.org/x/time/rate" ) // ErrInvalidLogin is an error for invalid credentials for login var ErrInvalidLogin = errors.New("wrong credentials") +// ErrContentTypeMismatch is an error for invalid credentials for login +var ErrContentTypeMismatch = errors.New("content type mismatch") + +// HTTPError represents an HTTP error response from the server +type HTTPError struct { + StatusCode int + Message string +} + +func (e *HTTPError) Error() string { + return fmt.Sprintf(`response %d "%s"`, e.StatusCode, e.Message) +} + +// IsConflict returns true if the error is a 409 Conflict error +func (e *HTTPError) IsConflict() bool { + return e.StatusCode == 409 +} + +var contentTypeApplicationJSON = "application/json" +var contentTypeNone = "" + +// requestOptions contains options for requests +type requestOptions struct { + HTTPClient *http.Client + // ExpectedContentType is the Content-Type that the client is expecting from the server + ExpectedContentType *string +} + +const ( + // clientRateLimitPerSecond is the max requests per second the client will make + clientRateLimitPerSecond = 50 + // clientRateLimitBurst is the burst capacity for rate limiting + clientRateLimitBurst = 100 +) + +// rateLimitedTransport wraps an http.RoundTripper with rate limiting +type rateLimitedTransport struct { + transport http.RoundTripper + limiter *rate.Limiter +} + +func (t *rateLimitedTransport) RoundTrip(req *http.Request) (*http.Response, error) { + // Wait for rate limiter to allow the request + if err := t.limiter.Wait(req.Context()); err != nil { + return nil, err + } + return t.transport.RoundTrip(req) +} + +// NewRateLimitedHTTPClient creates an HTTP client with rate limiting +func NewRateLimitedHTTPClient() *http.Client { + // Calculate interval from rate: 1 second / requests per second + interval := time.Second / time.Duration(clientRateLimitPerSecond) + + transport := &rateLimitedTransport{ + transport: http.DefaultTransport, + limiter: rate.NewLimiter(rate.Every(interval), clientRateLimitBurst), + } + return &http.Client{ + Transport: transport, + } +} + +func getHTTPClient(ctx context.DnoteCtx, options *requestOptions) *http.Client { + if options != nil && options.HTTPClient != nil { + return options.HTTPClient + } + + if ctx.HTTPClient != nil { + return ctx.HTTPClient + } + + return &http.Client{} +} + +func getExpectedContentType(options *requestOptions) string { + if options != nil && options.ExpectedContentType != nil { + return *options.ExpectedContentType + } + + return contentTypeApplicationJSON +} + +func getReq(ctx context.DnoteCtx, path, method, body string) (*http.Request, error) { + endpoint := fmt.Sprintf("%s%s", ctx.APIEndpoint, path) + req, err := http.NewRequest(method, endpoint, strings.NewReader(body)) + if err != nil { + return nil, errors.Wrap(err, "constructing http request") + } + + req.Header.Set("CLI-Version", ctx.Version) + + if ctx.SessionKey != "" { + credential := fmt.Sprintf("Bearer %s", ctx.SessionKey) + req.Header.Set("Authorization", credential) + } + + return req, nil +} + +// checkRespErr checks if the given http response indicates an error. It returns a boolean indicating +// if the response is an error, and a decoded error message. +func checkRespErr(res *http.Response) error { + if res.StatusCode < 400 { + return nil + } + + body, err := io.ReadAll(res.Body) + if err != nil { + return errors.Wrapf(err, "server responded with %d but client could not read the response body", res.StatusCode) + } + + bodyStr := string(body) + return &HTTPError{ + StatusCode: res.StatusCode, + Message: strings.TrimRight(bodyStr, "\n"), + } +} + +func checkContentType(res *http.Response, options *requestOptions) error { + expected := getExpectedContentType(options) + + got := res.Header.Get("Content-Type") + if got != expected { + return errors.Wrapf(ErrContentTypeMismatch, "got: '%s' want: '%s'. Did you configure your endpoint correctly?", got, expected) + } + + return nil +} + +// doReq does a http request to the given path in the api endpoint +func doReq(ctx context.DnoteCtx, method, path, body string, options *requestOptions) (*http.Response, error) { + req, err := getReq(ctx, path, method, body) + if err != nil { + return nil, errors.Wrap(err, "getting request") + } + + log.Debug("HTTP %s %s\n", method, path) + + hc := getHTTPClient(ctx, options) + res, err := hc.Do(req) + if err != nil { + return res, errors.Wrap(err, "making http request") + } + + log.Debug("HTTP %d %s\n", res.StatusCode, res.Status) + + if err = checkRespErr(res); err != nil { + return res, errors.Wrap(err, "server responded with an error") + } + + if err = checkContentType(res, options); err != nil { + return res, errors.Wrap(err, "unexpected Content-Type") + } + + return res, nil +} + +// doAuthorizedReq does a http request to the given path in the api endpoint as a user, +// with the appropriate headers. The given path should include the preceding slash. +func doAuthorizedReq(ctx context.DnoteCtx, method, path, body string, options *requestOptions) (*http.Response, error) { + if ctx.SessionKey == "" { + return nil, errors.New("no session key found") + } + + return doReq(ctx, method, path, body, options) +} + // GetSyncStateResp is the response get sync state endpoint type GetSyncStateResp struct { FullSyncBefore int `json:"full_sync_before"` @@ -47,16 +212,15 @@ type GetSyncStateResp struct { } // GetSyncState gets the sync state response from the server -func GetSyncState(ctx infra.DnoteCtx) (GetSyncStateResp, error) { +func GetSyncState(ctx context.DnoteCtx) (GetSyncStateResp, error) { var ret GetSyncStateResp - hc := http.Client{} - res, err := utils.DoAuthorizedReq(ctx, hc, "GET", "/v1/sync/state", "") + res, err := doAuthorizedReq(ctx, "GET", "/v3/sync/state", "", nil) if err != nil { return ret, errors.Wrap(err, "constructing http request") } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return ret, errors.Wrap(err, "reading the response body") } @@ -79,7 +243,6 @@ type SyncFragNote struct { AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` Body string `json:"content"` - Public bool `json:"public"` Deleted bool `json:"deleted"` } @@ -112,16 +275,18 @@ type GetSyncFragmentResp struct { } // GetSyncFragment gets a sync fragment response from the server -func GetSyncFragment(ctx infra.DnoteCtx, afterUSN int) (GetSyncFragmentResp, error) { +func GetSyncFragment(ctx context.DnoteCtx, afterUSN int) (GetSyncFragmentResp, error) { v := url.Values{} v.Set("after_usn", strconv.Itoa(afterUSN)) queryStr := v.Encode() - path := fmt.Sprintf("/v1/sync/fragment?%s", queryStr) - hc := http.Client{} - res, err := utils.DoAuthorizedReq(ctx, hc, "GET", path, "") + path := fmt.Sprintf("/v3/sync/fragment?%s", queryStr) + res, err := doAuthorizedReq(ctx, "GET", path, "", nil) + if err != nil { + return GetSyncFragmentResp{}, errors.Wrap(err, "making the request") + } - body, err := ioutil.ReadAll(res.Body) + body, err := io.ReadAll(res.Body) if err != nil { return GetSyncFragmentResp{}, errors.Wrap(err, "reading the response body") } @@ -154,52 +319,21 @@ type CreateBookResp struct { Book RespBook `json:"book"` } -// checkRespErr checks if the given http response indicates an error. It returns a boolean indicating -// if the response is an error, and a decoded error message. -func checkRespErr(res *http.Response) (bool, string, error) { - if res.StatusCode < 400 { - return false, "", nil - } - - body, err := ioutil.ReadAll(res.Body) - if err != nil { - return true, "", errors.Wrapf(err, "server responded with %d but could not read the response body", res.StatusCode) - } - - bodyStr := string(body) - message := fmt.Sprintf(`response %d "%s"`, res.StatusCode, strings.TrimRight(bodyStr, "\n")) - return true, message, nil -} - // CreateBook creates a new book in the server -func CreateBook(ctx infra.DnoteCtx, label string) (CreateBookResp, error) { - encLabel, err := crypt.AesGcmEncrypt(ctx.CipherKey, []byte(label)) - if err != nil { - return CreateBookResp{}, errors.Wrap(err, "encrypting the label") - } - +func CreateBook(ctx context.DnoteCtx, label string) (CreateBookResp, error) { payload := CreateBookPayload{ - Name: encLabel, + Name: label, } b, err := json.Marshal(payload) if err != nil { return CreateBookResp{}, errors.Wrap(err, "marshaling payload") } - hc := http.Client{} - res, err := utils.DoAuthorizedReq(ctx, hc, "POST", "/v2/books", string(b)) + res, err := doAuthorizedReq(ctx, "POST", "/v3/books", string(b), nil) if err != nil { return CreateBookResp{}, errors.Wrap(err, "posting a book to the server") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return CreateBookResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return CreateBookResp{}, errors.New(message) - } - var resp CreateBookResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return resp, errors.Wrap(err, "decoding response payload") @@ -218,35 +352,21 @@ type UpdateBookResp struct { } // UpdateBook updates a book in the server -func UpdateBook(ctx infra.DnoteCtx, label, uuid string) (UpdateBookResp, error) { - encName, err := crypt.AesGcmEncrypt(ctx.CipherKey, []byte(label)) - if err != nil { - return UpdateBookResp{}, errors.Wrap(err, "encrypting the content") - } - +func UpdateBook(ctx context.DnoteCtx, label, uuid string) (UpdateBookResp, error) { payload := updateBookPayload{ - Name: &encName, + Name: &label, } b, err := json.Marshal(payload) if err != nil { return UpdateBookResp{}, errors.Wrap(err, "marshaling payload") } - hc := http.Client{} - endpoint := fmt.Sprintf("/v1/books/%s", uuid) - res, err := utils.DoAuthorizedReq(ctx, hc, "PATCH", endpoint, string(b)) + endpoint := fmt.Sprintf("/v3/books/%s", uuid) + res, err := doAuthorizedReq(ctx, "PATCH", endpoint, string(b), nil) if err != nil { return UpdateBookResp{}, errors.Wrap(err, "posting a book to the server") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return UpdateBookResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return UpdateBookResp{}, errors.New(message) - } - var resp UpdateBookResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return resp, errors.Wrap(err, "decoding payload") @@ -262,22 +382,13 @@ type DeleteBookResp struct { } // DeleteBook deletes a book in the server -func DeleteBook(ctx infra.DnoteCtx, uuid string) (DeleteBookResp, error) { - hc := http.Client{} - endpoint := fmt.Sprintf("/v1/books/%s", uuid) - res, err := utils.DoAuthorizedReq(ctx, hc, "DELETE", endpoint, "") +func DeleteBook(ctx context.DnoteCtx, uuid string) (DeleteBookResp, error) { + endpoint := fmt.Sprintf("/v3/books/%s", uuid) + res, err := doAuthorizedReq(ctx, "DELETE", endpoint, "", nil) if err != nil { return DeleteBookResp{}, errors.Wrap(err, "deleting a book in the server") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return DeleteBookResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return DeleteBookResp{}, errors.New(message) - } - var resp DeleteBookResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return resp, errors.Wrap(err, "decoding the response") @@ -320,35 +431,21 @@ type RespNote struct { } // CreateNote creates a note in the server -func CreateNote(ctx infra.DnoteCtx, bookUUID, content string) (CreateNoteResp, error) { - encBody, err := crypt.AesGcmEncrypt(ctx.CipherKey, []byte(content)) - if err != nil { - return CreateNoteResp{}, errors.Wrap(err, "encrypting the content") - } - +func CreateNote(ctx context.DnoteCtx, bookUUID, content string) (CreateNoteResp, error) { payload := CreateNotePayload{ BookUUID: bookUUID, - Body: encBody, + Body: content, } b, err := json.Marshal(payload) if err != nil { return CreateNoteResp{}, errors.Wrap(err, "marshaling payload") } - hc := http.Client{} - res, err := utils.DoAuthorizedReq(ctx, hc, "POST", "/v2/notes", string(b)) + res, err := doAuthorizedReq(ctx, "POST", "/v3/notes", string(b), nil) if err != nil { return CreateNoteResp{}, errors.Wrap(err, "posting a book to the server") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return CreateNoteResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return CreateNoteResp{}, errors.New(message) - } - var resp CreateNoteResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return CreateNoteResp{}, errors.Wrap(err, "decoding payload") @@ -360,7 +457,6 @@ func CreateNote(ctx infra.DnoteCtx, bookUUID, content string) (CreateNoteResp, e type updateNotePayload struct { BookUUID *string `json:"book_uuid"` Body *string `json:"content"` - Public *bool `json:"public"` } // UpdateNoteResp is the response from create book api @@ -370,37 +466,22 @@ type UpdateNoteResp struct { } // UpdateNote updates a note in the server -func UpdateNote(ctx infra.DnoteCtx, uuid, bookUUID, content string, public bool) (UpdateNoteResp, error) { - encBody, err := crypt.AesGcmEncrypt(ctx.CipherKey, []byte(content)) - if err != nil { - return UpdateNoteResp{}, errors.Wrap(err, "encrypting the content") - } - +func UpdateNote(ctx context.DnoteCtx, uuid, bookUUID, content string) (UpdateNoteResp, error) { payload := updateNotePayload{ BookUUID: &bookUUID, - Body: &encBody, - Public: &public, + Body: &content, } b, err := json.Marshal(payload) if err != nil { return UpdateNoteResp{}, errors.Wrap(err, "marshaling payload") } - hc := http.Client{} - endpoint := fmt.Sprintf("/v1/notes/%s", uuid) - res, err := utils.DoAuthorizedReq(ctx, hc, "PATCH", endpoint, string(b)) + endpoint := fmt.Sprintf("/v3/notes/%s", uuid) + res, err := doAuthorizedReq(ctx, "PATCH", endpoint, string(b), nil) if err != nil { return UpdateNoteResp{}, errors.Wrap(err, "patching a note to the server") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return UpdateNoteResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return UpdateNoteResp{}, errors.New(message) - } - var resp UpdateNoteResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return UpdateNoteResp{}, errors.Wrap(err, "decoding payload") @@ -416,22 +497,13 @@ type DeleteNoteResp struct { } // DeleteNote removes a note in the server -func DeleteNote(ctx infra.DnoteCtx, uuid string) (DeleteNoteResp, error) { - hc := http.Client{} - endpoint := fmt.Sprintf("/v1/notes/%s", uuid) - res, err := utils.DoAuthorizedReq(ctx, hc, "DELETE", endpoint, "") +func DeleteNote(ctx context.DnoteCtx, uuid string) (DeleteNoteResp, error) { + endpoint := fmt.Sprintf("/v3/notes/%s", uuid) + res, err := doAuthorizedReq(ctx, "DELETE", endpoint, "", nil) if err != nil { return DeleteNoteResp{}, errors.Wrap(err, "patching a note to the server") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return DeleteNoteResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return DeleteNoteResp{}, errors.New(message) - } - var resp DeleteNoteResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return DeleteNoteResp{}, errors.Wrap(err, "decoding payload") @@ -447,21 +519,12 @@ type GetBooksResp []struct { } // GetBooks gets books from the server -func GetBooks(ctx infra.DnoteCtx, sessionKey string) (GetBooksResp, error) { - hc := http.Client{} - res, err := utils.DoAuthorizedReq(ctx, hc, "GET", "/v1/books", "") +func GetBooks(ctx context.DnoteCtx, sessionKey string) (GetBooksResp, error) { + res, err := doAuthorizedReq(ctx, "GET", "/v3/books", "", nil) if err != nil { return GetBooksResp{}, errors.Wrap(err, "making http request") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return GetBooksResp{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return GetBooksResp{}, errors.New(message) - } - var resp GetBooksResp if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return GetBooksResp{}, errors.Wrap(err, "decoding payload") @@ -470,26 +533,18 @@ func GetBooks(ctx infra.DnoteCtx, sessionKey string) (GetBooksResp, error) { return resp, nil } -// PresigninResponse is a reponse from /v1/presignin endpoint +// PresigninResponse is a reponse from /v3/presignin endpoint type PresigninResponse struct { Iteration int `json:"iteration"` } // GetPresignin gets presignin credentials -func GetPresignin(ctx infra.DnoteCtx, email string) (PresigninResponse, error) { - res, err := utils.DoReq(ctx, "GET", fmt.Sprintf("/v1/presignin?email=%s", email), "") +func GetPresignin(ctx context.DnoteCtx, email string) (PresigninResponse, error) { + res, err := doReq(ctx, "GET", fmt.Sprintf("/v3/presignin?email=%s", email), "", nil) if err != nil { return PresigninResponse{}, errors.Wrap(err, "making http request") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return PresigninResponse{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return PresigninResponse{}, errors.New(message) - } - var resp PresigninResponse if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return PresigninResponse{}, errors.Wrap(err, "decoding payload") @@ -498,46 +553,38 @@ func GetPresignin(ctx infra.DnoteCtx, email string) (PresigninResponse, error) { return resp, nil } -// SigninPayload is a payload for /v1/signin +// SigninPayload is a payload for /v3/signin type SigninPayload struct { - Email string `json:"email"` - AuthKey string `json:"auth_key"` + Email string `json:"email"` + Passowrd string `json:"password"` } -// SigninResponse is a response from /v1/signin endpoint +// SigninResponse is a response from /v3/signin endpoint type SigninResponse struct { - Key string `json:"key"` - ExpiresAt int64 `json:"expires_at"` - CipherKeyEnc string `json:"cipher_key_enc"` + Key string `json:"key"` + ExpiresAt int64 `json:"expires_at"` } // Signin requests a session token -func Signin(ctx infra.DnoteCtx, email, authKey string) (SigninResponse, error) { +func Signin(ctx context.DnoteCtx, email, password string) (SigninResponse, error) { payload := SigninPayload{ - Email: email, - AuthKey: authKey, + Email: email, + Passowrd: password, } b, err := json.Marshal(payload) if err != nil { return SigninResponse{}, errors.Wrap(err, "marshaling payload") } - res, err := utils.DoReq(ctx, "POST", "/v1/signin", string(b)) + res, err := doReq(ctx, "POST", "/v3/signin", string(b), nil) if err != nil { + // Check if this is a 401 Unauthorized error + var httpErr *HTTPError + if errors.As(err, &httpErr) && httpErr.StatusCode == http.StatusUnauthorized { + return SigninResponse{}, ErrInvalidLogin + } return SigninResponse{}, errors.Wrap(err, "making http request") } - if res.StatusCode == http.StatusUnauthorized { - return SigninResponse{}, ErrInvalidLogin - } - - hasErr, message, err := checkRespErr(res) - if err != nil { - return SigninResponse{}, errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return SigninResponse{}, errors.New(message) - } - var resp SigninResponse if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { return SigninResponse{}, errors.Wrap(err, "decoding payload") @@ -547,26 +594,34 @@ func Signin(ctx infra.DnoteCtx, email, authKey string) (SigninResponse, error) { } // Signout deletes a user session on the server side -func Signout(ctx infra.DnoteCtx, sessionKey string) error { - hc := http.Client{ - // No need to follow redirect - CheckRedirect: func(req *http.Request, via []*http.Request) error { - return http.ErrUseLastResponse - }, +func Signout(ctx context.DnoteCtx, sessionKey string) error { + // Create a client that shares the transport (and thus rate limiter) from ctx.HTTPClient + // but doesn't follow redirects + var hc *http.Client + if ctx.HTTPClient != nil { + hc = &http.Client{ + Transport: ctx.HTTPClient.Transport, + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } + } else { + log.Warnf("No HTTP client configured for signout - falling back\n") + hc = &http.Client{ + CheckRedirect: func(req *http.Request, via []*http.Request) error { + return http.ErrUseLastResponse + }, + } } - res, err := utils.DoAuthorizedReq(ctx, hc, "POST", "/v1/signout", "") + opts := requestOptions{ + HTTPClient: hc, + ExpectedContentType: &contentTypeNone, + } + _, err := doAuthorizedReq(ctx, "POST", "/v3/signout", "", &opts) if err != nil { return errors.Wrap(err, "making http request") } - hasErr, message, err := checkRespErr(res) - if err != nil { - return errors.Wrap(err, "checking repsonse error") - } - if hasErr { - return errors.New(message) - } - return nil } diff --git a/pkg/cli/client/client_test.go b/pkg/cli/client/client_test.go new file mode 100644 index 00000000..f83a0e6f --- /dev/null +++ b/pkg/cli/client/client_test.go @@ -0,0 +1,234 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package client + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/pkg/errors" + "golang.org/x/time/rate" +) + +// startCommonTestServer starts a test HTTP server that simulates a common set of senarios +func startCommonTestServer() *httptest.Server { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // internal server error + if r.URL.String() == "/bad-api/v3/signout" && r.Method == "POST" { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusInternalServerError) + return + } + + // catch-all + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(``)) + })) + + return ts +} + +func TestSignIn(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/api/v3/signin" && r.Method == "POST" { + var payload SigninPayload + + err := json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) + return + } + + if payload.Email == "alice@example.com" && payload.Passowrd == "pass1234" { + resp := testutils.MustMarshalJSON(t, SigninResponse{ + Key: "somekey", + ExpiresAt: int64(1596439890), + }) + + w.Header().Set("Content-Type", "application/json") + w.Write(resp) + } else { + w.WriteHeader(http.StatusUnauthorized) + } + + return + } + })) + defer ts.Close() + + commonTs := startCommonTestServer() + defer commonTs.Close() + + correctEndpoint := fmt.Sprintf("%s/api", ts.URL) + testClient := NewRateLimitedHTTPClient() + + t.Run("success", func(t *testing.T) { + result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") + if err != nil { + t.Errorf("got signin request error: %+v", err.Error()) + } + + assert.Equal(t, result.Key, "somekey", "Key mismatch") + assert.Equal(t, result.ExpiresAt, int64(1596439890), "ExpiresAt mismatch") + }) + + t.Run("failure", func(t *testing.T) { + result, err := Signin(context.DnoteCtx{APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com", "incorrectpassword") + + assert.Equal(t, err, ErrInvalidLogin, "err mismatch") + assert.Equal(t, result.Key, "", "Key mismatch") + assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") + }) + + t.Run("server error", func(t *testing.T) { + endpoint := fmt.Sprintf("%s/bad-api", ts.URL) + result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") + if err == nil { + t.Error("error should have been returned") + } + + assert.Equal(t, result.Key, "", "Key mismatch") + assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") + }) + + t.Run("accidentally pointing to a catch-all handler", func(t *testing.T) { + endpoint := fmt.Sprintf("%s", ts.URL) + result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") + + assert.Equal(t, errors.Cause(err), ErrContentTypeMismatch, "error cause mismatch") + assert.Equal(t, result.Key, "", "Key mismatch") + assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") + }) + + t.Run("network error", func(t *testing.T) { + // Use an invalid endpoint that will fail to connect + endpoint := "http://localhost:99999/api" + result, err := Signin(context.DnoteCtx{APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com", "pass1234") + + if err == nil { + t.Error("error should have been returned for network failure") + } + assert.Equal(t, result.Key, "", "Key mismatch") + assert.Equal(t, result.ExpiresAt, int64(0), "ExpiresAt mismatch") + }) +} + +func TestSignOut(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/api/v3/signout" && r.Method == "POST" { + w.WriteHeader(http.StatusNoContent) + } + })) + defer ts.Close() + + commonTs := startCommonTestServer() + defer commonTs.Close() + + correctEndpoint := fmt.Sprintf("%s/api", ts.URL) + testClient := NewRateLimitedHTTPClient() + + t.Run("success", func(t *testing.T) { + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint, HTTPClient: testClient}, "alice@example.com") + if err != nil { + t.Errorf("got signout request error: %+v", err.Error()) + } + }) + + t.Run("server error", func(t *testing.T) { + endpoint := fmt.Sprintf("%s/bad-api", commonTs.URL) + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com") + if err == nil { + t.Error("error should have been returned") + } + }) + + t.Run("accidentally pointing to a catch-all handler", func(t *testing.T) { + endpoint := fmt.Sprintf("%s", commonTs.URL) + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: endpoint, HTTPClient: testClient}, "alice@example.com") + + assert.Equal(t, errors.Cause(err), ErrContentTypeMismatch, "error cause mismatch") + }) + + // Gracefully handle a case where http client was not initialized in the context. + t.Run("nil HTTPClient", func(t *testing.T) { + err := Signout(context.DnoteCtx{SessionKey: "somekey", APIEndpoint: correctEndpoint}, "alice@example.com") + if err != nil { + t.Errorf("got signout request error: %+v", err.Error()) + } + }) +} + +func TestRateLimitedTransport(t *testing.T) { + var requestCount atomic.Int32 + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestCount.Add(1) + w.WriteHeader(http.StatusOK) + })) + defer ts.Close() + + transport := &rateLimitedTransport{ + transport: http.DefaultTransport, + limiter: rate.NewLimiter(10, 5), + } + client := &http.Client{Transport: transport} + + // Make 10 requests + start := time.Now() + numRequests := 10 + for i := range numRequests { + req, _ := http.NewRequest("GET", ts.URL, nil) + resp, err := client.Do(req) + if err != nil { + t.Fatalf("Request %d failed: %v", i, err) + } + resp.Body.Close() + } + elapsed := time.Since(start) + + // Burst of 5, then 5 more at 10 req/s = 500ms minimum + if elapsed < 500*time.Millisecond { + t.Errorf("Rate limit not enforced: 10 requests took %v, expected >= 500ms", elapsed) + } + + assert.Equal(t, int(requestCount.Load()), 10, "request count mismatch") +} + +func TestHTTPError(t *testing.T) { + t.Run("IsConflict returns true for 409", func(t *testing.T) { + conflictErr := &HTTPError{ + StatusCode: 409, + Message: "Conflict", + } + + assert.Equal(t, conflictErr.IsConflict(), true, "IsConflict() should return true for 409") + + notFoundErr := &HTTPError{ + StatusCode: 404, + Message: "Not Found", + } + + assert.Equal(t, notFoundErr.IsConflict(), false, "IsConflict() should return false for 404") + }) +} diff --git a/pkg/cli/cmd/add/add.go b/pkg/cli/cmd/add/add.go new file mode 100644 index 00000000..3e6d089d --- /dev/null +++ b/pkg/cli/cmd/add/add.go @@ -0,0 +1,198 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package add + +import ( + "database/sql" + "time" + "os" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/output" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/dnote/dnote/pkg/cli/upgrade" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/dnote/dnote/pkg/cli/validate" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var contentFlag string + +var example = ` + * Open an editor to write content + dnote add git + + * Skip the editor by providing content directly + dnote add git -c "time is a part of the commit hash" + + * Send stdin content to a note + echo "a branch is just a pointer to a commit" | dnote add git + # or + dnote add git << EOF + pull is fetch with a merge + EOF` + +func preRun(cmd *cobra.Command, args []string) error { + if len(args) != 1 { + return errors.New("Incorrect number of argument") + } + + return nil +} + +// NewCmd returns a new add command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "add ", + Short: "Add a new note", + Aliases: []string{"a", "n", "new"}, + Example: example, + PreRunE: preRun, + RunE: newRun(ctx), + } + + f := cmd.Flags() + f.StringVarP(&contentFlag, "content", "c", "", "The new content for the note") + + return cmd +} + +func getContent(ctx context.DnoteCtx) (string, error) { + if contentFlag != "" { + return contentFlag, nil + } + + // check for piped content + fInfo, _ := os.Stdin.Stat() + if fInfo.Mode() & os.ModeCharDevice == 0 { + c, err := ui.ReadStdInput() + if err != nil { + return "", errors.Wrap(err, "Failed to get piped input") + } + return c, nil + } + + fpath, err := ui.GetTmpContentPath(ctx) + if err != nil { + return "", errors.Wrap(err, "getting temporarily content file path") + } + + c, err := ui.GetEditorInput(ctx, fpath) + if err != nil { + return "", errors.Wrap(err, "Failed to get editor input") + } + + return c, nil +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + bookName := args[0] + if err := validate.BookName(bookName); err != nil { + return errors.Wrap(err, "invalid book name") + } + + content, err := getContent(ctx) + if err != nil { + return errors.Wrap(err, "getting content") + } + if content == "" { + return errors.New("Empty content") + } + + ts := time.Now().UnixNano() + noteRowID, err := writeNote(ctx, bookName, content, ts) + if err != nil { + return errors.Wrap(err, "Failed to write note") + } + + log.Successf("added to %s\n", bookName) + + db := ctx.DB + info, err := database.GetNoteInfo(db, noteRowID) + if err != nil { + return err + } + + output.NoteInfo(os.Stdout, info) + + if err := upgrade.Check(ctx); err != nil { + log.Error(errors.Wrap(err, "automatically checking updates").Error()) + } + + return nil + } +} + +func writeNote(ctx context.DnoteCtx, bookLabel string, content string, ts int64) (int, error) { + tx, err := ctx.DB.Begin() + if err != nil { + return 0, errors.Wrap(err, "beginning a transaction") + } + + var bookUUID string + err = tx.QueryRow("SELECT uuid FROM books WHERE label = ?", bookLabel).Scan(&bookUUID) + if err == sql.ErrNoRows { + bookUUID, err = utils.GenerateUUID() + if err != nil { + return 0, errors.Wrap(err, "generating uuid") + } + + b := database.NewBook(bookUUID, bookLabel, 0, false, true) + err = b.Insert(tx) + if err != nil { + tx.Rollback() + return 0, errors.Wrap(err, "creating the book") + } + } else if err != nil { + return 0, errors.Wrap(err, "finding the book") + } + + noteUUID, err := utils.GenerateUUID() + if err != nil { + return 0, errors.Wrap(err, "generating uuid") + } + + n := database.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, true) + + err = n.Insert(tx) + if err != nil { + tx.Rollback() + return 0, errors.Wrap(err, "creating the note") + } + + var noteRowID int + err = tx.QueryRow(`SELECT notes.rowid + FROM notes + WHERE notes.uuid = ?`, noteUUID). + Scan(¬eRowID) + if err != nil { + tx.Rollback() + return noteRowID, errors.Wrap(err, "getting the note rowid") + } + + err = tx.Commit() + if err != nil { + tx.Rollback() + return noteRowID, errors.Wrap(err, "committing a transaction") + } + + return noteRowID, nil +} diff --git a/pkg/cli/cmd/edit/book.go b/pkg/cli/cmd/edit/book.go new file mode 100644 index 00000000..ab326e7f --- /dev/null +++ b/pkg/cli/cmd/edit/book.go @@ -0,0 +1,121 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package edit + +import ( + "strings" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/output" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/dnote/dnote/pkg/cli/validate" + "github.com/pkg/errors" +) + +func validateRunBookFlags() error { + if contentFlag != "" { + return errors.New("--content is invalid for editing a book") + } + if bookFlag != "" { + return errors.New("--book is invalid for editing a book") + } + + return nil +} + +func waitEditorBookName(ctx context.DnoteCtx) (string, error) { + fpath, err := ui.GetTmpContentPath(ctx) + if err != nil { + return "", errors.Wrap(err, "getting temporarily content file path") + } + + c, err := ui.GetEditorInput(ctx, fpath) + if err != nil { + return "", errors.Wrap(err, "getting editor input") + } + + // remove the newline at the end because files end with linebreaks in POSIX + c = strings.TrimSuffix(c, "\n") + c = strings.TrimSuffix(c, "\r\n") + + return c, nil +} + +func getName(ctx context.DnoteCtx) (string, error) { + if nameFlag != "" { + return nameFlag, nil + } + + c, err := waitEditorBookName(ctx) + if err != nil { + return "", errors.Wrap(err, "Failed to get editor input") + } + + return c, nil +} + +func runBook(ctx context.DnoteCtx, bookName string) error { + err := validateRunBookFlags() + if err != nil { + return errors.Wrap(err, "validating flags.") + } + + db := ctx.DB + uuid, err := database.GetBookUUID(db, bookName) + if err != nil { + return errors.Wrap(err, "getting book uuid") + } + + name, err := getName(ctx) + if err != nil { + return errors.Wrap(err, "getting name") + } + + err = validate.BookName(name) + if err != nil { + return errors.Wrap(err, "validating book name") + } + + tx, err := ctx.DB.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + err = database.UpdateBookName(tx, uuid, name) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "updating the book name") + } + + bookInfo, err := database.GetBookInfo(tx, uuid) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "getting book info") + } + + err = tx.Commit() + if err != nil { + tx.Rollback() + return errors.Wrap(err, "committing a transaction") + } + + log.Success("edited the book\n") + output.BookInfo(bookInfo) + + return nil +} diff --git a/pkg/cli/cmd/edit/edit.go b/pkg/cli/cmd/edit/edit.go new file mode 100644 index 00000000..16a7c413 --- /dev/null +++ b/pkg/cli/cmd/edit/edit.go @@ -0,0 +1,104 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package edit + +import ( + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var contentFlag string +var bookFlag string +var nameFlag string + +var example = ` + * Edit a note by id + dnote edit 3 + + * Edit a note without launching an editor + dnote edit 3 -c "new content" + + * Move a note to another book + dnote edit 3 -b javascript + + * Rename a book + dnote edit javascript + + * Rename a book without launching an editor + dnote edit javascript -n js +` + +// NewCmd returns a new edit command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "edit ", + Short: "Edit a note or a book", + Aliases: []string{"e"}, + Example: example, + PreRunE: preRun, + RunE: newRun(ctx), + } + + f := cmd.Flags() + f.StringVarP(&contentFlag, "content", "c", "", "a new content for the note") + f.StringVarP(&bookFlag, "book", "b", "", "the name of the book to move the note to") + f.StringVarP(&nameFlag, "name", "n", "", "a new name for a book") + + return cmd +} + +func preRun(cmd *cobra.Command, args []string) error { + if len(args) != 1 && len(args) != 2 { + return errors.New("Incorrect number of argument") + } + + return nil +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + // DEPRECATED: Remove in 1.0.0 + if len(args) == 2 { + log.Plain(log.ColorYellow.Sprintf("DEPRECATED: you no longer need to pass book name to the view command. e.g. `dnote view 123`.\n\n")) + + target := args[1] + + if err := runNote(ctx, target); err != nil { + return errors.Wrap(err, "editing note") + } + + return nil + } + + target := args[0] + + if utils.IsNumber(target) { + if err := runNote(ctx, target); err != nil { + return errors.Wrap(err, "editing note") + } + } else { + if err := runBook(ctx, target); err != nil { + return errors.Wrap(err, "editing book") + } + } + + return nil + } +} diff --git a/pkg/cli/cmd/edit/note.go b/pkg/cli/cmd/edit/note.go new file mode 100644 index 00000000..cb837e11 --- /dev/null +++ b/pkg/cli/cmd/edit/note.go @@ -0,0 +1,172 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package edit + +import ( + "database/sql" + "os" + "strconv" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/output" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/pkg/errors" +) + +func validateRunNoteFlags() error { + if nameFlag != "" { + return errors.New("--name is invalid for editing a book") + } + + return nil +} + +func waitEditorNoteContent(ctx context.DnoteCtx, note database.Note) (string, error) { + fpath, err := ui.GetTmpContentPath(ctx) + if err != nil { + return "", errors.Wrap(err, "getting temporarily content file path") + } + + if err := os.WriteFile(fpath, []byte(note.Body), 0644); err != nil { + return "", errors.Wrap(err, "preparing tmp content file") + } + + c, err := ui.GetEditorInput(ctx, fpath) + if err != nil { + return "", errors.Wrap(err, "getting editor input") + } + + return c, nil +} + +func getContent(ctx context.DnoteCtx, note database.Note) (string, error) { + if contentFlag != "" { + return contentFlag, nil + } + + c, err := waitEditorNoteContent(ctx, note) + if err != nil { + return "", errors.Wrap(err, "getting content from editor") + } + + return c, nil +} + +func changeContent(ctx context.DnoteCtx, tx *database.DB, note database.Note, content string) error { + if note.Body == content { + return errors.New("Nothing changed") + } + + if err := database.UpdateNoteContent(tx, ctx.Clock, note.RowID, content); err != nil { + return errors.Wrap(err, "updating the note") + } + + return nil +} + +func moveBook(ctx context.DnoteCtx, tx *database.DB, note database.Note, bookName string) error { + targetBookUUID, err := database.GetBookUUID(tx, bookName) + if err != nil { + return errors.Wrap(err, "finding book uuid") + } + + if note.BookUUID == targetBookUUID { + return errors.New("book has not changed") + } + + if err := database.UpdateNoteBook(tx, ctx.Clock, note.RowID, targetBookUUID); err != nil { + return errors.Wrap(err, "moving book") + } + + return nil +} + +func updateNote(ctx context.DnoteCtx, tx *database.DB, note database.Note, bookName, content string) error { + if bookName != "" { + if err := moveBook(ctx, tx, note, bookName); err != nil { + return errors.Wrap(err, "moving book") + } + } + if content != "" { + if err := changeContent(ctx, tx, note, content); err != nil { + return errors.Wrap(err, "changing content") + } + } + + return nil +} + +func runNote(ctx context.DnoteCtx, rowIDArg string) error { + err := validateRunNoteFlags() + if err != nil { + return errors.Wrap(err, "validating flags.") + } + + rowID, err := strconv.Atoi(rowIDArg) + if err != nil { + return errors.Wrap(err, "invalid rowid") + } + + db := ctx.DB + note, err := database.GetActiveNote(db, rowID) + if err == sql.ErrNoRows { + return errors.Errorf("note %d not found", rowID) + } else if err != nil { + return errors.Wrap(err, "querying the book") + } + + content := contentFlag + + // If no flag was provided, launch an editor to get the content + if bookFlag == "" && contentFlag == "" { + c, err := getContent(ctx, note) + if err != nil { + return errors.Wrap(err, "getting content from editor") + } + + content = c + } + + tx, err := ctx.DB.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + err = updateNote(ctx, tx, note, bookFlag, content) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "updating note fields") + } + + noteInfo, err := database.GetNoteInfo(tx, rowID) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "getting note info") + } + + err = tx.Commit() + if err != nil { + tx.Rollback() + return errors.Wrap(err, "committing a transaction") + } + + log.Success("edited the note\n") + output.NoteInfo(os.Stdout, noteInfo) + + return nil +} diff --git a/cli/cmd/find/find.go b/pkg/cli/cmd/find/find.go similarity index 79% rename from cli/cmd/find/find.go rename to pkg/cli/cmd/find/find.go index b03fc4cc..4723bcb4 100644 --- a/cli/cmd/find/find.go +++ b/pkg/cli/cmd/find/find.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package find @@ -23,9 +20,9 @@ import ( "fmt" "strings" - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -52,7 +49,7 @@ func preRun(cmd *cobra.Command, args []string) error { } // NewCmd returns a new remove command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { +func NewCmd(ctx context.DnoteCtx) *cobra.Command { cmd := &cobra.Command{ Use: "find", Short: "Find notes by keywords", @@ -130,7 +127,7 @@ func escapePhrase(s string) (string, error) { return b.String(), nil } -func doQuery(ctx infra.DnoteCtx, query, bookName string) (*sql.Rows, error) { +func doQuery(ctx context.DnoteCtx, query, bookName string) (*sql.Rows, error) { db := ctx.DB sql := `SELECT @@ -153,7 +150,7 @@ func doQuery(ctx infra.DnoteCtx, query, bookName string) (*sql.Rows, error) { return rows, err } -func newRun(ctx infra.DnoteCtx) core.RunEFunc { +func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { phrase, err := escapePhrase(args[0]) if err != nil { diff --git a/cli/cmd/find/lexer.go b/pkg/cli/cmd/find/lexer.go similarity index 71% rename from cli/cmd/find/lexer.go rename to pkg/cli/cmd/find/lexer.go index 2e9037bd..6115becc 100644 --- a/cli/cmd/find/lexer.go +++ b/pkg/cli/cmd/find/lexer.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package find diff --git a/cli/cmd/find/lexer_test.go b/pkg/cli/cmd/find/lexer_test.go similarity index 79% rename from cli/cmd/find/lexer_test.go rename to pkg/cli/cmd/find/lexer_test.go index 55118695..3a74ad5a 100644 --- a/cli/cmd/find/lexer_test.go +++ b/pkg/cli/cmd/find/lexer_test.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package find @@ -22,7 +19,7 @@ import ( "fmt" "testing" - "github.com/dnote/dnote/cli/testutils" + "github.com/dnote/dnote/pkg/assert" ) func TestScanToken(t *testing.T) { @@ -117,8 +114,8 @@ func TestScanToken(t *testing.T) { t.Run(fmt.Sprintf("test case %d", tcIdx), func(t *testing.T) { tok, nextIdx := scanToken(tc.idx, tc.input) - testutils.AssertEqual(t, nextIdx, tc.retIdx, "retIdx mismatch") - testutils.AssertDeepEqual(t, tok, tc.retTok, "retTok mismatch") + assert.Equal(t, nextIdx, tc.retIdx, "retIdx mismatch") + assert.DeepEqual(t, tok, tc.retTok, "retTok mismatch") }) } } @@ -225,7 +222,7 @@ func TestTokenize(t *testing.T) { t.Run(fmt.Sprintf("test case %d", tcIdx), func(t *testing.T) { tokens := tokenize(tc.input) - testutils.AssertDeepEqual(t, tokens, tc.tokens, "tokens mismatch") + assert.DeepEqual(t, tokens, tc.tokens, "tokens mismatch") }) } } diff --git a/pkg/cli/cmd/login/login.go b/pkg/cli/cmd/login/login.go new file mode 100644 index 00000000..c9b1dfd5 --- /dev/null +++ b/pkg/cli/cmd/login/login.go @@ -0,0 +1,184 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package login + +import ( + "fmt" + "net/url" + "strconv" + + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var example = ` + dnote login` + +var usernameFlag, passwordFlag, apiEndpointFlag string + +// NewCmd returns a new login command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "login", + Short: "Login to dnote server", + Example: example, + RunE: newRun(ctx), + } + + f := cmd.Flags() + f.StringVarP(&usernameFlag, "username", "u", "", "email address for authentication") + f.StringVarP(&passwordFlag, "password", "p", "", "password for authentication") + f.StringVar(&apiEndpointFlag, "apiEndpoint", "", "API endpoint to connect to (defaults to value in config)") + + return cmd +} + +// Do dervies credentials on the client side and requests a session token from the server +func Do(ctx context.DnoteCtx, email, password string) error { + signinResp, err := client.Signin(ctx, email, password) + if err != nil { + return errors.Wrap(err, "requesting session") + } + + db := ctx.DB + tx, err := db.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + if err := database.UpsertSystem(tx, consts.SystemSessionKey, signinResp.Key); err != nil { + return errors.Wrap(err, "saving session key") + } + if err := database.UpsertSystem(tx, consts.SystemSessionKeyExpiry, strconv.FormatInt(signinResp.ExpiresAt, 10)); err != nil { + return errors.Wrap(err, "saving session key") + } + + tx.Commit() + + return nil +} + +func getUsername() (string, error) { + if usernameFlag != "" { + return usernameFlag, nil + } + + var email string + if err := ui.PromptInput("email", &email); err != nil { + return "", errors.Wrap(err, "getting email input") + } + if email == "" { + return "", errors.New("Email is empty") + } + + return email, nil +} + +func getPassword() (string, error) { + if passwordFlag != "" { + return passwordFlag, nil + } + + var password string + if err := ui.PromptPassword("password", &password); err != nil { + return "", errors.Wrap(err, "getting password input") + } + if password == "" { + return "", errors.New("Password is empty") + } + + return password, nil +} + +func getBaseURL(rawURL string) (string, error) { + u, err := url.Parse(rawURL) + if err != nil { + return "", errors.Wrap(err, "parsing url") + } + + if u.Scheme == "" || u.Host == "" { + return "", nil + } + + return fmt.Sprintf("%s://%s", u.Scheme, u.Host), nil +} + +func getServerDisplayURL(ctx context.DnoteCtx) string { + baseURL, err := getBaseURL(ctx.APIEndpoint) + if err != nil { + return "" + } + + return baseURL +} + +func getGreeting(ctx context.DnoteCtx) string { + base := "Welcome to Dnote" + + serverURL := getServerDisplayURL(ctx) + if serverURL == "" { + return fmt.Sprintf("%s\n", base) + } + + return fmt.Sprintf("%s (%s)\n", base, serverURL) +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + // Override APIEndpoint if flag was provided + if apiEndpointFlag != "" { + ctx.APIEndpoint = apiEndpointFlag + } + + greeting := getGreeting(ctx) + log.Plain(greeting) + + email, err := getUsername() + if err != nil { + return errors.Wrap(err, "getting email input") + } + + password, err := getPassword() + if err != nil { + return errors.Wrap(err, "getting password input") + } + if password == "" { + return errors.New("Password is empty") + } + + log.Debug("Logging in with email: %s and password: (length %d)\n", email, len(password)) + + err = Do(ctx, email, password) + if errors.Cause(err) == client.ErrInvalidLogin { + log.Error("wrong login\n") + return nil + } else if err != nil { + return errors.Wrap(err, "logging in") + } + + log.Success("logged in\n") + + return nil + } + +} diff --git a/pkg/cli/cmd/login/login_test.go b/pkg/cli/cmd/login/login_test.go new file mode 100644 index 00000000..6d5917cc --- /dev/null +++ b/pkg/cli/cmd/login/login_test.go @@ -0,0 +1,67 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package login + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" +) + +func TestGetServerDisplayURL(t *testing.T) { + testCases := []struct { + apiEndpoint string + expected string + }{ + { + apiEndpoint: "https://dnote.mydomain.com/api", + expected: "https://dnote.mydomain.com", + }, + { + apiEndpoint: "https://mysubdomain.mydomain.com/dnote/api", + expected: "https://mysubdomain.mydomain.com", + }, + { + apiEndpoint: "https://dnote.mysubdomain.mydomain.com/api", + expected: "https://dnote.mysubdomain.mydomain.com", + }, + { + apiEndpoint: "some-string", + expected: "", + }, + { + apiEndpoint: "", + expected: "", + }, + { + apiEndpoint: "https://", + expected: "", + }, + { + apiEndpoint: "https://abc", + expected: "https://abc", + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("for input %s", tc.apiEndpoint), func(t *testing.T) { + got := getServerDisplayURL(context.DnoteCtx{APIEndpoint: tc.apiEndpoint}) + assert.Equal(t, got, tc.expected, "result mismatch") + }) + } +} diff --git a/pkg/cli/cmd/logout/logout.go b/pkg/cli/cmd/logout/logout.go new file mode 100644 index 00000000..98cd69eb --- /dev/null +++ b/pkg/cli/cmd/logout/logout.go @@ -0,0 +1,106 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package logout + +import ( + "database/sql" + + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +// ErrNotLoggedIn is an error for logging out when not logged in +var ErrNotLoggedIn = errors.New("not logged in") + +var example = ` + dnote logout` + +var apiEndpointFlag string + +// NewCmd returns a new logout command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "logout", + Short: "Logout from the server", + Example: example, + RunE: newRun(ctx), + } + + f := cmd.Flags() + f.StringVar(&apiEndpointFlag, "apiEndpoint", "", "API endpoint to connect to (defaults to value in config)") + + return cmd +} + +// Do performs logout +func Do(ctx context.DnoteCtx) error { + db := ctx.DB + tx, err := db.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + var key string + err = database.GetSystem(tx, consts.SystemSessionKey, &key) + if errors.Cause(err) == sql.ErrNoRows { + return ErrNotLoggedIn + } else if err != nil { + return errors.Wrap(err, "getting session key") + } + + err = client.Signout(ctx, key) + if err != nil { + return errors.Wrap(err, "requesting logout") + } + + if err := database.DeleteSystem(tx, consts.SystemSessionKey); err != nil { + return errors.Wrap(err, "deleting session key") + } + if err := database.DeleteSystem(tx, consts.SystemSessionKeyExpiry); err != nil { + return errors.Wrap(err, "deleting session key expiry") + } + + tx.Commit() + + return nil +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + // Override APIEndpoint if flag was provided + if apiEndpointFlag != "" { + ctx.APIEndpoint = apiEndpointFlag + } + + err := Do(ctx) + if err == ErrNotLoggedIn { + log.Error("not logged in\n") + return nil + } else if err != nil { + return errors.Wrap(err, "logging out") + } + + log.Success("logged out\n") + + return nil + } +} diff --git a/pkg/cli/cmd/remove/remove.go b/pkg/cli/cmd/remove/remove.go new file mode 100644 index 00000000..18224c0f --- /dev/null +++ b/pkg/cli/cmd/remove/remove.go @@ -0,0 +1,212 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package remove + +import ( + "fmt" + "os" + "strconv" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/output" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var bookFlag string +var yesFlag bool + +var example = ` + * Delete a note by id + dnote delete 2 + + * Delete a book by name + dnote delete js +` + +// NewCmd returns a new remove command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "remove ", + Short: "Remove a note or a book", + Aliases: []string{"rm", "d", "delete"}, + Example: example, + PreRunE: preRun, + RunE: newRun(ctx), + } + + f := cmd.Flags() + f.StringVarP(&bookFlag, "book", "b", "", "The book name to delete") + f.BoolVarP(&yesFlag, "yes", "y", false, "Assume yes to the prompts and run in non-interactive mode") + + f.MarkDeprecated("book", "Pass the book name as an argument. e.g. `dnote rm book_name`") + + return cmd +} + +func preRun(cmd *cobra.Command, args []string) error { + if len(args) != 1 && len(args) != 2 { + return errors.New("Incorrect number of argument") + } + + return nil +} + +func maybeConfirm(message string, defaultValue bool) (bool, error) { + if yesFlag { + return true, nil + } + + return ui.Confirm(message, defaultValue) +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + // DEPRECATED: Remove in 1.0.0 + if bookFlag != "" { + if err := runBook(ctx, bookFlag); err != nil { + return errors.Wrap(err, "removing the book") + } + + return nil + } + + // DEPRECATED: Remove in 1.0.0 + if len(args) == 2 { + log.Plain(log.ColorYellow.Sprintf("DEPRECATED: you no longer need to pass book name to the remove command. e.g. `dnote remove 123`.\n\n")) + + target := args[1] + if err := runNote(ctx, target); err != nil { + return errors.Wrap(err, "removing the note") + } + + return nil + } + + target := args[0] + + if utils.IsNumber(target) { + if err := runNote(ctx, target); err != nil { + return errors.Wrap(err, "removing the note") + } + } else { + if err := runBook(ctx, target); err != nil { + return errors.Wrap(err, "removing the book") + } + } + + return nil + } +} + +func runNote(ctx context.DnoteCtx, rowIDArg string) error { + db := ctx.DB + + noteRowID, err := strconv.Atoi(rowIDArg) + if err != nil { + return errors.Wrap(err, "invalid rowid") + } + + noteInfo, err := database.GetNoteInfo(db, noteRowID) + if err != nil { + return err + } + + output.NoteInfo(os.Stdout, noteInfo) + + ok, err := maybeConfirm("remove this note?", false) + if err != nil { + return errors.Wrap(err, "getting confirmation") + } + if !ok { + log.Warnf("aborted by user\n") + return nil + } + + tx, err := db.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE uuid = ?", true, true, "", noteInfo.UUID); err != nil { + tx.Rollback() + return errors.Wrap(err, "removing the note") + } + + err = tx.Commit() + if err != nil { + tx.Rollback() + return errors.Wrap(err, "comitting transaction") + } + + log.Successf("removed from %s\n", noteInfo.BookLabel) + + return nil +} + +func runBook(ctx context.DnoteCtx, bookLabel string) error { + db := ctx.DB + + bookUUID, err := database.GetBookUUID(db, bookLabel) + if err != nil { + return errors.Wrap(err, "finding book uuid") + } + + ok, err := maybeConfirm(fmt.Sprintf("delete book '%s' and all its notes?", bookLabel), false) + if err != nil { + return errors.Wrap(err, "getting confirmation") + } + if !ok { + log.Warnf("aborted by user\n") + return nil + } + + tx, err := db.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + if _, err = tx.Exec("UPDATE notes SET deleted = ?, dirty = ?, body = ? WHERE book_uuid = ?", true, true, "", bookUUID); err != nil { + tx.Rollback() + return errors.Wrap(err, "removing notes in the book") + } + + // override the label with a random string + uniqLabel, err := utils.GenerateUUID() + if err != nil { + return errors.Wrap(err, "generating uuid to override with") + } + + if _, err = tx.Exec("UPDATE books SET deleted = ?, dirty = ?, label = ? WHERE uuid = ?", true, true, uniqLabel, bookUUID); err != nil { + tx.Rollback() + return errors.Wrap(err, "removing the book") + } + + err = tx.Commit() + if err != nil { + tx.Rollback() + return errors.Wrap(err, "committing transaction") + } + + log.Success("removed book\n") + + return nil +} diff --git a/pkg/cli/cmd/root/root.go b/pkg/cli/cmd/root/root.go new file mode 100644 index 00000000..6da096bf --- /dev/null +++ b/pkg/cli/cmd/root/root.go @@ -0,0 +1,56 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package root + +import ( + "github.com/spf13/cobra" +) + +var dbPathFlag string + +var root = &cobra.Command{ + Use: "dnote", + Short: "Dnote - a simple command line notebook", + SilenceErrors: true, + SilenceUsage: true, + CompletionOptions: cobra.CompletionOptions{ + DisableDefaultCmd: true, + }, +} + +func init() { + root.PersistentFlags().StringVar(&dbPathFlag, "dbPath", "", "the path to the database file (defaults to standard location)") +} + +// GetRoot returns the root command +func GetRoot() *cobra.Command { + return root +} + +// GetDBPathFlag returns the value of the --dbPath flag +func GetDBPathFlag() string { + return dbPathFlag +} + +// Register adds a new command +func Register(cmd *cobra.Command) { + root.AddCommand(cmd) +} + +// Execute runs the main command +func Execute() error { + return root.Execute() +} diff --git a/pkg/cli/cmd/sync/merge.go b/pkg/cli/cmd/sync/merge.go new file mode 100644 index 00000000..0e4d15be --- /dev/null +++ b/pkg/cli/cmd/sync/merge.go @@ -0,0 +1,201 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "database/sql" + "fmt" + "strings" + + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/dnote/dnote/pkg/cli/utils/diff" + "github.com/pkg/errors" +) + +const ( + modeNormal = iota + modeRemote + modeLocal +) + +const ( + conflictLabelLocal = "<<<<<<< Local\n" + conflictLabelServer = ">>>>>>> Server\n" + conflictLabelDivide = "=======\n" +) + +func sanitize(s string) string { + var textBuilder strings.Builder + textBuilder.WriteString(s) + if !strings.HasSuffix(s, "\n") { + textBuilder.WriteString("\n") + } + + return textBuilder.String() +} + +// reportBodyConflict returns a conflict report of the local and the remote version +// of a body +func reportBodyConflict(localBody, remoteBody string) string { + diffs := diff.Do(localBody, remoteBody) + + var ret strings.Builder + mode := modeNormal + maxIdx := len(diffs) - 1 + + for idx, d := range diffs { + if d.Type == diff.DiffEqual { + if mode != modeNormal { + mode = modeNormal + ret.WriteString(conflictLabelServer) + } + + ret.WriteString(d.Text) + } + + // within the conflict area, append a linebreak to the text if it is missing one + // to make sure conflict labels are separated by new lines + sanitized := sanitize(d.Text) + + if d.Type == diff.DiffDelete { + if mode == modeNormal { + mode = modeLocal + ret.WriteString(conflictLabelLocal) + } + + ret.WriteString(sanitized) + } + + if d.Type == diff.DiffInsert { + if mode == modeLocal { + mode = modeRemote + ret.WriteString(conflictLabelDivide) + } + + ret.WriteString(sanitized) + + if idx == maxIdx { + ret.WriteString(conflictLabelServer) + } + } + } + + return ret.String() +} + +func maxInt64(a, b int64) int64 { + if a > b { + return a + } + + return b +} + +func reportBookConflict(tx *database.DB, body, localBookUUID, serverBookUUID string) (string, error) { + var builder strings.Builder + + var localBookName, serverBookName string + if err := tx.QueryRow("SELECT label FROM books WHERE uuid = ?", localBookUUID).Scan(&localBookName); err != nil { + return "", errors.Wrapf(err, "getting book label for %s", localBookUUID) + } + if err := tx.QueryRow("SELECT label FROM books WHERE uuid = ?", serverBookUUID).Scan(&serverBookName); err != nil { + return "", errors.Wrapf(err, "getting book label for %s", serverBookUUID) + } + + builder.WriteString(conflictLabelLocal) + builder.WriteString(fmt.Sprintf("Moved to the book %s\n", localBookName)) + builder.WriteString(conflictLabelDivide) + builder.WriteString(fmt.Sprintf("Moved to the book %s\n", serverBookName)) + builder.WriteString(conflictLabelServer) + builder.WriteString("\n") + builder.WriteString(body) + + return builder.String(), nil +} + +func getConflictsBookUUID(tx *database.DB) (string, error) { + var ret string + + err := tx.QueryRow("SELECT uuid FROM books WHERE label = ?", "conflicts").Scan(&ret) + if err == sql.ErrNoRows { + // Create a conflicts book + ret, err = utils.GenerateUUID() + if err != nil { + return "", err + } + + b := database.NewBook(ret, "conflicts", 0, false, true) + err = b.Insert(tx) + if err != nil { + tx.Rollback() + return "", errors.Wrap(err, "creating the conflicts book") + } + } else if err != nil { + return "", errors.Wrap(err, "getting uuid for conflicts book") + } + + return ret, nil +} + +// noteMergeReport holds the result of a field-by-field merge of two copies of notes +type noteMergeReport struct { + body string + bookUUID string + editedOn int64 +} + +// mergeNoteFields performs a field-by-field merge between the local and the server copy. It returns a merge report +// between the local and the server copy of the note. +func mergeNoteFields(tx *database.DB, localNote database.Note, serverNote client.SyncFragNote) (*noteMergeReport, error) { + if !localNote.Dirty { + return ¬eMergeReport{ + body: serverNote.Body, + bookUUID: serverNote.BookUUID, + editedOn: serverNote.EditedOn, + }, nil + } + + body := reportBodyConflict(localNote.Body, serverNote.Body) + + var bookUUID string + if serverNote.BookUUID != localNote.BookUUID { + b, err := reportBookConflict(tx, body, localNote.BookUUID, serverNote.BookUUID) + if err != nil { + return nil, errors.Wrapf(err, "reporting book conflict for note %s", localNote.UUID) + } + + body = b + + conflictsBookUUID, err := getConflictsBookUUID(tx) + if err != nil { + return nil, errors.Wrap(err, "getting the conflicts book uuid") + } + + bookUUID = conflictsBookUUID + } else { + bookUUID = serverNote.BookUUID + } + + ret := noteMergeReport{ + body: body, + bookUUID: bookUUID, + editedOn: maxInt64(localNote.EditedOn, serverNote.EditedOn), + } + + return &ret, nil +} diff --git a/pkg/cli/cmd/sync/merge_test.go b/pkg/cli/cmd/sync/merge_test.go new file mode 100644 index 00000000..e6f4839e --- /dev/null +++ b/pkg/cli/cmd/sync/merge_test.go @@ -0,0 +1,151 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestReportConflict(t *testing.T) { + testCases := []struct { + local string + server string + expected string + }{ + { + local: "\n", + server: "\n", + expected: "\n", + }, + { + local: "", + server: "", + expected: "", + }, + { + local: "foo", + server: "foo", + expected: "foo", + }, + { + local: "foo\nbar", + server: "foo\nbar", + expected: "foo\nbar", + }, + { + local: "foo-local", + server: "foo-server", + expected: `<<<<<<< Local +foo-local +======= +foo-server +>>>>>>> Server +`, + }, + { + local: "foo\n", + server: "bar\n", + expected: `<<<<<<< Local +foo +======= +bar +>>>>>>> Server +`, + }, + { + local: "foo\n", + server: "\n", + expected: `<<<<<<< Local +foo +======= + +>>>>>>> Server +`, + }, + + { + local: "\n", + server: "foo\n", + expected: `<<<<<<< Local + +======= +foo +>>>>>>> Server +`, + }, + { + local: "foo\n\nquz\nbaz\n", + server: "foo\n\nbar\nbaz\n", + expected: `foo + +<<<<<<< Local +quz +======= +bar +>>>>>>> Server +baz +`, + }, + { + local: "foo\n\nquz\nbaz\n\nqux quz\nfuz\n", + server: "foo\n\nbar\nbaz\n\nqux quz\nfuuz\n", + expected: `foo + +<<<<<<< Local +quz +======= +bar +>>>>>>> Server +baz + +qux quz +<<<<<<< Local +fuz +======= +fuuz +>>>>>>> Server +`, + }, + { + local: "foo\nquz\nbaz\nbar\n", + server: "foo\nquzz\nbazz\nbar\n", + expected: `foo +<<<<<<< Local +quz +======= +quzz +>>>>>>> Server +<<<<<<< Local +baz +======= +bazz +>>>>>>> Server +bar +`, + }, + } + + for idx, tc := range testCases { + result := reportBodyConflict(tc.local, tc.server) + + t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { + assert.DeepEqual(t, result, tc.expected, "result mismatch") + }) + } +} diff --git a/cli/cmd/sync/sync.go b/pkg/cli/cmd/sync/sync.go similarity index 58% rename from cli/cmd/sync/sync.go rename to pkg/cli/cmd/sync/sync.go index ee846b34..6cb506ae 100644 --- a/cli/cmd/sync/sync.go +++ b/pkg/cli/cmd/sync/sync.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package sync @@ -22,12 +19,15 @@ import ( "database/sql" "fmt" - "github.com/dnote/dnote/cli/client" - "github.com/dnote/dnote/cli/core" - "github.com/dnote/dnote/cli/crypt" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/migrate" + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/migrate" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/dnote/dnote/pkg/cli/upgrade" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -41,9 +41,10 @@ var example = ` dnote sync` var isFullSync bool +var apiEndpointFlag string // NewCmd returns a new sync command -func NewCmd(ctx infra.DnoteCtx) *cobra.Command { +func NewCmd(ctx context.DnoteCtx) *cobra.Command { cmd := &cobra.Command{ Use: "sync", Aliases: []string{"s"}, @@ -54,24 +55,25 @@ func NewCmd(ctx infra.DnoteCtx) *cobra.Command { f := cmd.Flags() f.BoolVarP(&isFullSync, "full", "f", false, "perform a full sync instead of incrementally syncing only the changed data.") + f.StringVar(&apiEndpointFlag, "apiEndpoint", "", "API endpoint to connect to (defaults to value in config)") return cmd } -func getLastSyncAt(tx *infra.DB) (int, error) { +func getLastSyncAt(tx *database.DB) (int, error) { var ret int - if err := core.GetSystem(tx, infra.SystemLastSyncAt, &ret); err != nil { + if err := database.GetSystem(tx, consts.SystemLastSyncAt, &ret); err != nil { return ret, errors.Wrap(err, "querying last sync time") } return ret, nil } -func getLastMaxUSN(tx *infra.DB) (int, error) { +func getLastMaxUSN(tx *database.DB) (int, error) { var ret int - if err := core.GetSystem(tx, infra.SystemLastMaxUSN, &ret); err != nil { + if err := database.GetSystem(tx, consts.SystemLastMaxUSN, &ret); err != nil { return ret, errors.Wrap(err, "querying last user max_usn") } @@ -85,6 +87,7 @@ type syncList struct { ExpungedNotes map[string]bool ExpungedBooks map[string]bool MaxUSN int + UserMaxUSN int // Server's actual max USN (for distinguishing empty fragment vs empty server) MaxCurrentTime int64 } @@ -92,35 +95,21 @@ func (l syncList) getLength() int { return len(l.Notes) + len(l.Books) + len(l.ExpungedNotes) + len(l.ExpungedBooks) } -// processFragments categorizes items in sync fragments into a sync list. It also decrypts any -// encrypted data in sync fragments. -func processFragments(fragments []client.SyncFragment, cipherKey []byte) (syncList, error) { +// processFragments categorizes items in sync fragments into a sync list. +func processFragments(fragments []client.SyncFragment) (syncList, error) { notes := map[string]client.SyncFragNote{} books := map[string]client.SyncFragBook{} expungedNotes := map[string]bool{} expungedBooks := map[string]bool{} var maxUSN int + var userMaxUSN int var maxCurrentTime int64 for _, fragment := range fragments { for _, note := range fragment.Notes { - log.Debug("decrypting note %s\n", note.UUID) - bodyDec, err := crypt.AesGcmDecrypt(cipherKey, note.Body) - if err != nil { - return syncList{}, errors.Wrapf(err, "decrypting body for note %s", note.UUID) - } - - note.Body = string(bodyDec) notes[note.UUID] = note } for _, book := range fragment.Books { - log.Debug("decrypting book %s\n", book.UUID) - labelDec, err := crypt.AesGcmDecrypt(cipherKey, book.Label) - if err != nil { - return syncList{}, errors.Wrapf(err, "decrypting label for book %s", book.UUID) - } - - book.Label = string(labelDec) books[book.UUID] = book } for _, uuid := range fragment.ExpungedBooks { @@ -133,6 +122,9 @@ func processFragments(fragments []client.SyncFragment, cipherKey []byte) (syncLi if fragment.FragMaxUSN > maxUSN { maxUSN = fragment.FragMaxUSN } + if fragment.UserMaxUSN > userMaxUSN { + userMaxUSN = fragment.UserMaxUSN + } if fragment.CurrentTime > maxCurrentTime { maxCurrentTime = fragment.CurrentTime } @@ -144,6 +136,7 @@ func processFragments(fragments []client.SyncFragment, cipherKey []byte) (syncLi ExpungedNotes: expungedNotes, ExpungedBooks: expungedBooks, MaxUSN: maxUSN, + UserMaxUSN: userMaxUSN, MaxCurrentTime: maxCurrentTime, } @@ -152,13 +145,13 @@ func processFragments(fragments []client.SyncFragment, cipherKey []byte) (syncLi // getSyncList gets a list of all sync fragments after the specified usn // and aggregates them into a syncList data structure -func getSyncList(ctx infra.DnoteCtx, afterUSN int) (syncList, error) { +func getSyncList(ctx context.DnoteCtx, afterUSN int) (syncList, error) { fragments, err := getSyncFragments(ctx, afterUSN) if err != nil { return syncList{}, errors.Wrap(err, "getting sync fragments") } - ret, err := processFragments(fragments, ctx.CipherKey) + ret, err := processFragments(fragments) if err != nil { return syncList{}, errors.Wrap(err, "making sync list") } @@ -168,7 +161,7 @@ func getSyncList(ctx infra.DnoteCtx, afterUSN int) (syncList, error) { // getSyncFragments repeatedly gets all sync fragments after the specified usn until there is no more new data // remaining and returns the buffered list -func getSyncFragments(ctx infra.DnoteCtx, afterUSN int) ([]client.SyncFragment, error) { +func getSyncFragments(ctx context.DnoteCtx, afterUSN int) ([]client.SyncFragment, error) { var buf []client.SyncFragment nextAfterUSN := afterUSN @@ -190,18 +183,75 @@ func getSyncFragments(ctx infra.DnoteCtx, afterUSN int) ([]client.SyncFragment, } } - log.Debug("received sync fragments: %+v\n", buf) + log.Debug("received sync fragments: %+v\n", redactSyncFragments(buf)) return buf, nil } +// redactSyncFragments returns a deep copy of sync fragments with sensitive fields (note body, book label) removed for safe logging +func redactSyncFragments(fragments []client.SyncFragment) []client.SyncFragment { + redacted := make([]client.SyncFragment, len(fragments)) + for i, frag := range fragments { + // Create new notes with redacted bodies + notes := make([]client.SyncFragNote, len(frag.Notes)) + for j, note := range frag.Notes { + notes[j] = client.SyncFragNote{ + UUID: note.UUID, + BookUUID: note.BookUUID, + USN: note.USN, + CreatedAt: note.CreatedAt, + UpdatedAt: note.UpdatedAt, + AddedOn: note.AddedOn, + EditedOn: note.EditedOn, + Body: func() string { + if note.Body != "" { + return "" + } + return "" + }(), + Deleted: note.Deleted, + } + } + + // Create new books with redacted labels + books := make([]client.SyncFragBook, len(frag.Books)) + for j, book := range frag.Books { + books[j] = client.SyncFragBook{ + UUID: book.UUID, + USN: book.USN, + CreatedAt: book.CreatedAt, + UpdatedAt: book.UpdatedAt, + AddedOn: book.AddedOn, + Label: func() string { + if book.Label != "" { + return "" + } + return "" + }(), + Deleted: book.Deleted, + } + } + + redacted[i] = client.SyncFragment{ + FragMaxUSN: frag.FragMaxUSN, + UserMaxUSN: frag.UserMaxUSN, + CurrentTime: frag.CurrentTime, + Notes: notes, + Books: books, + ExpungedNotes: frag.ExpungedNotes, + ExpungedBooks: frag.ExpungedBooks, + } + } + return redacted +} + // resolveLabel resolves a book label conflict by repeatedly appending an increasing integer // to the label until it finds a unique label. It returns the first non-conflicting label. -func resolveLabel(tx *infra.DB, label string) (string, error) { +func resolveLabel(tx *database.DB, label string) (string, error) { var ret string for i := 2; ; i++ { - ret = fmt.Sprintf("%s (%d)", label, i) + ret = fmt.Sprintf("%s_%d", label, i) var cnt int if err := tx.QueryRow("SELECT count(*) FROM books WHERE label = ?", ret).Scan(&cnt); err != nil { @@ -218,7 +268,7 @@ func resolveLabel(tx *infra.DB, label string) (string, error) { // mergeBook inserts or updates the given book in the local database. // If a book with a duplicate label exists locally, it renames the duplicate by appending a number. -func mergeBook(tx *infra.DB, b client.SyncFragBook, mode int) error { +func mergeBook(tx *database.DB, b client.SyncFragBook, mode int) error { var count int if err := tx.QueryRow("SELECT count(*) FROM books WHERE label = ?", b.Label).Scan(&count); err != nil { return errors.Wrapf(err, "checking for books with a duplicate label %s", b.Label) @@ -231,18 +281,18 @@ func mergeBook(tx *infra.DB, b client.SyncFragBook, mode int) error { return errors.Wrap(err, "getting a new book label for conflict resolution") } - if _, err := tx.Exec("UPDATE books SET label = ?, dirty = ? WHERE label = ?", newLabel, true, b.Label); err != nil { + if _, err := tx.Exec("UPDATE books SET label = ?, dirty = ? WHERE label = ? AND uuid != ?", newLabel, true, b.Label, b.UUID); err != nil { return errors.Wrap(err, "resolving duplicate book label") } } if mode == modeInsert { - book := core.NewBook(b.UUID, b.Label, b.USN, false, false) + book := database.NewBook(b.UUID, b.Label, b.USN, false, false) if err := book.Insert(tx); err != nil { return errors.Wrapf(err, "inserting note with uuid %s", b.UUID) } } else if mode == modeUpdate { - // TODO: if the client copy is dirty, perform field-by-field merge and report conflict instead of overwriting + // The state from the server overwrites the local state. In other words, the server change always wins. if _, err := tx.Exec("UPDATE books SET usn = ?, uuid = ?, label = ?, deleted = ? WHERE uuid = ?", b.USN, b.UUID, b.Label, b.Deleted, b.UUID); err != nil { return errors.Wrapf(err, "updating local book %s", b.UUID) @@ -252,7 +302,7 @@ func mergeBook(tx *infra.DB, b client.SyncFragBook, mode int) error { return nil } -func stepSyncBook(tx *infra.DB, b client.SyncFragBook) error { +func stepSyncBook(tx *database.DB, b client.SyncFragBook) error { var localUSN int var dirty bool err := tx.QueryRow("SELECT usn, dirty FROM books WHERE uuid = ?", b.UUID).Scan(&localUSN, &dirty) @@ -276,7 +326,7 @@ func stepSyncBook(tx *infra.DB, b client.SyncFragBook) error { return nil } -func mergeNote(tx *infra.DB, serverNote client.SyncFragNote, localNote core.Note) error { +func mergeNote(tx *database.DB, serverNote client.SyncFragNote, localNote database.Note) error { var bookDeleted bool err := tx.QueryRow("SELECT deleted FROM books WHERE uuid = ?", localNote.BookUUID).Scan(&bookDeleted) if err != nil { @@ -288,36 +338,40 @@ func mergeNote(tx *infra.DB, serverNote client.SyncFragNote, localNote core.Note return nil } - // if the local copy is deleted, and the it was edited on the server, override with server values and mark it not dirty. + // if the local copy is deleted, and it was edited on the server, override with server values and mark it not dirty. if localNote.Deleted { - if _, err := tx.Exec("UPDATE notes SET usn = ?, book_uuid = ?, body = ?, edited_on = ?, deleted = ?, public = ?, dirty = ? WHERE uuid = ?", - serverNote.USN, serverNote.BookUUID, serverNote.Body, serverNote.EditedOn, serverNote.Deleted, serverNote.Public, false, serverNote.UUID); err != nil { + if _, err := tx.Exec("UPDATE notes SET usn = ?, book_uuid = ?, body = ?, edited_on = ?, deleted = ?, dirty = ? WHERE uuid = ?", + serverNote.USN, serverNote.BookUUID, serverNote.Body, serverNote.EditedOn, serverNote.Deleted, false, serverNote.UUID); err != nil { return errors.Wrapf(err, "updating local note %s", serverNote.UUID) } return nil } - // TODO: if the client copy is dirty, perform field-by-field merge and report conflict instead of overwriting - if _, err := tx.Exec("UPDATE notes SET usn = ?, book_uuid = ?, body = ?, edited_on = ?, deleted = ?, public = ? WHERE uuid = ?", - serverNote.USN, serverNote.BookUUID, serverNote.Body, serverNote.EditedOn, serverNote.Deleted, serverNote.Public, serverNote.UUID); err != nil { + mr, err := mergeNoteFields(tx, localNote, serverNote) + if err != nil { + return errors.Wrapf(err, "reporting note conflict for note %s", localNote.UUID) + } + + if _, err := tx.Exec("UPDATE notes SET usn = ?, book_uuid = ?, body = ?, edited_on = ?, deleted = ? WHERE uuid = ?", + serverNote.USN, mr.bookUUID, mr.body, mr.editedOn, serverNote.Deleted, serverNote.UUID); err != nil { return errors.Wrapf(err, "updating local note %s", serverNote.UUID) } return nil } -func stepSyncNote(tx *infra.DB, n client.SyncFragNote) error { - var localNote core.Note - err := tx.QueryRow("SELECT usn, book_uuid, dirty, deleted FROM notes WHERE uuid = ?", n.UUID). - Scan(&localNote.USN, &localNote.BookUUID, &localNote.Dirty, &localNote.Deleted) +func stepSyncNote(tx *database.DB, n client.SyncFragNote) error { + var localNote database.Note + err := tx.QueryRow("SELECT body, usn, book_uuid, dirty, deleted FROM notes WHERE uuid = ?", n.UUID). + Scan(&localNote.Body, &localNote.USN, &localNote.BookUUID, &localNote.Dirty, &localNote.Deleted) if err != nil && err != sql.ErrNoRows { return errors.Wrapf(err, "getting local note %s", n.UUID) } // if note exists in the server and does not exist in the client, insert the note. if err == sql.ErrNoRows { - note := core.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, false) + note := database.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, false) if err := note.Insert(tx); err != nil { return errors.Wrapf(err, "inserting note with uuid %s", n.UUID) @@ -331,17 +385,17 @@ func stepSyncNote(tx *infra.DB, n client.SyncFragNote) error { return nil } -func fullSyncNote(tx *infra.DB, n client.SyncFragNote) error { - var localNote core.Note - err := tx.QueryRow("SELECT usn,book_uuid, dirty, deleted FROM notes WHERE uuid = ?", n.UUID). - Scan(&localNote.USN, &localNote.BookUUID, &localNote.Dirty, &localNote.Deleted) +func fullSyncNote(tx *database.DB, n client.SyncFragNote) error { + var localNote database.Note + err := tx.QueryRow("SELECT body, usn, book_uuid, dirty, deleted FROM notes WHERE uuid = ?", n.UUID). + Scan(&localNote.Body, &localNote.USN, &localNote.BookUUID, &localNote.Dirty, &localNote.Deleted) if err != nil && err != sql.ErrNoRows { return errors.Wrapf(err, "getting local note %s", n.UUID) } // if note exists in the server and does not exist in the client, insert the note. if err == sql.ErrNoRows { - note := core.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, false) + note := database.NewNote(n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, false) if err := note.Insert(tx); err != nil { return errors.Wrapf(err, "inserting note with uuid %s", n.UUID) @@ -355,7 +409,7 @@ func fullSyncNote(tx *infra.DB, n client.SyncFragNote) error { return nil } -func syncDeleteNote(tx *infra.DB, noteUUID string) error { +func syncDeleteNote(tx *database.DB, noteUUID string) error { var localUSN int var dirty bool err := tx.QueryRow("SELECT usn, dirty FROM notes WHERE uuid = ?", noteUUID).Scan(&localUSN, &dirty) @@ -380,7 +434,7 @@ func syncDeleteNote(tx *infra.DB, noteUUID string) error { } // checkNotesPristine checks that none of the notes in the given book are dirty -func checkNotesPristine(tx *infra.DB, bookUUID string) (bool, error) { +func checkNotesPristine(tx *database.DB, bookUUID string) (bool, error) { var count int if err := tx.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ? AND dirty = ?", bookUUID, true).Scan(&count); err != nil { return false, errors.Wrapf(err, "counting notes that are dirty in book %s", bookUUID) @@ -393,7 +447,7 @@ func checkNotesPristine(tx *infra.DB, bookUUID string) (bool, error) { return true, nil } -func syncDeleteBook(tx *infra.DB, bookUUID string) error { +func syncDeleteBook(tx *database.DB, bookUUID string) error { var localUSN int var dirty bool err := tx.QueryRow("SELECT usn, dirty FROM books WHERE uuid = ?", bookUUID).Scan(&localUSN, &dirty) @@ -439,7 +493,7 @@ func syncDeleteBook(tx *infra.DB, bookUUID string) error { return nil } -func fullSyncBook(tx *infra.DB, b client.SyncFragBook) error { +func fullSyncBook(tx *database.DB, b client.SyncFragBook) error { var localUSN int var dirty bool err := tx.QueryRow("SELECT usn, dirty FROM books WHERE uuid = ?", b.UUID).Scan(&localUSN, &dirty) @@ -491,7 +545,7 @@ func checkBookInList(uuid string, list *syncList) bool { // judging by the full list of resources in the server. Concretely, the only acceptable // situation in which a local note is not present in the server is if it is new and has not been // uploaded (i.e. dirty and usn is 0). Otherwise, it is a result of some kind of error and should be cleaned. -func cleanLocalNotes(tx *infra.DB, fullList *syncList) error { +func cleanLocalNotes(tx *database.DB, fullList *syncList) error { rows, err := tx.Query("SELECT uuid, usn, dirty FROM notes") if err != nil { return errors.Wrap(err, "getting local notes") @@ -499,7 +553,7 @@ func cleanLocalNotes(tx *infra.DB, fullList *syncList) error { defer rows.Close() for rows.Next() { - var note core.Note + var note database.Note if err := rows.Scan(¬e.UUID, ¬e.USN, ¬e.Dirty); err != nil { return errors.Wrap(err, "scanning a row for local note") } @@ -517,7 +571,7 @@ func cleanLocalNotes(tx *infra.DB, fullList *syncList) error { } // cleanLocalBooks deletes from the local database any books that are in invalid state -func cleanLocalBooks(tx *infra.DB, fullList *syncList) error { +func cleanLocalBooks(tx *database.DB, fullList *syncList) error { rows, err := tx.Query("SELECT uuid, usn, dirty FROM books") if err != nil { return errors.Wrap(err, "getting local books") @@ -525,7 +579,7 @@ func cleanLocalBooks(tx *infra.DB, fullList *syncList) error { defer rows.Close() for rows.Next() { - var book core.Book + var book database.Book if err := rows.Scan(&book.UUID, &book.USN, &book.Dirty); err != nil { return errors.Wrap(err, "scanning a row for local book") } @@ -542,10 +596,12 @@ func cleanLocalBooks(tx *infra.DB, fullList *syncList) error { return nil } -func fullSync(ctx infra.DnoteCtx, tx *infra.DB) error { +func fullSync(ctx context.DnoteCtx, tx *database.DB) error { log.Debug("performing a full sync\n") log.Info("resolving delta.") + log.DebugNewline() + list, err := getSyncList(ctx, 0) if err != nil { return errors.Wrap(err, "getting sync list") @@ -553,6 +609,8 @@ func fullSync(ctx infra.DnoteCtx, tx *infra.DB) error { fmt.Printf(" (total %d).", list.getLength()) + log.DebugNewline() + // clean resources that are in erroneous states if err := cleanLocalNotes(tx, &list); err != nil { return errors.Wrap(err, "cleaning up local notes") @@ -583,7 +641,7 @@ func fullSync(ctx infra.DnoteCtx, tx *infra.DB) error { } } - err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN) + err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN, list.UserMaxUSN) if err != nil { return errors.Wrap(err, "saving sync state") } @@ -593,11 +651,13 @@ func fullSync(ctx infra.DnoteCtx, tx *infra.DB) error { return nil } -func stepSync(ctx infra.DnoteCtx, tx *infra.DB, afterUSN int) error { +func stepSync(ctx context.DnoteCtx, tx *database.DB, afterUSN int) error { log.Debug("performing a step sync\n") log.Info("resolving delta.") + log.DebugNewline() + list, err := getSyncList(ctx, afterUSN) if err != nil { return errors.Wrap(err, "getting sync list") @@ -627,7 +687,7 @@ func stepSync(ctx infra.DnoteCtx, tx *infra.DB, afterUSN int) error { } } - err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN) + err = saveSyncState(tx, list.MaxCurrentTime, list.MaxUSN, list.UserMaxUSN) if err != nil { return errors.Wrap(err, "saving sync state") } @@ -637,7 +697,21 @@ func stepSync(ctx infra.DnoteCtx, tx *infra.DB, afterUSN int) error { return nil } -func sendBooks(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { +// isConflictError checks if an error is a 409 Conflict error from the server +func isConflictError(err error) bool { + if err == nil { + return false + } + + var httpErr *client.HTTPError + if errors.As(err, &httpErr) { + return httpErr.IsConflict() + } + + return false +} + +func sendBooks(ctx context.DnoteCtx, tx *database.DB) (bool, error) { isBehind := false rows, err := tx.Query("SELECT uuid, label, usn, deleted FROM books WHERE dirty") @@ -647,7 +721,7 @@ func sendBooks(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { defer rows.Close() for rows.Next() { - var book core.Book + var book database.Book if err = rows.Scan(&book.UUID, &book.Label, &book.USN, &book.Deleted); err != nil { return isBehind, errors.Wrap(err, "scanning a syncable book") @@ -669,7 +743,9 @@ func sendBooks(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { } else { resp, err := client.CreateBook(ctx, book.Label) if err != nil { - return isBehind, errors.Wrap(err, "creating a book") + log.Debug("error creating book (will retry after stepSync): %v\n", err) + isBehind = true + continue } _, err = tx.Exec("UPDATE notes SET book_uuid = ? WHERE book_uuid = ?", resp.Book.UUID, book.UUID) @@ -741,23 +817,105 @@ func sendBooks(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { return isBehind, nil } -func sendNotes(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { +// findOrphanedNotes returns a list of all orphaned notes +func findOrphanedNotes(db *database.DB) (int, []struct{ noteUUID, bookUUID string }, error) { + var orphanCount int + err := db.QueryRow(` + SELECT COUNT(*) FROM notes n + WHERE NOT EXISTS ( + SELECT 1 FROM books b + WHERE b.uuid = n.book_uuid + AND NOT b.deleted + ) + `).Scan(&orphanCount) + if err != nil { + return 0, nil, err + } + + if orphanCount == 0 { + return 0, nil, nil + } + + rows, err := db.Query(` + SELECT n.uuid, n.book_uuid + FROM notes n + WHERE NOT EXISTS ( + SELECT 1 FROM books b + WHERE b.uuid = n.book_uuid + AND NOT b.deleted + ) + `) + if err != nil { + return orphanCount, nil, err + } + defer rows.Close() + + var orphans []struct{ noteUUID, bookUUID string } + for rows.Next() { + var noteUUID, bookUUID string + if err := rows.Scan(¬eUUID, &bookUUID); err != nil { + continue + } + orphans = append(orphans, struct{ noteUUID, bookUUID string }{noteUUID, bookUUID}) + } + + return orphanCount, orphans, nil +} + +func warnOrphanedNotes(tx *database.DB) { + count, orphans, err := findOrphanedNotes(tx) + if err != nil { + log.Debug("error checking orphaned notes: %v\n", err) + return + } + + if count == 0 { + return + } + + log.Debug("Found %d orphaned notes (book doesn't exist locally):\n", count) + for _, o := range orphans { + log.Debug("note %s (book %s)\n", o.noteUUID, o.bookUUID) + } +} + +// checkPostSyncIntegrity checks for data integrity issues after sync and warns the user +func checkPostSyncIntegrity(db *database.DB) { + count, orphans, err := findOrphanedNotes(db) + if err != nil { + log.Debug("error checking orphaned notes: %v\n", err) + return + } + + if count == 0 { + return + } + + log.Warnf("Found %d orphaned notes (referencing non-existent or deleted books):\n", count) + for _, o := range orphans { + log.Plainf(" - note %s (missing book: %s)\n", o.noteUUID, o.bookUUID) + } +} + +func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) { isBehind := false - rows, err := tx.Query("SELECT uuid, book_uuid, body, public, deleted, usn, added_on FROM notes WHERE dirty") + warnOrphanedNotes(tx) + + rows, err := tx.Query("SELECT uuid, book_uuid, body, deleted, usn, added_on FROM notes WHERE dirty") if err != nil { return isBehind, errors.Wrap(err, "getting syncable notes") } defer rows.Close() for rows.Next() { - var note core.Note + var note database.Note - if err = rows.Scan(¬e.UUID, ¬e.BookUUID, ¬e.Body, ¬e.Public, ¬e.Deleted, ¬e.USN, ¬e.AddedOn); err != nil { + if err = rows.Scan(¬e.UUID, ¬e.BookUUID, ¬e.Body, ¬e.Deleted, ¬e.USN, ¬e.AddedOn); err != nil { return isBehind, errors.Wrap(err, "scanning a syncable note") } - log.Debug("sending note %s\n", note.UUID) + log.Debug("sending note %s (book: %s)\n", note.UUID, note.BookUUID) var respUSN int @@ -774,7 +932,9 @@ func sendNotes(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { } else { resp, err := client.CreateNote(ctx, note.BookUUID, note.Body) if err != nil { - return isBehind, errors.Wrap(err, "creating a note") + log.Debug("failed to create note %s (book: %s): %v\n", note.UUID, note.BookUUID, err) + isBehind = true + continue } note.Dirty = false @@ -805,7 +965,7 @@ func sendNotes(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { respUSN = resp.Result.USN } else { - resp, err := client.UpdateNote(ctx, note.UUID, note.BookUUID, note.Body, note.Public) + resp, err := client.UpdateNote(ctx, note.UUID, note.BookUUID, note.Body) if err != nil { return isBehind, errors.Wrap(err, "updating a note") } @@ -841,7 +1001,7 @@ func sendNotes(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { return isBehind, nil } -func sendChanges(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { +func sendChanges(ctx context.DnoteCtx, tx *database.DB) (bool, error) { log.Info("sending changes.") var delta int @@ -849,6 +1009,8 @@ func sendChanges(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { fmt.Printf(" (total %d).", delta) + log.DebugNewline() + behind1, err := sendBooks(ctx, tx) if err != nil { return behind1, errors.Wrap(err, "sending books") @@ -866,26 +1028,40 @@ func sendChanges(ctx infra.DnoteCtx, tx *infra.DB) (bool, error) { return isBehind, nil } -func updateLastMaxUSN(tx *infra.DB, val int) error { - if err := core.UpdateSystem(tx, infra.SystemLastMaxUSN, val); err != nil { - return errors.Wrapf(err, "updating %s", infra.SystemLastMaxUSN) +func updateLastMaxUSN(tx *database.DB, val int) error { + if err := database.UpdateSystem(tx, consts.SystemLastMaxUSN, val); err != nil { + return errors.Wrapf(err, "updating %s", consts.SystemLastMaxUSN) } return nil } -func updateLastSyncAt(tx *infra.DB, val int64) error { - if err := core.UpdateSystem(tx, infra.SystemLastSyncAt, val); err != nil { - return errors.Wrapf(err, "updating %s", infra.SystemLastSyncAt) +func updateLastSyncAt(tx *database.DB, val int64) error { + if err := database.UpdateSystem(tx, consts.SystemLastSyncAt, val); err != nil { + return errors.Wrapf(err, "updating %s", consts.SystemLastSyncAt) } return nil } -func saveSyncState(tx *infra.DB, serverTime int64, serverMaxUSN int) error { - if err := updateLastMaxUSN(tx, serverMaxUSN); err != nil { - return errors.Wrap(err, "updating last max usn") +func saveSyncState(tx *database.DB, serverTime int64, serverMaxUSN int, userMaxUSN int) error { + // Handle last_max_usn update based on server state: + // - If serverMaxUSN > 0: we got data, update to serverMaxUSN + // - If serverMaxUSN == 0 && userMaxUSN > 0: empty fragment (caught up), preserve existing + // - If serverMaxUSN == 0 && userMaxUSN == 0: empty server, reset to 0 + if serverMaxUSN > 0 { + if err := updateLastMaxUSN(tx, serverMaxUSN); err != nil { + return errors.Wrap(err, "updating last max usn") + } + } else if userMaxUSN == 0 { + // Server is empty, reset to 0 + if err := updateLastMaxUSN(tx, 0); err != nil { + return errors.Wrap(err, "updating last max usn") + } } + // else: empty fragment but server has data, preserve existing last_max_usn + + // Always update last_sync_at (we did communicate with server) if err := updateLastSyncAt(tx, serverTime); err != nil { return errors.Wrap(err, "updating last sync at") } @@ -893,9 +1069,34 @@ func saveSyncState(tx *infra.DB, serverTime int64, serverMaxUSN int) error { return nil } -func newRun(ctx infra.DnoteCtx) core.RunEFunc { +// prepareEmptyServerSync marks all local books and notes as dirty when syncing to an empty server. +// This is typically used when switching to a new empty server but wanting to upload existing local data. +// Returns true if preparation was done, false otherwise. +func prepareEmptyServerSync(tx *database.DB) error { + // Mark all books and notes as dirty and reset USN to 0 + if _, err := tx.Exec("UPDATE books SET usn = 0, dirty = 1 WHERE deleted = 0"); err != nil { + return errors.Wrap(err, "marking books as dirty") + } + if _, err := tx.Exec("UPDATE notes SET usn = 0, dirty = 1 WHERE deleted = 0"); err != nil { + return errors.Wrap(err, "marking notes as dirty") + } + + // Reset lastMaxUSN to 0 to match the server + if err := updateLastMaxUSN(tx, 0); err != nil { + return errors.Wrap(err, "resetting last max usn") + } + + return nil +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { return func(cmd *cobra.Command, args []string) error { - if ctx.SessionKey == "" || ctx.CipherKey == nil { + // Override APIEndpoint if flag was provided + if apiEndpointFlag != "" { + ctx.APIEndpoint = apiEndpointFlag + } + + if ctx.SessionKey == "" { return errors.New("not logged in") } @@ -923,6 +1124,74 @@ func newRun(ctx infra.DnoteCtx) core.RunEFunc { log.Debug("lastSyncAt: %d, lastMaxUSN: %d, syncState: %+v\n", lastSyncAt, lastMaxUSN, syncState) + // Handle a case where server has MaxUSN=0 but local has data (server switch) + var bookCount, noteCount int + if err := tx.QueryRow("SELECT count(*) FROM books WHERE deleted = 0").Scan(&bookCount); err != nil { + return errors.Wrap(err, "counting local books") + } + if err := tx.QueryRow("SELECT count(*) FROM notes WHERE deleted = 0").Scan(¬eCount); err != nil { + return errors.Wrap(err, "counting local notes") + } + + // If a client has previously synced (lastMaxUSN > 0) but the server was never synced to (MaxUSN = 0), + // and the client has undeleted books or notes, allow to upload all data to the server. + // The client might have switched servers or the server might need to be restored for any reasons. + if syncState.MaxUSN == 0 && lastMaxUSN > 0 && (bookCount > 0 || noteCount > 0) { + log.Debug("empty server detected: server.MaxUSN=%d, local.MaxUSN=%d, books=%d, notes=%d\n", + syncState.MaxUSN, lastMaxUSN, bookCount, noteCount) + + log.Warnf("The server is empty but you have local data. Maybe you switched servers?\n") + log.Debug("server state: MaxUSN = 0 (empty)\n") + log.Debug("local state: %d books, %d notes (MaxUSN = %d)\n", bookCount, noteCount, lastMaxUSN) + + confirmed, err := ui.Confirm(fmt.Sprintf("Upload %d books and %d notes to the server?", bookCount, noteCount), false) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "getting user confirmation") + } + + if !confirmed { + tx.Rollback() + return errors.New("sync cancelled by user") + } + + fmt.Println() // Add newline after confirmation. + + if err := prepareEmptyServerSync(tx); err != nil { + return errors.Wrap(err, "preparing for empty server sync") + } + + // Re-fetch lastMaxUSN after prepareEmptyServerSync + lastMaxUSN, err = getLastMaxUSN(tx) + if err != nil { + return errors.Wrap(err, "getting the last max_usn after prepare") + } + + log.Debug("prepared empty server sync: marked %d books and %d notes as dirty\n", bookCount, noteCount) + } + + // If full sync will be triggered by FullSyncBefore (not manual --full flag), + // and client has more data than server, prepare local data for upload to avoid orphaning notes. + // The lastMaxUSN > syncState.MaxUSN check prevents duplicate uploads when switching + // back to a server that already has our data. + if !isFullSync && lastSyncAt < syncState.FullSyncBefore && lastMaxUSN > syncState.MaxUSN { + log.Debug("full sync triggered by FullSyncBefore: preparing local data for upload\n") + log.Debug("server.FullSyncBefore=%d, local.lastSyncAt=%d, local.MaxUSN=%d, server.MaxUSN=%d, books=%d, notes=%d\n", + syncState.FullSyncBefore, lastSyncAt, lastMaxUSN, syncState.MaxUSN, bookCount, noteCount) + + if err := prepareEmptyServerSync(tx); err != nil { + return errors.Wrap(err, "preparing local data for full sync") + } + + // Re-fetch lastMaxUSN after prepareEmptyServerSync + lastMaxUSN, err = getLastMaxUSN(tx) + if err != nil { + return errors.Wrap(err, "getting the last max_usn after prepare") + } + + log.Debug("prepared for full sync: marked %d books and %d notes as dirty\n", bookCount, noteCount) + } + var syncErr error if isFullSync || lastSyncAt < syncState.FullSyncBefore { syncErr = fullSync(ctx, tx) @@ -961,13 +1230,25 @@ func newRun(ctx infra.DnoteCtx) core.RunEFunc { tx.Rollback() return errors.Wrap(err, "performing the follow-up step sync") } + + // After syncing server changes (which resolves conflicts), send local changes again + // This uploads books/notes that were skipped due to 409 conflicts + _, err = sendChanges(ctx, tx) + if err != nil { + tx.Rollback() + return errors.Wrap(err, "sending changes after conflict resolution") + } } - tx.Commit() + if err := tx.Commit(); err != nil { + return errors.Wrap(err, "committing transaction") + } log.Success("success\n") - if err := core.CheckUpdate(ctx); err != nil { + checkPostSyncIntegrity(ctx.DB) + + if err := upgrade.Check(ctx); err != nil { log.Error(errors.Wrap(err, "automatically checking updates").Error()) } diff --git a/pkg/cli/cmd/sync/sync_test.go b/pkg/cli/cmd/sync/sync_test.go new file mode 100644 index 00000000..ae3b94ff --- /dev/null +++ b/pkg/cli/cmd/sync/sync_test.go @@ -0,0 +1,3287 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "sort" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/pkg/errors" +) + +func TestProcessFragments(t *testing.T) { + fragments := []client.SyncFragment{ + { + FragMaxUSN: 10, + UserMaxUSN: 10, + CurrentTime: 1550436136, + Notes: []client.SyncFragNote{ + { + UUID: "45546de0-40ed-45cf-9bfc-62ce729a7d3d", + Body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Donec ac libero efficitur, posuere dui non, egestas lectus.\n Aliquam urna ligula, sagittis eu volutpat vel, consequat et augue.\n\n Ut mi urna, dignissim a ex eget, venenatis accumsan sem. Praesent facilisis, ligula hendrerit auctor varius, mauris metus hendrerit dolor, sit amet pulvinar.", + }, + { + UUID: "a25a5336-afe9-46c4-b881-acab911c0bc3", + Body: "foo bar baz quz\nqux", + }, + }, + Books: []client.SyncFragBook{ + { + UUID: "e8ac6f25-d95b-435a-9fae-094f7506a5ac", + Label: "foo", + }, + { + UUID: "05fd8b95-ddcd-4071-9380-4358ffb8a436", + Label: "foo-bar-baz-1000", + }, + }, + ExpungedNotes: []string{}, + ExpungedBooks: []string{}, + }, + } + + // exec + sl, err := processFragments(fragments) + if err != nil { + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + expected := syncList{ + Notes: map[string]client.SyncFragNote{ + "45546de0-40ed-45cf-9bfc-62ce729a7d3d": { + UUID: "45546de0-40ed-45cf-9bfc-62ce729a7d3d", + Body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n Donec ac libero efficitur, posuere dui non, egestas lectus.\n Aliquam urna ligula, sagittis eu volutpat vel, consequat et augue.\n\n Ut mi urna, dignissim a ex eget, venenatis accumsan sem. Praesent facilisis, ligula hendrerit auctor varius, mauris metus hendrerit dolor, sit amet pulvinar.", + }, + "a25a5336-afe9-46c4-b881-acab911c0bc3": { + UUID: "a25a5336-afe9-46c4-b881-acab911c0bc3", + Body: "foo bar baz quz\nqux", + }, + }, + Books: map[string]client.SyncFragBook{ + "e8ac6f25-d95b-435a-9fae-094f7506a5ac": { + UUID: "e8ac6f25-d95b-435a-9fae-094f7506a5ac", + Label: "foo", + }, + "05fd8b95-ddcd-4071-9380-4358ffb8a436": { + UUID: "05fd8b95-ddcd-4071-9380-4358ffb8a436", + Label: "foo-bar-baz-1000", + }, + }, + ExpungedNotes: map[string]bool{}, + ExpungedBooks: map[string]bool{}, + MaxUSN: 10, + UserMaxUSN: 10, + MaxCurrentTime: 1550436136, + } + + // test + assert.DeepEqual(t, sl, expected, "syncList mismatch") +} + +func TestGetLastSyncAt(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + database.MustExec(t, "setting up last_sync_at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, 1541108743) + + // exec + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + got, err := getLastSyncAt(tx) + if err != nil { + t.Fatal(errors.Wrap(err, "getting last_sync_at").Error()) + } + + tx.Commit() + + // test + assert.Equal(t, got, 1541108743, "last_sync_at mismatch") +} + +func TestGetLastMaxUSN(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + database.MustExec(t, "setting up last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 20001) + + // exec + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + got, err := getLastMaxUSN(tx) + if err != nil { + t.Fatal(errors.Wrap(err, "getting last_max_usn").Error()) + } + + tx.Commit() + + // test + assert.Equal(t, got, 20001, "last_max_usn mismatch") +} + +func TestResolveLabel(t *testing.T) { + testCases := []struct { + input string + expected string + }{ + { + input: "js", + expected: "js_2", + }, + { + input: "css", + expected: "css_3", + }, + { + input: "linux", + expected: "linux_4", + }, + { + input: "cool_ideas", + expected: "cool_ideas_2", + }, + } + + for idx, tc := range testCases { + func() { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b1-uuid", "js") + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b2-uuid", "css_2") + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b3-uuid", "linux_(1)") + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b4-uuid", "linux_2") + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b5-uuid", "linux_3") + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b6-uuid", "cool_ideas") + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + got, err := resolveLabel(tx, tc.input) + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + tx.Rollback() + + assert.Equal(t, got, tc.expected, fmt.Sprintf("output mismatch for test case %d", idx)) + }() + } +} + +func TestSyncDeleteNote(t *testing.T) { + t.Run("exists on server only", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := syncDeleteNote(tx, "nonexistent-note-uuid"); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 0, "book count mismatch") + }) + + t.Run("local copy is dirty", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) + database.MustExec(t, "inserting n2 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 11, "n2 body", 1541108743, false, true) + + var n1 database.Note + database.MustScan(t, "getting n1 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + var n2 database.Note + database.MustScan(t, "getting n2 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", "n2-uuid"), + &n2.UUID, &n2.BookUUID, &n2.USN, &n2.AddedOn, &n2.EditedOn, &n2.Body, &n2.Deleted, &n2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) + } + + if err := syncDeleteNote(tx, "n1-uuid"); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + // do not delete note if local copy is dirty + assert.Equalf(t, noteCount, 2, "note count mismatch for test case") + assert.Equalf(t, bookCount, 1, "book count mismatch for test case") + + var n1Record database.Note + database.MustScan(t, "getting n1 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n1.UUID), + &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.Body, &n1Record.Deleted, &n1Record.Dirty) + var n2Record database.Note + database.MustScan(t, "getting n2 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.USN, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.Body, &n2Record.Deleted, &n2Record.Dirty) + + assert.Equal(t, n1Record.UUID, n1.UUID, "n1 UUID mismatch for test case") + assert.Equal(t, n1Record.BookUUID, n1.BookUUID, "n1 BookUUID mismatch for test case") + assert.Equal(t, n1Record.USN, n1.USN, "n1 USN mismatch for test case") + assert.Equal(t, n1Record.AddedOn, n1.AddedOn, "n1 AddedOn mismatch for test case") + assert.Equal(t, n1Record.EditedOn, n1.EditedOn, "n1 EditedOn mismatch for test case") + assert.Equal(t, n1Record.Body, n1.Body, "n1 Body mismatch for test case") + assert.Equal(t, n1Record.Deleted, n1.Deleted, "n1 Deleted mismatch for test case") + assert.Equal(t, n1Record.Dirty, n1.Dirty, "n1 Dirty mismatch for test case") + + assert.Equal(t, n2Record.UUID, n2.UUID, "n2 UUID mismatch for test case") + assert.Equal(t, n2Record.BookUUID, n2.BookUUID, "n2 BookUUID mismatch for test case") + assert.Equal(t, n2Record.USN, n2.USN, "n2 USN mismatch for test case") + assert.Equal(t, n2Record.AddedOn, n2.AddedOn, "n2 AddedOn mismatch for test case") + assert.Equal(t, n2Record.EditedOn, n2.EditedOn, "n2 EditedOn mismatch for test case") + assert.Equal(t, n2Record.Body, n2.Body, "n2 Body mismatch for test case") + assert.Equal(t, n2Record.Deleted, n2.Deleted, "n2 Deleted mismatch for test case") + assert.Equal(t, n2Record.Dirty, n2.Dirty, "n2 Dirty mismatch for test case") + }) + + t.Run("local copy is not dirty", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) + database.MustExec(t, "inserting n2 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 11, "n2 body", 1541108743, false, false) + + var n1 database.Note + database.MustScan(t, "getting n1 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + var n2 database.Note + database.MustScan(t, "getting n2 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", "n2-uuid"), + &n2.UUID, &n2.BookUUID, &n2.USN, &n2.AddedOn, &n2.EditedOn, &n2.Body, &n2.Deleted, &n2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) + } + + if err := syncDeleteNote(tx, "n1-uuid"); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, "note count mismatch for test case") + assert.Equalf(t, bookCount, 1, "book count mismatch for test case") + + var n2Record database.Note + database.MustScan(t, "getting n2 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.USN, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.Body, &n2Record.Deleted, &n2Record.Dirty) + + assert.Equal(t, n2Record.UUID, n2.UUID, "n2 UUID mismatch for test case") + assert.Equal(t, n2Record.BookUUID, n2.BookUUID, "n2 BookUUID mismatch for test case") + assert.Equal(t, n2Record.USN, n2.USN, "n2 USN mismatch for test case") + assert.Equal(t, n2Record.AddedOn, n2.AddedOn, "n2 AddedOn mismatch for test case") + assert.Equal(t, n2Record.EditedOn, n2.EditedOn, "n2 EditedOn mismatch for test case") + assert.Equal(t, n2Record.Body, n2.Body, "n2 Body mismatch for test case") + assert.Equal(t, n2Record.Deleted, n2.Deleted, "n2 Deleted mismatch for test case") + assert.Equal(t, n2Record.Dirty, n2.Dirty, "n2 Dirty mismatch for test case") + }) +} + +func TestSyncDeleteBook(t *testing.T) { + t.Run("exists on server only", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", "b1-uuid", "b1-label") + + var b1 database.Book + database.MustScan(t, "getting b1 for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1.UUID, &b1.Label, &b1.USN, &b1.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := syncDeleteBook(tx, "nonexistent-book-uuid"); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 1, "book count mismatch") + + var b1Record database.Book + database.MustScan(t, "getting b1 for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch for test case") + assert.Equal(t, b1Record.Label, b1.Label, "b1 Label mismatch for test case") + assert.Equal(t, b1Record.USN, b1.USN, "b1 USN mismatch for test case") + assert.Equal(t, b1Record.Dirty, b1.Dirty, "b1 Dirty mismatch for test case") + }) + + t.Run("local copy is dirty", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b1UUID, "b1-label", 12, true) + database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) + + var b1 database.Book + database.MustScan(t, "getting b1 for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), + &b1.UUID, &b1.Label, &b1.USN, &b1.Dirty) + var n1 database.Note + database.MustScan(t, "getting n1 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) + } + + if err := syncDeleteBook(tx, b1UUID); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + // do not delete note if local copy is dirty + assert.Equalf(t, noteCount, 1, "note count mismatch for test case") + assert.Equalf(t, bookCount, 1, "book count mismatch for test case") + + var b1Record database.Book + database.MustScan(t, "getting b1Record for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + var n1Record database.Note + database.MustScan(t, "getting n1 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n1.UUID), + &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.Body, &n1Record.Deleted, &n1Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch for test case") + assert.Equal(t, b1Record.Label, b1.Label, "b1 Label mismatch for test case") + assert.Equal(t, b1Record.USN, b1.USN, "b1 USN mismatch for test case") + assert.Equal(t, b1Record.Dirty, b1.Dirty, "b1 Dirty mismatch for test case") + + assert.Equal(t, n1Record.UUID, n1.UUID, "n1 UUID mismatch for test case") + assert.Equal(t, n1Record.BookUUID, n1.BookUUID, "n1 BookUUID mismatch for test case") + assert.Equal(t, n1Record.USN, n1.USN, "n1 USN mismatch for test case") + assert.Equal(t, n1Record.AddedOn, n1.AddedOn, "n1 AddedOn mismatch for test case") + assert.Equal(t, n1Record.EditedOn, n1.EditedOn, "n1 EditedOn mismatch for test case") + assert.Equal(t, n1Record.Body, n1.Body, "n1 Body mismatch for test case") + assert.Equal(t, n1Record.Deleted, n1.Deleted, "n1 Deleted mismatch for test case") + assert.Equal(t, n1Record.Dirty, n1.Dirty, "n1 Dirty mismatch for test case") + }) + + t.Run("local copy is not dirty", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + b2UUID := testutils.MustGenerateUUID(t) + + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) + database.MustExec(t, "inserting b2 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") + database.MustExec(t, "inserting n2 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b2UUID, 11, "n2 body", 1541108743, false, false) + + var b2 database.Book + database.MustScan(t, "getting b2 for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b2UUID), + &b2.UUID, &b2.Label, &b2.USN, &b2.Dirty) + var n2 database.Note + database.MustScan(t, "getting n2 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", "n2-uuid"), + &n2.UUID, &n2.BookUUID, &n2.USN, &n2.AddedOn, &n2.EditedOn, &n2.Body, &n2.Deleted, &n2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) + } + + if err := syncDeleteBook(tx, b1UUID); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, "note count mismatch for test case") + assert.Equalf(t, bookCount, 1, "book count mismatch for test case") + + var b2Record database.Book + database.MustScan(t, "getting b2 for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b2UUID), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) + var n2Record database.Note + database.MustScan(t, "getting n2 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.USN, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.Body, &n2Record.Deleted, &n2Record.Dirty) + + assert.Equal(t, b2Record.UUID, b2.UUID, "b2 UUID mismatch for test case") + assert.Equal(t, b2Record.Label, b2.Label, "b2 Label mismatch for test case") + assert.Equal(t, b2Record.USN, b2.USN, "b2 USN mismatch for test case") + assert.Equal(t, b2Record.Dirty, b2.Dirty, "b2 Dirty mismatch for test case") + + assert.Equal(t, n2Record.UUID, n2.UUID, "n2 UUID mismatch for test case") + assert.Equal(t, n2Record.BookUUID, n2.BookUUID, "n2 BookUUID mismatch for test case") + assert.Equal(t, n2Record.USN, n2.USN, "n2 USN mismatch for test case") + assert.Equal(t, n2Record.AddedOn, n2.AddedOn, "n2 AddedOn mismatch for test case") + assert.Equal(t, n2Record.EditedOn, n2.EditedOn, "n2 EditedOn mismatch for test case") + assert.Equal(t, n2Record.Body, n2.Body, "n2 Body mismatch for test case") + assert.Equal(t, n2Record.Deleted, n2.Deleted, "n2 Deleted mismatch for test case") + assert.Equal(t, n2Record.Dirty, n2.Dirty, "n2 Dirty mismatch for test case") + }) + + t.Run("local copy has at least one note that is dirty", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting b1 for test case %d", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + database.MustExec(t, "inserting n1 for test case %d", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction for test case").Error()) + } + + if err := syncDeleteBook(tx, b1UUID); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes for test case", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books for test case", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + assert.Equalf(t, noteCount, 1, "note count mismatch for test case") + assert.Equalf(t, bookCount, 1, "book count mismatch for test case") + + var b1Record database.Book + database.MustScan(t, "getting b1 for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + var n1Record database.Note + database.MustScan(t, "getting n1 for test case", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, body,deleted, dirty FROM notes WHERE uuid = ?", "n1-uuid"), + &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.Body, &n1Record.Deleted, &n1Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1UUID, "b1 UUID mismatch for test case") + assert.Equal(t, b1Record.Label, "b1-label", "b1 Label mismatch for test case") + assert.Equal(t, b1Record.Dirty, true, "b1 Dirty mismatch for test case") + + assert.Equal(t, n1Record.UUID, "n1-uuid", "n1 UUID mismatch for test case") + assert.Equal(t, n1Record.BookUUID, b1UUID, "n1 BookUUID mismatch for test case") + assert.Equal(t, n1Record.USN, 10, "n1 USN mismatch for test case") + assert.Equal(t, n1Record.AddedOn, int64(1541108743), "n1 AddedOn mismatch for test case") + assert.Equal(t, n1Record.Body, "n1 body", "n1 Body mismatch for test case") + assert.Equal(t, n1Record.Deleted, false, "n1 Deleted mismatch for test case") + assert.Equal(t, n1Record.Dirty, true, "n1 Dirty mismatch for test case") + }) +} + +func TestFullSyncNote(t *testing.T) { + t.Run("exists on server only", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + n := client.SyncFragNote{ + UUID: "n1-uuid", + BookUUID: b1UUID, + USN: 128, + AddedOn: 1541232118, + EditedOn: 1541219321, + Body: "n1-body", + Deleted: false, + } + + if err := fullSyncNote(tx, n); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, "note count mismatch") + assert.Equalf(t, bookCount, 1, "book count mismatch") + + var n1 database.Note + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + + assert.Equal(t, n1.UUID, n.UUID, "n1 UUID mismatch") + assert.Equal(t, n1.BookUUID, n.BookUUID, "n1 BookUUID mismatch") + assert.Equal(t, n1.USN, n.USN, "n1 USN mismatch") + assert.Equal(t, n1.AddedOn, n.AddedOn, "n1 AddedOn mismatch") + assert.Equal(t, n1.EditedOn, n.EditedOn, "n1 EditedOn mismatch") + assert.Equal(t, n1.Body, n.Body, "n1 Body mismatch") + assert.Equal(t, n1.Deleted, n.Deleted, "n1 Deleted mismatch") + assert.Equal(t, n1.Dirty, false, "n1 Dirty mismatch") + }) + + t.Run("exists on server and client", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + b2UUID := testutils.MustGenerateUUID(t) + conflictBookUUID := testutils.MustGenerateUUID(t) + + testCases := []struct { + addedOn int64 + clientUSN int + clientEditedOn int64 + clientBody string + clientDeleted bool + clientBookUUID string + clientDirty bool + serverUSN int + serverEditedOn int64 + serverBody string + serverDeleted bool + serverBookUUID string + expectedUSN int + expectedAddedOn int64 + expectedEditedOn int64 + expectedBody string + expectedDeleted bool + expectedBookUUID string + expectedDirty bool + }{ + // server has higher usn and client is dirty + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: `<<<<<<< Local +Moved to the book b1-label +======= +Moved to the book b2-label +>>>>>>> Server + +<<<<<<< Local +n1 body +======= +n1 body edited +>>>>>>> Server +`, + expectedDeleted: false, + expectedBookUUID: conflictBookUUID, + expectedDirty: true, + }, + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b1UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: `<<<<<<< Local +n1 body +======= +n1 body edited +>>>>>>> Server +`, + expectedDeleted: false, + expectedBookUUID: b1UUID, + expectedDirty: true, + }, + // server has higher usn and client deleted locally + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "", + clientDeleted: true, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body server", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body server", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: false, + }, + // server has higher usn and client is not dirty + { + clientDirty: false, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body edited", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: false, + }, + // they're in sync + { + clientDirty: true, + clientUSN: 21, + clientEditedOn: 1541219321, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b2UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: true, + }, + // they have the same usn but client is dirty + // not sure if this is a possible scenario but if it happens, the local copy will + // be uploaded to the server anyway. + { + clientDirty: true, + clientUSN: 21, + clientEditedOn: 1541219320, + clientBody: "n1 body client", + clientDeleted: false, + clientBookUUID: b2UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body server", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219320, + expectedBody: "n1 body client", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + database.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") + database.MustExec(t, fmt.Sprintf("inserting conflitcs book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", conflictBookUUID, "conflicts") + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, tc.clientBookUUID, tc.clientUSN, tc.addedOn, tc.clientEditedOn, tc.clientBody, tc.clientDeleted, tc.clientDirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + // update all fields but uuid and bump usn + n := client.SyncFragNote{ + UUID: n1UUID, + BookUUID: tc.serverBookUUID, + USN: tc.serverUSN, + AddedOn: tc.addedOn, + EditedOn: tc.serverEditedOn, + Body: tc.serverBody, + Deleted: tc.serverDeleted, + } + + if err := fullSyncNote(tx, n); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, fmt.Sprintf("note count mismatch for test case %d", idx)) + assert.Equalf(t, bookCount, 3, fmt.Sprintf("book count mismatch for test case %d", idx)) + + var n1 database.Note + database.MustScan(t, fmt.Sprintf("getting n1 for test case %d", idx), + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + + assert.Equal(t, n1.UUID, n.UUID, fmt.Sprintf("n1 UUID mismatch for test case %d", idx)) + assert.Equal(t, n1.BookUUID, tc.expectedBookUUID, fmt.Sprintf("n1 BookUUID mismatch for test case %d", idx)) + assert.Equal(t, n1.USN, tc.expectedUSN, fmt.Sprintf("n1 USN mismatch for test case %d", idx)) + assert.Equal(t, n1.AddedOn, tc.expectedAddedOn, fmt.Sprintf("n1 AddedOn mismatch for test case %d", idx)) + assert.Equal(t, n1.EditedOn, tc.expectedEditedOn, fmt.Sprintf("n1 EditedOn mismatch for test case %d", idx)) + assert.Equal(t, n1.Body, tc.expectedBody, fmt.Sprintf("n1 Body mismatch for test case %d", idx)) + assert.Equal(t, n1.Deleted, tc.expectedDeleted, fmt.Sprintf("n1 Deleted mismatch for test case %d", idx)) + assert.Equal(t, n1.Dirty, tc.expectedDirty, fmt.Sprintf("n1 Dirty mismatch for test case %d", idx)) + }() + } + }) +} + +func TestFullSyncBook(t *testing.T) { + t.Run("exists on server only", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 555, "b1-label", true, false) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b2UUID := testutils.MustGenerateUUID(t) + b := client.SyncFragBook{ + UUID: b2UUID, + USN: 1, + AddedOn: 1541108743, + Label: "b2-label", + Deleted: false, + } + + if err := fullSyncBook(tx, b); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 2, "book count mismatch") + + var b1, b2 database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), + &b1.UUID, &b1.USN, &b1.Label, &b1.Dirty, &b1.Deleted) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b2UUID), + &b2.UUID, &b2.USN, &b2.Label, &b2.Dirty, &b2.Deleted) + + assert.Equal(t, b1.UUID, b1UUID, "b1 UUID mismatch") + assert.Equal(t, b1.USN, 555, "b1 USN mismatch") + assert.Equal(t, b1.Label, "b1-label", "b1 Label mismatch") + assert.Equal(t, b1.Dirty, true, "b1 Dirty mismatch") + assert.Equal(t, b1.Deleted, false, "b1 Deleted mismatch") + + assert.Equal(t, b2.UUID, b2UUID, "b2 UUID mismatch") + assert.Equal(t, b2.USN, b.USN, "b2 USN mismatch") + assert.Equal(t, b2.Label, b.Label, "b2 Label mismatch") + assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch") + assert.Equal(t, b2.Deleted, b.Deleted, "b2 Deleted mismatch") + }) + + t.Run("exists on server and client", func(t *testing.T) { + testCases := []struct { + clientDirty bool + clientUSN int + clientLabel string + clientDeleted bool + serverUSN int + serverLabel string + serverDeleted bool + expectedUSN int + expectedLabel string + expectedDeleted bool + }{ + // server has higher usn and client is dirty + { + clientDirty: true, + clientUSN: 1, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "b2-label-updated", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "b2-label-updated", + expectedDeleted: false, + }, + { + clientDirty: true, + clientUSN: 1, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "", + serverDeleted: true, + expectedUSN: 3, + expectedLabel: "", + expectedDeleted: true, + }, + // server has higher usn and client is not dirty + { + clientDirty: false, + clientUSN: 1, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "b2-label-updated", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "b2-label-updated", + expectedDeleted: false, + }, + // they are in sync + { + clientDirty: false, + clientUSN: 3, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "b2-label", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "b2-label", + expectedDeleted: false, + }, + // they have the same usn but client is dirty + { + clientDirty: true, + clientUSN: 3, + clientLabel: "b2-label-client", + clientDeleted: false, + serverUSN: 3, + serverLabel: "b2-label", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "b2-label-client", + expectedDeleted: false, + }, + } + + for idx, tc := range testCases { + func() { + // set up + db := database.InitTestMemoryDB(t) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, tc.clientUSN, tc.clientLabel, tc.clientDirty, tc.clientDeleted) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + // update all fields but uuid and bump usn + b := client.SyncFragBook{ + UUID: b1UUID, + USN: tc.serverUSN, + Label: tc.serverLabel, + Deleted: tc.serverDeleted, + } + + if err := fullSyncBook(tx, b); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, fmt.Sprintf("note count mismatch for test case %d", idx)) + assert.Equalf(t, bookCount, 1, fmt.Sprintf("book count mismatch for test case %d", idx)) + + var b1 database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), + &b1.UUID, &b1.USN, &b1.Label, &b1.Dirty, &b1.Deleted) + + assert.Equal(t, b1.UUID, b1UUID, fmt.Sprintf("b1 UUID mismatch for idx %d", idx)) + assert.Equal(t, b1.USN, tc.expectedUSN, fmt.Sprintf("b1 USN mismatch for test case %d", idx)) + assert.Equal(t, b1.Label, tc.expectedLabel, fmt.Sprintf("b1 Label mismatch for test case %d", idx)) + assert.Equal(t, b1.Dirty, tc.clientDirty, fmt.Sprintf("b1 Dirty mismatch for test case %d", idx)) + assert.Equal(t, b1.Deleted, tc.expectedDeleted, fmt.Sprintf("b1 Deleted mismatch for test case %d", idx)) + }() + } + }) +} + +func TestStepSyncNote(t *testing.T) { + t.Run("exists on server only", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + n := client.SyncFragNote{ + UUID: "n1-uuid", + BookUUID: b1UUID, + USN: 128, + AddedOn: 1541232118, + EditedOn: 1541219321, + Body: "n1-body", + Deleted: false, + } + + if err := stepSyncNote(tx, n); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, "note count mismatch") + assert.Equalf(t, bookCount, 1, "book count mismatch") + + var n1 database.Note + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + + assert.Equal(t, n1.UUID, n.UUID, "n1 UUID mismatch") + assert.Equal(t, n1.BookUUID, n.BookUUID, "n1 BookUUID mismatch") + assert.Equal(t, n1.USN, n.USN, "n1 USN mismatch") + assert.Equal(t, n1.AddedOn, n.AddedOn, "n1 AddedOn mismatch") + assert.Equal(t, n1.EditedOn, n.EditedOn, "n1 EditedOn mismatch") + assert.Equal(t, n1.Body, n.Body, "n1 Body mismatch") + assert.Equal(t, n1.Deleted, n.Deleted, "n1 Deleted mismatch") + assert.Equal(t, n1.Dirty, false, "n1 Dirty mismatch") + }) + + t.Run("exists on server and client", func(t *testing.T) { + b1UUID := testutils.MustGenerateUUID(t) + b2UUID := testutils.MustGenerateUUID(t) + conflictBookUUID := testutils.MustGenerateUUID(t) + + testCases := []struct { + addedOn int64 + clientUSN int + clientEditedOn int64 + clientBody string + clientDeleted bool + clientBookUUID string + clientDirty bool + serverUSN int + serverEditedOn int64 + serverBody string + serverDeleted bool + serverBookUUID string + expectedUSN int + expectedAddedOn int64 + expectedEditedOn int64 + expectedBody string + expectedDeleted bool + expectedBookUUID string + expectedDirty bool + }{ + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: `<<<<<<< Local +Moved to the book b1-label +======= +Moved to the book b2-label +>>>>>>> Server + +<<<<<<< Local +n1 body +======= +n1 body edited +>>>>>>> Server +`, + expectedDeleted: false, + expectedBookUUID: conflictBookUUID, + expectedDirty: true, + }, + // if deleted locally, resurrect it + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 1541219321, + clientBody: "", + clientDeleted: true, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body edited", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: false, + }, + { + clientDirty: false, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body edited", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: false, + }, + } + + for idx, tc := range testCases { + func() { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label") + database.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "b2-label") + database.MustExec(t, fmt.Sprintf("inserting conflitcs book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", conflictBookUUID, "conflicts") + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, tc.clientBookUUID, tc.clientUSN, tc.addedOn, tc.clientEditedOn, tc.clientBody, tc.clientDeleted, tc.clientDirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + // update all fields but uuid and bump usn + n := client.SyncFragNote{ + UUID: n1UUID, + BookUUID: tc.serverBookUUID, + USN: tc.serverUSN, + AddedOn: tc.addedOn, + EditedOn: tc.serverEditedOn, + Body: tc.serverBody, + Deleted: tc.serverDeleted, + } + + if err := stepSyncNote(tx, n); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, fmt.Sprintf("note count mismatch for test case %d", idx)) + assert.Equalf(t, bookCount, 3, fmt.Sprintf("book count mismatch for test case %d", idx)) + + var n1 database.Note + database.MustScan(t, fmt.Sprintf("getting n1 for test case %d", idx), + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n.UUID), + &n1.UUID, &n1.BookUUID, &n1.USN, &n1.AddedOn, &n1.EditedOn, &n1.Body, &n1.Deleted, &n1.Dirty) + + assert.Equal(t, n1.UUID, n.UUID, fmt.Sprintf("n1 UUID mismatch for test case %d", idx)) + assert.Equal(t, n1.BookUUID, tc.expectedBookUUID, fmt.Sprintf("n1 BookUUID mismatch for test case %d", idx)) + assert.Equal(t, n1.USN, tc.expectedUSN, fmt.Sprintf("n1 USN mismatch for test case %d", idx)) + assert.Equal(t, n1.AddedOn, tc.expectedAddedOn, fmt.Sprintf("n1 AddedOn mismatch for test case %d", idx)) + assert.Equal(t, n1.EditedOn, tc.expectedEditedOn, fmt.Sprintf("n1 EditedOn mismatch for test case %d", idx)) + assert.Equal(t, n1.Body, tc.expectedBody, fmt.Sprintf("n1 Body mismatch for test case %d", idx)) + assert.Equal(t, n1.Deleted, tc.expectedDeleted, fmt.Sprintf("n1 Deleted mismatch for test case %d", idx)) + assert.Equal(t, n1.Dirty, tc.expectedDirty, fmt.Sprintf("n1 Dirty mismatch for test case %d", idx)) + }() + } + }) +} + +func TestStepSyncBook(t *testing.T) { + t.Run("exists on server only", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 555, "b1-label", true, false) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b2UUID := testutils.MustGenerateUUID(t) + b := client.SyncFragBook{ + UUID: b2UUID, + USN: 1, + AddedOn: 1541108743, + Label: "b2-label", + Deleted: false, + } + + if err := stepSyncBook(tx, b); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 2, "book count mismatch") + + var b1, b2 database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), + &b1.UUID, &b1.USN, &b1.Label, &b1.Dirty, &b1.Deleted) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b2UUID), + &b2.UUID, &b2.USN, &b2.Label, &b2.Dirty, &b2.Deleted) + + assert.Equal(t, b1.UUID, b1UUID, "b1 UUID mismatch") + assert.Equal(t, b1.USN, 555, "b1 USN mismatch") + assert.Equal(t, b1.Label, "b1-label", "b1 Label mismatch") + assert.Equal(t, b1.Dirty, true, "b1 Dirty mismatch") + assert.Equal(t, b1.Deleted, false, "b1 Deleted mismatch") + + assert.Equal(t, b2.UUID, b2UUID, "b2 UUID mismatch") + assert.Equal(t, b2.USN, b.USN, "b2 USN mismatch") + assert.Equal(t, b2.Label, b.Label, "b2 Label mismatch") + assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch") + assert.Equal(t, b2.Deleted, b.Deleted, "b2 Deleted mismatch") + }) + + t.Run("exists on server and client", func(t *testing.T) { + testCases := []struct { + clientDirty bool + clientUSN int + clientLabel string + clientDeleted bool + serverUSN int + serverLabel string + serverDeleted bool + expectedUSN int + expectedLabel string + expectedDeleted bool + anotherBookLabel string + expectedAnotherBookLabel string + expectedAnotherBookDirty bool + }{ + { + clientDirty: true, + clientUSN: 1, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "b2-label-updated", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "b2-label-updated", + expectedDeleted: false, + anotherBookLabel: "foo", + expectedAnotherBookLabel: "foo", + expectedAnotherBookDirty: false, + }, + { + clientDirty: false, + clientUSN: 1, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "b2-label-updated", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "b2-label-updated", + expectedDeleted: false, + anotherBookLabel: "foo", + expectedAnotherBookLabel: "foo", + expectedAnotherBookDirty: false, + }, + { + clientDirty: false, + clientUSN: 1, + clientLabel: "b2-label", + clientDeleted: false, + serverUSN: 3, + serverLabel: "foo", + serverDeleted: false, + expectedUSN: 3, + expectedLabel: "foo", + expectedDeleted: false, + anotherBookLabel: "foo", + expectedAnotherBookLabel: "foo_2", + expectedAnotherBookDirty: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + db := database.InitTestMemoryDB(t) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, tc.clientUSN, tc.clientLabel, tc.clientDirty, tc.clientDeleted) + b2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, fmt.Sprintf("inserting book for test case %d", idx), db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b2UUID, 2, tc.anotherBookLabel, false, false) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + // update all fields but uuid and bump usn + b := client.SyncFragBook{ + UUID: b1UUID, + USN: tc.serverUSN, + Label: tc.serverLabel, + Deleted: tc.serverDeleted, + } + + if err := fullSyncBook(tx, b); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, fmt.Sprintf("note count mismatch for test case %d", idx)) + assert.Equalf(t, bookCount, 2, fmt.Sprintf("book count mismatch for test case %d", idx)) + + var b1Record, b2Record database.Book + database.MustScan(t, "getting b1Record", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b1UUID), + &b1Record.UUID, &b1Record.USN, &b1Record.Label, &b1Record.Dirty, &b1Record.Deleted) + database.MustScan(t, "getting b2Record", + db.QueryRow("SELECT uuid, usn, label, dirty, deleted FROM books WHERE uuid = ?", b2UUID), + &b2Record.UUID, &b2Record.USN, &b2Record.Label, &b2Record.Dirty, &b2Record.Deleted) + + assert.Equal(t, b1Record.UUID, b1UUID, fmt.Sprintf("b1Record UUID mismatch for idx %d", idx)) + assert.Equal(t, b1Record.USN, tc.expectedUSN, fmt.Sprintf("b1Record USN mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Label, tc.expectedLabel, fmt.Sprintf("b1Record Label mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Dirty, tc.clientDirty, fmt.Sprintf("b1Record Dirty mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Deleted, tc.expectedDeleted, fmt.Sprintf("b1Record Deleted mismatch for test case %d", idx)) + + assert.Equal(t, b2Record.UUID, b2UUID, fmt.Sprintf("b2Record UUID mismatch for idx %d", idx)) + assert.Equal(t, b2Record.USN, 2, fmt.Sprintf("b2Record USN mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Label, tc.expectedAnotherBookLabel, fmt.Sprintf("b2Record Label mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Dirty, tc.expectedAnotherBookDirty, fmt.Sprintf("b2Record Dirty mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Deleted, false, fmt.Sprintf("b2Record Deleted mismatch for test case %d", idx)) + }() + } + }) +} + +func TestMergeBook(t *testing.T) { + t.Run("insert, no duplicates", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + // test + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b1 := client.SyncFragBook{ + UUID: "b1-uuid", + USN: 12, + AddedOn: 1541108743, + Label: "b1-label", + Deleted: false, + } + + if err := mergeBook(tx, b1, modeInsert); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // execute + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 1, "book count mismatch") + + var b1Record database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1.UUID, "b1 UUID mismatch") + assert.Equal(t, b1Record.Label, b1.Label, "b1 Label mismatch") + assert.Equal(t, b1Record.USN, b1.USN, "b1 USN mismatch") + }) + + t.Run("insert, 1 duplicate", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) + + // test + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b := client.SyncFragBook{ + UUID: "b2-uuid", + USN: 12, + AddedOn: 1541108743, + Label: "foo", + Deleted: false, + } + + if err := mergeBook(tx, b, modeInsert); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // execute + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 2, "book count mismatch") + + var b1Record, b2Record database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) + + assert.Equal(t, b1Record.Label, "foo_2", "b1 Label mismatch") + assert.Equal(t, b1Record.USN, 1, "b1 USN mismatch") + assert.Equal(t, b1Record.Dirty, true, "b1 should have been marked dirty") + + assert.Equal(t, b2Record.Label, "foo", "b2 Label mismatch") + assert.Equal(t, b2Record.USN, 12, "b2 USN mismatch") + assert.Equal(t, b2Record.Dirty, false, "b2 Dirty mismatch") + }) + + t.Run("insert, 3 duplicates", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "foo_2", true, false) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b3-uuid", 3, "foo_3", false, false) + + // test + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b := client.SyncFragBook{ + UUID: "b4-uuid", + USN: 12, + AddedOn: 1541108743, + Label: "foo", + Deleted: false, + } + + if err := mergeBook(tx, b, modeInsert); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // execute + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 4, "book count mismatch") + + var b1Record, b2Record, b3Record, b4Record database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) + database.MustScan(t, "getting b3", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b3-uuid"), + &b3Record.UUID, &b3Record.Label, &b3Record.USN, &b3Record.Dirty) + database.MustScan(t, "getting b4", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b4-uuid"), + &b4Record.UUID, &b4Record.Label, &b4Record.USN, &b4Record.Dirty) + + assert.Equal(t, b1Record.Label, "foo_4", "b1 Label mismatch") + assert.Equal(t, b1Record.USN, 1, "b1 USN mismatch") + assert.Equal(t, b1Record.Dirty, true, "b1 Dirty mismatch") + + assert.Equal(t, b2Record.Label, "foo_2", "b2 Label mismatch") + assert.Equal(t, b2Record.USN, 2, "b2 USN mismatch") + assert.Equal(t, b2Record.Dirty, true, "b2 Dirty mismatch") + + assert.Equal(t, b3Record.Label, "foo_3", "b3 Label mismatch") + assert.Equal(t, b3Record.USN, 3, "b3 USN mismatch") + assert.Equal(t, b3Record.Dirty, false, "b3 Dirty mismatch") + + assert.Equal(t, b4Record.Label, "foo", "b4 Label mismatch") + assert.Equal(t, b4Record.USN, 12, "b4 USN mismatch") + assert.Equal(t, b4Record.Dirty, false, "b4 Dirty mismatch") + }) + + t.Run("update, no duplicates", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + // test + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b1UUID, 1, "b1-label", false, false) + + b1 := client.SyncFragBook{ + UUID: b1UUID, + USN: 12, + AddedOn: 1541108743, + Label: "b1-label-edited", + Deleted: false, + } + + if err := mergeBook(tx, b1, modeUpdate); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // execute + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 1, "book count mismatch") + + var b1Record database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1UUID, "b1 UUID mismatch") + assert.Equal(t, b1Record.Label, "b1-label-edited", "b1 Label mismatch") + assert.Equal(t, b1Record.USN, 12, "b1 USN mismatch") + }) + + t.Run("update, 1 duplicate", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "bar", false, false) + + // test + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b := client.SyncFragBook{ + UUID: "b1-uuid", + USN: 12, + AddedOn: 1541108743, + Label: "bar", + Deleted: false, + } + + if err := mergeBook(tx, b, modeUpdate); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // execute + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 2, "book count mismatch") + + var b1Record, b2Record database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) + + assert.Equal(t, b1Record.Label, "bar", "b1 Label mismatch") + assert.Equal(t, b1Record.USN, 12, "b1 USN mismatch") + assert.Equal(t, b1Record.Dirty, false, "b1 Dirty mismatch") + + assert.Equal(t, b2Record.Label, "bar_2", "b2 Label mismatch") + assert.Equal(t, b2Record.USN, 2, "b2 USN mismatch") + assert.Equal(t, b2Record.Dirty, true, "b2 Dirty mismatch") + }) + + t.Run("update, 3 duplicate", func(t *testing.T) { + // set uj + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b1-uuid", 1, "foo", false, false) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b2-uuid", 2, "bar", false, false) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b3-uuid", 3, "bar_2", true, false) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, usn, label, dirty, deleted) VALUES (?, ?, ?, ?, ?)", "b4-uuid", 4, "bar_3", false, false) + + // test + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + b := client.SyncFragBook{ + UUID: "b1-uuid", + USN: 12, + AddedOn: 1541108743, + Label: "bar", + Deleted: false, + } + + if err := mergeBook(tx, b, modeUpdate); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // execute + var noteCount, bookCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 0, "note count mismatch") + assert.Equalf(t, bookCount, 4, "book count mismatch") + + var b1Record, b2Record, b3Record, b4Record database.Book + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b1-uuid"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b2-uuid"), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) + database.MustScan(t, "getting b3", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b3-uuid"), + &b3Record.UUID, &b3Record.Label, &b3Record.USN, &b3Record.Dirty) + database.MustScan(t, "getting b4", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "b4-uuid"), + &b4Record.UUID, &b4Record.Label, &b4Record.USN, &b4Record.Dirty) + + assert.Equal(t, b1Record.Label, "bar", "b1 Label mismatch") + assert.Equal(t, b1Record.USN, 12, "b1 USN mismatch") + assert.Equal(t, b1Record.Dirty, false, "b1 Dirty mismatch") + + assert.Equal(t, b2Record.Label, "bar_4", "b2 Label mismatch") + assert.Equal(t, b2Record.USN, 2, "b2 USN mismatch") + assert.Equal(t, b2Record.Dirty, true, "b2 Dirty mismatch") + + assert.Equal(t, b3Record.Label, "bar_2", "b3 Label mismatch") + assert.Equal(t, b3Record.USN, 3, "b3 USN mismatch") + assert.Equal(t, b3Record.Dirty, true, "b3 Dirty mismatch") + + assert.Equal(t, b4Record.Label, "bar_3", "b4 Label mismatch") + assert.Equal(t, b4Record.USN, 4, "b4 USN mismatch") + assert.Equal(t, b4Record.Dirty, false, "b4 Dirty mismatch") + }) +} + +func TestSaveServerState(t *testing.T) { + t.Run("with data received", func(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + testutils.LoginDB(t, db) + + database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 8) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + serverTime := int64(1541108743) + serverMaxUSN := 100 + userMaxUSN := 100 + + err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var lastSyncedAt int64 + var lastMaxUSN int + + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + + assert.Equal(t, lastSyncedAt, serverTime, "last synced at mismatch") + assert.Equal(t, lastMaxUSN, serverMaxUSN, "last max usn mismatch") + }) + + t.Run("with empty fragment but server has data - preserves last_max_usn", func(t *testing.T) { + // This tests the fix for the infinite sync bug where empty fragments + // would reset last_max_usn to 0, causing the client to re-download all data. + // When serverMaxUSN=0 (no data in fragment) but userMaxUSN>0 (server has data), + // we're caught up and should preserve the existing last_max_usn. + + // set up + db := database.InitTestMemoryDB(t) + testutils.LoginDB(t, db) + + existingLastMaxUSN := 100 + database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, existingLastMaxUSN) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + serverTime := int64(1541108743) + serverMaxUSN := 0 // Empty fragment (no data in this sync) + userMaxUSN := 150 // Server's actual max USN (higher than ours) + + err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var lastSyncedAt int64 + var lastMaxUSN int + + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + + assert.Equal(t, lastSyncedAt, serverTime, "last synced at should be updated") + // last_max_usn should NOT be updated to 0, it should preserve the existing value + assert.Equal(t, lastMaxUSN, existingLastMaxUSN, "last max usn should be preserved when fragment is empty but server has data") + }) + + t.Run("with empty server - resets last_max_usn to 0", func(t *testing.T) { + // When both serverMaxUSN=0 and userMaxUSN=0, the server is truly empty + // and we should reset last_max_usn to 0. + + // set up + db := database.InitTestMemoryDB(t) + testutils.LoginDB(t, db) + + database.MustExec(t, "inserting last synced at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, int64(1231108742)) + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 50) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + serverTime := int64(1541108743) + serverMaxUSN := 0 // Empty fragment + userMaxUSN := 0 // Server is actually empty + + err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var lastSyncedAt int64 + var lastMaxUSN int + + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncedAt) + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + + assert.Equal(t, lastSyncedAt, serverTime, "last synced at should be updated") + assert.Equal(t, lastMaxUSN, 0, "last max usn should be reset to 0 when server is empty") + }) +} + +// TestSendBooks tests that books are put to correct 'buckets' by running a test server and recording the +// uuid from the incoming data. It also tests that the uuid of the created books and book_uuids of their notes +// are updated accordingly based on the server response. +func TestSendBooks(t *testing.T) { + // set up + ctx := context.InitTestCtx(t) + testutils.Login(t, &ctx) + + db := ctx.DB + + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 0) + + // should be ignored + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) + database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b2-uuid", "b2-label", 2, false, false) + // should be created + database.MustExec(t, "inserting b3", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 0, false, true) + database.MustExec(t, "inserting b4", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b4-uuid", "b4-label", 0, false, true) + // should be only expunged locally without syncing to server + database.MustExec(t, "inserting b5", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b5-uuid", "b5-label", 0, true, true) + // should be deleted + database.MustExec(t, "inserting b6", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b6-uuid", "b6-label", 10, true, true) + // should be updated + database.MustExec(t, "inserting b7", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b7-uuid", "b7-label", 11, false, true) + database.MustExec(t, "inserting b8", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b8-uuid", "b8-label", 18, false, true) + + // some random notes + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 10, "n1 body", 1541108743, false, false) + database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", "b5-uuid", 10, "n2 body", 1541108743, false, false) + database.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", "b6-uuid", 10, "n3 body", 1541108743, false, false) + database.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n4-uuid", "b7-uuid", 10, "n4 body", 1541108743, false, false) + // notes that belong to the created book. Their book_uuid should be updated. + database.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", "b3-uuid", 10, "n5 body", 1541108743, false, false) + database.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", "b3-uuid", 10, "n6 body", 1541108743, false, false) + database.MustExec(t, "inserting n7", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n7-uuid", "b4-uuid", 10, "n7 body", 1541108743, false, false) + + var createdLabels []string + var updatesUUIDs []string + var deletedUUIDs []string + + // fire up a test server + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/v3/books" && r.Method == "POST" { + var payload client.CreateBookPayload + + err := json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) + return + } + + createdLabels = append(createdLabels, payload.Name) + + resp := client.CreateBookResp{ + Book: client.RespBook{ + UUID: fmt.Sprintf("server-%s-uuid", payload.Name), + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + + p := strings.Split(r.URL.Path, "/") + if len(p) == 4 && p[0] == "" && p[1] == "v3" && p[2] == "books" { + if r.Method == "PATCH" { + uuid := p[3] + updatesUUIDs = append(updatesUUIDs, uuid) + + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("{}")) + return + } else if r.Method == "DELETE" { + uuid := p[3] + deletedUUIDs = append(deletedUUIDs, uuid) + + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("{}")) + return + } + } + + t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) + })) + defer ts.Close() + + ctx.APIEndpoint = ts.URL + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if _, err := sendBooks(ctx, tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + + // First, sort data so that they can be asserted + sort.SliceStable(createdLabels, func(i, j int) bool { + return strings.Compare(createdLabels[i], createdLabels[j]) < 0 + }) + + assert.DeepEqual(t, createdLabels, []string{"b3-label", "b4-label"}, "createdLabels mismatch") + assert.DeepEqual(t, updatesUUIDs, []string{"b7-uuid", "b8-uuid"}, "updatesUUIDs mismatch") + assert.DeepEqual(t, deletedUUIDs, []string{"b6-uuid"}, "deletedUUIDs mismatch") + + var b1, b2, b3, b4, b7, b8 database.Book + database.MustScan(t, "getting b1", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b1-label"), &b1.UUID, &b1.Dirty) + database.MustScan(t, "getting b2", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b2-label"), &b2.UUID, &b2.Dirty) + database.MustScan(t, "getting b3", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b3-label"), &b3.UUID, &b3.Dirty) + database.MustScan(t, "getting b4", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b4-label"), &b4.UUID, &b4.Dirty) + database.MustScan(t, "getting b7", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b7-label"), &b7.UUID, &b7.Dirty) + database.MustScan(t, "getting b8", db.QueryRow("SELECT uuid, dirty FROM books WHERE label = ?", "b8-label"), &b8.UUID, &b8.Dirty) + + var bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + assert.Equalf(t, bookCount, 6, "book count mismatch") + + assert.Equal(t, b1.Dirty, false, "b1 Dirty mismatch") + assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch") + assert.Equal(t, b3.Dirty, false, "b3 Dirty mismatch") + assert.Equal(t, b4.Dirty, false, "b4 Dirty mismatch") + assert.Equal(t, b7.Dirty, false, "b7 Dirty mismatch") + assert.Equal(t, b8.Dirty, false, "b8 Dirty mismatch") + assert.Equal(t, b1.UUID, "b1-uuid", "b1 UUID mismatch") + assert.Equal(t, b2.UUID, "b2-uuid", "b2 UUID mismatch") + // uuids of created books should have been updated + assert.Equal(t, b3.UUID, "server-b3-label-uuid", "b3 UUID mismatch") + assert.Equal(t, b4.UUID, "server-b4-label-uuid", "b4 UUID mismatch") + assert.Equal(t, b7.UUID, "b7-uuid", "b7 UUID mismatch") + assert.Equal(t, b8.UUID, "b8-uuid", "b8 UUID mismatch") + + var n1, n2, n3, n4, n5, n6, n7 database.Note + database.MustScan(t, "getting n1", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n1 body"), &n1.BookUUID) + database.MustScan(t, "getting n2", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n2 body"), &n2.BookUUID) + database.MustScan(t, "getting n3", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n3 body"), &n3.BookUUID) + database.MustScan(t, "getting n4", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n4 body"), &n4.BookUUID) + database.MustScan(t, "getting n5", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n5 body"), &n5.BookUUID) + database.MustScan(t, "getting n6", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n6 body"), &n6.BookUUID) + database.MustScan(t, "getting n7", db.QueryRow("SELECT book_uuid FROM notes WHERE body = ?", "n7 body"), &n7.BookUUID) + assert.Equal(t, n1.BookUUID, "b1-uuid", "n1 bookUUID mismatch") + assert.Equal(t, n2.BookUUID, "b5-uuid", "n2 bookUUID mismatch") + assert.Equal(t, n3.BookUUID, "b6-uuid", "n3 bookUUID mismatch") + assert.Equal(t, n4.BookUUID, "b7-uuid", "n4 bookUUID mismatch") + assert.Equal(t, n5.BookUUID, "server-b3-label-uuid", "n5 bookUUID mismatch") + assert.Equal(t, n6.BookUUID, "server-b3-label-uuid", "n6 bookUUID mismatch") + assert.Equal(t, n7.BookUUID, "server-b4-label-uuid", "n7 bookUUID mismatch") +} + +func TestSendBooks_isBehind(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/v3/books" && r.Method == "POST" { + var payload client.CreateBookPayload + + err := json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) + return + } + + resp := client.CreateBookResp{ + Book: client.RespBook{ + USN: 11, + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + + p := strings.Split(r.URL.Path, "/") + if len(p) == 4 && p[0] == "" && p[1] == "v3" && p[2] == "books" { + if r.Method == "PATCH" { + resp := client.UpdateBookResp{ + Book: client.RespBook{ + USN: 11, + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } else if r.Method == "DELETE" { + resp := client.DeleteBookResp{ + Book: client.RespBook{ + USN: 11, + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + } + + t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) + })) + defer ts.Close() + + t.Run("create book", func(t *testing.T) { + testCases := []struct { + systemLastMaxUSN int + expectedIsBehind bool + }{ + { + systemLastMaxUSN: 10, + expectedIsBehind: false, + }, + { + systemLastMaxUSN: 9, + expectedIsBehind: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + ctx := context.InitTestCtx(t) + ctx.APIEndpoint = ts.URL + testutils.Login(t, &ctx) + + db := ctx.DB + + database.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, tc.systemLastMaxUSN) + database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 0, false, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + isBehind, err := sendBooks(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + assert.Equal(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) + }() + } + }) + + t.Run("delete book", func(t *testing.T) { + testCases := []struct { + systemLastMaxUSN int + expectedIsBehind bool + }{ + { + systemLastMaxUSN: 10, + expectedIsBehind: false, + }, + { + systemLastMaxUSN: 9, + expectedIsBehind: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + ctx := context.InitTestCtx(t) + ctx.APIEndpoint = ts.URL + testutils.Login(t, &ctx) + + db := ctx.DB + + database.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, tc.systemLastMaxUSN) + database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, true, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + isBehind, err := sendBooks(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + assert.Equal(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) + }() + } + }) + + t.Run("update book", func(t *testing.T) { + testCases := []struct { + systemLastMaxUSN int + expectedIsBehind bool + }{ + { + systemLastMaxUSN: 10, + expectedIsBehind: false, + }, + { + systemLastMaxUSN: 9, + expectedIsBehind: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + ctx := context.InitTestCtx(t) + ctx.APIEndpoint = ts.URL + testutils.Login(t, &ctx) + + db := ctx.DB + + database.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, tc.systemLastMaxUSN) + database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 11, false, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + isBehind, err := sendBooks(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + assert.Equal(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) + }() + } + }) +} + +// TestSendNotes tests that notes are put to correct 'buckets' by running a test server and recording the +// uuid from the incoming data. +func TestSendNotes(t *testing.T) { + // set up + ctx := context.InitTestCtx(t) + testutils.Login(t, &ctx) + + db := ctx.DB + + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 0) + + b1UUID := "b1-uuid" + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 1, false, false) + + // should be ignored + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1-body", 1541108743, false, false) + // should be created + database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 0, "n2-body", 1541108743, false, true) + // should be updated + database.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", b1UUID, 11, "n3-body", 1541108743, false, true) + // should be only expunged locally without syncing to server + database.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n4-uuid", b1UUID, 0, "n4-body", 1541108743, true, true) + // should be deleted + database.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", b1UUID, 17, "n5-body", 1541108743, true, true) + // should be created + database.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", b1UUID, 0, "n6-body", 1541108743, false, true) + // should be ignored + database.MustExec(t, "inserting n7", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n7-uuid", b1UUID, 12, "n7-body", 1541108743, false, false) + // should be updated + database.MustExec(t, "inserting n8", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n8-uuid", b1UUID, 17, "n8-body", 1541108743, false, true) + // should be deleted + database.MustExec(t, "inserting n9", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n9-uuid", b1UUID, 17, "n9-body", 1541108743, true, true) + // should be created + database.MustExec(t, "inserting n10", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n10-uuid", b1UUID, 0, "n10-body", 1541108743, false, true) + + var createdBodys []string + var updatedUUIDs []string + var deletedUUIDs []string + + // fire up a test server + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/v3/notes" && r.Method == "POST" { + var payload client.CreateNotePayload + + err := json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) + return + } + + createdBodys = append(createdBodys, payload.Body) + + resp := client.CreateNoteResp{ + Result: client.RespNote{ + UUID: fmt.Sprintf("server-%s-uuid", payload.Body), + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + + p := strings.Split(r.URL.Path, "/") + if len(p) == 4 && p[0] == "" && p[1] == "v3" && p[2] == "notes" { + if r.Method == "PATCH" { + uuid := p[3] + updatedUUIDs = append(updatedUUIDs, uuid) + + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("{}")) + return + } else if r.Method == "DELETE" { + uuid := p[3] + deletedUUIDs = append(deletedUUIDs, uuid) + + w.Header().Set("Content-Type", "application/json") + w.Write([]byte("{}")) + return + } + } + + t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) + })) + defer ts.Close() + + ctx.APIEndpoint = ts.URL + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if _, err := sendNotes(ctx, tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + sort.SliceStable(createdBodys, func(i, j int) bool { + return strings.Compare(createdBodys[i], createdBodys[j]) < 0 + }) + + assert.DeepEqual(t, createdBodys, []string{"n10-body", "n2-body", "n6-body"}, "createdBodys mismatch") + assert.DeepEqual(t, updatedUUIDs, []string{"n3-uuid", "n8-uuid"}, "updatedUUIDs mismatch") + assert.DeepEqual(t, deletedUUIDs, []string{"n5-uuid", "n9-uuid"}, "deletedUUIDs mismatch") + + var noteCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + assert.Equalf(t, noteCount, 7, "note count mismatch") + + var n1, n2, n3, n6, n7, n8, n10 database.Note + database.MustScan(t, "getting n1", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n1-body"), &n1.UUID, &n1.AddedOn, &n1.Dirty) + database.MustScan(t, "getting n2", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n2-body"), &n2.UUID, &n2.AddedOn, &n2.Dirty) + database.MustScan(t, "getting n3", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n3-body"), &n3.UUID, &n3.AddedOn, &n3.Dirty) + database.MustScan(t, "getting n6", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n6-body"), &n6.UUID, &n6.AddedOn, &n6.Dirty) + database.MustScan(t, "getting n7", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n7-body"), &n7.UUID, &n7.AddedOn, &n7.Dirty) + database.MustScan(t, "getting n8", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n8-body"), &n8.UUID, &n8.AddedOn, &n8.Dirty) + database.MustScan(t, "getting n10", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n10-body"), &n10.UUID, &n10.AddedOn, &n10.Dirty) + + assert.Equalf(t, noteCount, 7, "note count mismatch") + + assert.Equal(t, n1.Dirty, false, "n1 Dirty mismatch") + assert.Equal(t, n2.Dirty, false, "n2 Dirty mismatch") + assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch") + assert.Equal(t, n6.Dirty, false, "n6 Dirty mismatch") + assert.Equal(t, n7.Dirty, false, "n7 Dirty mismatch") + assert.Equal(t, n8.Dirty, false, "n8 Dirty mismatch") + assert.Equal(t, n10.Dirty, false, "n10 Dirty mismatch") + + assert.Equal(t, n1.AddedOn, int64(1541108743), "n1 AddedOn mismatch") + assert.Equal(t, n2.AddedOn, int64(1541108743), "n2 AddedOn mismatch") + assert.Equal(t, n3.AddedOn, int64(1541108743), "n3 AddedOn mismatch") + assert.Equal(t, n6.AddedOn, int64(1541108743), "n6 AddedOn mismatch") + assert.Equal(t, n7.AddedOn, int64(1541108743), "n7 AddedOn mismatch") + assert.Equal(t, n8.AddedOn, int64(1541108743), "n8 AddedOn mismatch") + assert.Equal(t, n10.AddedOn, int64(1541108743), "n10 AddedOn mismatch") + + // UUIDs of created notes should have been updated with those from the server response + assert.Equal(t, n1.UUID, "n1-uuid", "n1 UUID mismatch") + assert.Equal(t, n2.UUID, "server-n2-body-uuid", "n2 UUID mismatch") + assert.Equal(t, n3.UUID, "n3-uuid", "n3 UUID mismatch") + assert.Equal(t, n6.UUID, "server-n6-body-uuid", "n6 UUID mismatch") + assert.Equal(t, n7.UUID, "n7-uuid", "n7 UUID mismatch") + assert.Equal(t, n8.UUID, "n8-uuid", "n8 UUID mismatch") + assert.Equal(t, n10.UUID, "server-n10-body-uuid", "n10 UUID mismatch") +} + +func TestSendNotes_addedOn(t *testing.T) { + // set up + ctx := context.InitTestCtx(t) + testutils.Login(t, &ctx) + + db := ctx.DB + + database.MustExec(t, "inserting last max usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 0) + + // should be created + b1UUID := "b1-uuid" + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 0, "n1-body", 1541108743, false, true) + + // fire up a test server + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/v3/notes" && r.Method == "POST" { + resp := client.CreateNoteResp{ + Result: client.RespNote{ + UUID: testutils.MustGenerateUUID(t), + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + + t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) + })) + defer ts.Close() + + ctx.APIEndpoint = ts.URL + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if _, err := sendNotes(ctx, tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var n1 database.Note + database.MustScan(t, "getting n1", db.QueryRow("SELECT uuid, added_on, dirty FROM notes WHERE body = ?", "n1-body"), &n1.UUID, &n1.AddedOn, &n1.Dirty) + assert.Equal(t, n1.AddedOn, int64(1541108743), "n1 AddedOn mismatch") +} + +func TestSendNotes_isBehind(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/v3/notes" && r.Method == "POST" { + var payload client.CreateBookPayload + + err := json.NewDecoder(r.Body).Decode(&payload) + if err != nil { + t.Fatal(errors.Wrap(err, "decoding payload in the test server").Error()) + return + } + + resp := client.CreateNoteResp{ + Result: client.RespNote{ + USN: 11, + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + + p := strings.Split(r.URL.Path, "/") + if len(p) == 4 && p[0] == "" && p[1] == "v3" && p[2] == "notes" { + if r.Method == "PATCH" { + resp := client.UpdateNoteResp{ + Result: client.RespNote{ + USN: 11, + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } else if r.Method == "DELETE" { + resp := client.DeleteNoteResp{ + Result: client.RespNote{ + USN: 11, + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + return + } + } + + t.Fatalf("unrecognized endpoint reached Method: %s Path: %s", r.Method, r.URL.Path) + })) + defer ts.Close() + + t.Run("create note", func(t *testing.T) { + testCases := []struct { + systemLastMaxUSN int + expectedIsBehind bool + }{ + { + systemLastMaxUSN: 10, + expectedIsBehind: false, + }, + { + systemLastMaxUSN: 9, + expectedIsBehind: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + ctx := context.InitTestCtx(t) + testutils.Login(t, &ctx) + ctx.APIEndpoint = ts.URL + + db := ctx.DB + + database.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, tc.systemLastMaxUSN) + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 1, "n1 body", 1541108743, false, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + isBehind, err := sendNotes(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + assert.Equal(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) + }() + } + }) + + t.Run("delete note", func(t *testing.T) { + testCases := []struct { + systemLastMaxUSN int + expectedIsBehind bool + }{ + { + systemLastMaxUSN: 10, + expectedIsBehind: false, + }, + { + systemLastMaxUSN: 9, + expectedIsBehind: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + ctx := context.InitTestCtx(t) + testutils.Login(t, &ctx) + ctx.APIEndpoint = ts.URL + + db := ctx.DB + + database.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, tc.systemLastMaxUSN) + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 2, "n1 body", 1541108743, true, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + isBehind, err := sendNotes(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + assert.Equal(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) + }() + } + }) + + t.Run("update note", func(t *testing.T) { + testCases := []struct { + systemLastMaxUSN int + expectedIsBehind bool + }{ + { + systemLastMaxUSN: 10, + expectedIsBehind: false, + }, + { + systemLastMaxUSN: 9, + expectedIsBehind: true, + }, + } + + for idx, tc := range testCases { + func() { + // set up + ctx := context.InitTestCtx(t) + testutils.Login(t, &ctx) + ctx.APIEndpoint = ts.URL + + db := ctx.DB + + database.MustExec(t, fmt.Sprintf("inserting last max usn for test case %d", idx), db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, tc.systemLastMaxUSN) + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 8, "n1 body", 1541108743, false, true) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + isBehind, err := sendNotes(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + assert.Equal(t, isBehind, tc.expectedIsBehind, fmt.Sprintf("isBehind mismatch for test case %d", idx)) + }() + } + }) +} + +func TestMergeNote(t *testing.T) { + b1UUID := "b1-uuid" + b2UUID := "b2-uuid" + conflictBookUUID := testutils.MustGenerateUUID(t) + + testCases := []struct { + addedOn int64 + clientUSN int + clientEditedOn int64 + clientBody string + clientDeleted bool + clientBookUUID string + clientDirty bool + serverUSN int + serverEditedOn int64 + serverBody string + serverDeleted bool + serverBookUUID string + expectedUSN int + expectedAddedOn int64 + expectedEditedOn int64 + expectedBody string + expectedDeleted bool + expectedBookUUID string + expectedDirty bool + }{ + // local copy is not dirty + { + clientDirty: false, + clientUSN: 1, + clientEditedOn: 0, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b1UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body edited", + expectedDeleted: false, + expectedBookUUID: b1UUID, + expectedDirty: false, + }, + // local copy is dirty and needs conflict resolution + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 1541219320, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b1UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: `<<<<<<< Local +n1 body +======= +n1 body edited +>>>>>>> Server +`, + expectedDeleted: false, + expectedBookUUID: b1UUID, + expectedDirty: true, + }, + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 1541219319, + clientBody: "n1 body", + clientDeleted: false, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: `<<<<<<< Local +Moved to the book b1-label +======= +Moved to the book b2-label +>>>>>>> Server + +<<<<<<< Local +n1 body +======= +n1 body edited +>>>>>>> Server +`, + expectedDeleted: false, + expectedBookUUID: conflictBookUUID, + expectedDirty: true, + }, + // deleted locally and edited on server + { + clientDirty: true, + clientUSN: 1, + clientEditedOn: 1541219321, + clientBody: "", + clientDeleted: true, + clientBookUUID: b1UUID, + addedOn: 1541232118, + serverUSN: 21, + serverEditedOn: 1541219321, + serverBody: "n1 body edited", + serverDeleted: false, + serverBookUUID: b2UUID, + expectedUSN: 21, + expectedAddedOn: 1541232118, + expectedEditedOn: 1541219321, + expectedBody: "n1 body edited", + expectedDeleted: false, + expectedBookUUID: b2UUID, + expectedDirty: false, + }, + } + + for idx, tc := range testCases { + func() { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b1UUID, "b1-label", 5, false) + database.MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", b2UUID, "b2-label", 6, false) + database.MustExec(t, fmt.Sprintf("inserting conflitcs book for test case %d", idx), db, "INSERT INTO books (uuid, label) VALUES (?, ?)", conflictBookUUID, "conflicts") + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, b1UUID, tc.clientUSN, tc.addedOn, tc.clientEditedOn, tc.clientBody, tc.clientDeleted, tc.clientDirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + // update all fields but uuid and bump usn + fragNote := client.SyncFragNote{ + UUID: n1UUID, + BookUUID: tc.serverBookUUID, + USN: tc.serverUSN, + AddedOn: tc.addedOn, + EditedOn: tc.serverEditedOn, + Body: tc.serverBody, + Deleted: tc.serverDeleted, + } + var localNote database.Note + database.MustScan(t, fmt.Sprintf("getting localNote for test case %d", idx), + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n1UUID), + &localNote.UUID, &localNote.BookUUID, &localNote.USN, &localNote.AddedOn, &localNote.EditedOn, &localNote.Body, &localNote.Deleted, &localNote.Dirty) + + if err := mergeNote(tx, fragNote, localNote); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var noteCount, bookCount int + database.MustScan(t, fmt.Sprintf("counting notes for test case %d", idx), db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, fmt.Sprintf("counting books for test case %d", idx), db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, noteCount, 1, fmt.Sprintf("note count mismatch for test case %d", idx)) + assert.Equalf(t, bookCount, 3, fmt.Sprintf("book count mismatch for test case %d", idx)) + + var n1Record database.Note + database.MustScan(t, fmt.Sprintf("getting n1Record for test case %d", idx), + db.QueryRow("SELECT uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty FROM notes WHERE uuid = ?", n1UUID), + &n1Record.UUID, &n1Record.BookUUID, &n1Record.USN, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.Body, &n1Record.Deleted, &n1Record.Dirty) + var b1Record database.Book + database.MustScan(t, "getting b1Record for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b1UUID), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Dirty) + var b2Record database.Book + database.MustScan(t, "getting b2Record for test case", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", b2UUID), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1UUID, fmt.Sprintf("b1Record UUID mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Label, "b1-label", fmt.Sprintf("b1Record Label mismatch for test case %d", idx)) + assert.Equal(t, b1Record.USN, 5, fmt.Sprintf("b1Record USN mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Dirty, false, fmt.Sprintf("b1Record Dirty mismatch for test case %d", idx)) + + assert.Equal(t, b2Record.UUID, b2UUID, fmt.Sprintf("b2Record UUID mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Label, "b2-label", fmt.Sprintf("b2Record Label mismatch for test case %d", idx)) + assert.Equal(t, b2Record.USN, 6, fmt.Sprintf("b2Record USN mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Dirty, false, fmt.Sprintf("b2Record Dirty mismatch for test case %d", idx)) + + assert.Equal(t, n1Record.UUID, n1UUID, fmt.Sprintf("n1Record UUID mismatch for test case %d", idx)) + assert.Equal(t, n1Record.BookUUID, tc.expectedBookUUID, fmt.Sprintf("n1Record BookUUID mismatch for test case %d", idx)) + assert.Equal(t, n1Record.USN, tc.expectedUSN, fmt.Sprintf("n1Record USN mismatch for test case %d", idx)) + assert.Equal(t, n1Record.AddedOn, tc.expectedAddedOn, fmt.Sprintf("n1Record AddedOn mismatch for test case %d", idx)) + assert.Equal(t, n1Record.EditedOn, tc.expectedEditedOn, fmt.Sprintf("n1Record EditedOn mismatch for test case %d", idx)) + assert.Equal(t, n1Record.Body, tc.expectedBody, fmt.Sprintf("n1Record Body mismatch for test case %d", idx)) + assert.Equal(t, n1Record.Deleted, tc.expectedDeleted, fmt.Sprintf("n1Record Deleted mismatch for test case %d", idx)) + assert.Equal(t, n1Record.Dirty, tc.expectedDirty, fmt.Sprintf("n1Record Dirty mismatch for test case %d", idx)) + }() + } +} + +func TestCheckBookPristine(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", "b1-uuid", "b1-label", 5, false) + database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, dirty) VALUES (?, ?, ?, ?)", "b2-uuid", "b2-label", 6, false) + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", 1541108743, "n1 body", false) + database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n2-uuid", "b1-uuid", 1541108743, "n2 body", false) + database.MustExec(t, "inserting n3", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n3-uuid", "b1-uuid", 1541108743, "n3 body", true) + database.MustExec(t, "inserting n4", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n4-uuid", "b2-uuid", 1541108743, "n4 body", false) + database.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, added_on, body, dirty) VALUES (?, ?, ?, ?, ?)", "n5-uuid", "b2-uuid", 1541108743, "n5 body", false) + + t.Run("b1", func(t *testing.T) { + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + got, err := checkNotesPristine(tx, "b1-uuid") + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + assert.Equal(t, got, false, "b1 should not be pristine") + }) + + t.Run("b2", func(t *testing.T) { + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + got, err := checkNotesPristine(tx, "b2-uuid") + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + assert.Equal(t, got, true, "b2 should be pristine") + }) +} + +func TestCheckNoteInList(t *testing.T) { + list := syncList{ + Notes: map[string]client.SyncFragNote{ + "n1-uuid": { + UUID: "n1-uuid", + }, + "n2-uuid": { + UUID: "n2-uuid", + }, + }, + Books: map[string]client.SyncFragBook{ + "b1-uuid": { + UUID: "b1-uuid", + }, + "b2-uuid": { + UUID: "b2-uuid", + }, + }, + ExpungedNotes: map[string]bool{ + "n3-uuid": true, + "n4-uuid": true, + }, + ExpungedBooks: map[string]bool{ + "b3-uuid": true, + "b4-uuid": true, + }, + MaxUSN: 1, + MaxCurrentTime: 2, + } + + testCases := []struct { + uuid string + expected bool + }{ + { + uuid: "n1-uuid", + expected: true, + }, + { + uuid: "n2-uuid", + expected: true, + }, + { + uuid: "n3-uuid", + expected: true, + }, + { + uuid: "n4-uuid", + expected: true, + }, + { + uuid: "nonexistent-note-uuid", + expected: false, + }, + } + + for idx, tc := range testCases { + got := checkNoteInList(tc.uuid, &list) + assert.Equal(t, got, tc.expected, fmt.Sprintf("result mismatch for test case %d", idx)) + } +} + +func TestCheckBookInList(t *testing.T) { + list := syncList{ + Notes: map[string]client.SyncFragNote{ + "n1-uuid": { + UUID: "n1-uuid", + }, + "n2-uuid": { + UUID: "n2-uuid", + }, + }, + Books: map[string]client.SyncFragBook{ + "b1-uuid": { + UUID: "b1-uuid", + }, + "b2-uuid": { + UUID: "b2-uuid", + }, + }, + ExpungedNotes: map[string]bool{ + "n3-uuid": true, + "n4-uuid": true, + }, + ExpungedBooks: map[string]bool{ + "b3-uuid": true, + "b4-uuid": true, + }, + MaxUSN: 1, + MaxCurrentTime: 2, + } + + testCases := []struct { + uuid string + expected bool + }{ + { + uuid: "b1-uuid", + expected: true, + }, + { + uuid: "b2-uuid", + expected: true, + }, + { + uuid: "b3-uuid", + expected: true, + }, + { + uuid: "b4-uuid", + expected: true, + }, + { + uuid: "nonexistent-book-uuid", + expected: false, + }, + } + + for idx, tc := range testCases { + got := checkBookInList(tc.uuid, &list) + assert.Equal(t, got, tc.expected, fmt.Sprintf("result mismatch for test case %d", idx)) + } +} + +func TestCleanLocalNotes(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + list := syncList{ + Notes: map[string]client.SyncFragNote{ + "n1-uuid": { + UUID: "n1-uuid", + }, + "n2-uuid": { + UUID: "n2-uuid", + }, + }, + Books: map[string]client.SyncFragBook{ + "b1-uuid": { + UUID: "b1-uuid", + }, + "b2-uuid": { + UUID: "b2-uuid", + }, + }, + ExpungedNotes: map[string]bool{ + "n3-uuid": true, + "n4-uuid": true, + }, + ExpungedBooks: map[string]bool{ + "b3-uuid": true, + "b4-uuid": true, + }, + MaxUSN: 1, + MaxCurrentTime: 2, + } + + b1UUID := "b1-uuid" + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 1, false, false) + + // exists in the list + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", b1UUID, 10, "n1 body", 1541108743, false, false) + database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", b1UUID, 0, "n2 body", 1541108743, false, true) + // non-existent in the list but in valid state + // (created in the cli and hasn't been uploaded) + database.MustExec(t, "inserting n6", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n6-uuid", b1UUID, 0, "n6 body", 1541108743, false, true) + // non-existent in the list and in an invalid state + database.MustExec(t, "inserting n5", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n5-uuid", b1UUID, 7, "n5 body", 1541108743, true, true) + database.MustExec(t, "inserting n9", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n9-uuid", b1UUID, 17, "n9 body", 1541108743, true, false) + database.MustExec(t, "inserting n10", db, "INSERT INTO notes (uuid, book_uuid, usn, body, added_on, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", "n10-uuid", b1UUID, 0, "n10 body", 1541108743, false, false) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := cleanLocalNotes(tx, &list); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount int + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + assert.Equal(t, noteCount, 3, "note count mismatch") + + var n1, n2, n6 database.Note + database.MustScan(t, "getting n1", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", "n1-uuid"), &n1.Dirty) + database.MustScan(t, "getting n2", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", "n2-uuid"), &n2.Dirty) + database.MustScan(t, "getting n6", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", "n6-uuid"), &n6.Dirty) +} + +func TestCleanLocalBooks(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + list := syncList{ + Notes: map[string]client.SyncFragNote{ + "n1-uuid": { + UUID: "n1-uuid", + }, + "n2-uuid": { + UUID: "n2-uuid", + }, + }, + Books: map[string]client.SyncFragBook{ + "b1-uuid": { + UUID: "b1-uuid", + }, + "b2-uuid": { + UUID: "b2-uuid", + }, + }, + ExpungedNotes: map[string]bool{ + "n3-uuid": true, + "n4-uuid": true, + }, + ExpungedBooks: map[string]bool{ + "b3-uuid": true, + "b4-uuid": true, + }, + MaxUSN: 1, + MaxCurrentTime: 2, + } + + // existent in the server + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 1, false, false) + database.MustExec(t, "inserting b3", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 0, false, true) + // non-existent in the server but in valid state + database.MustExec(t, "inserting b5", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b5-uuid", "b5-label", 0, true, true) + // non-existent in the server and in an invalid state + database.MustExec(t, "inserting b6", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b6-uuid", "b6-label", 10, true, true) + database.MustExec(t, "inserting b7", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b7-uuid", "b7-label", 11, false, false) + database.MustExec(t, "inserting b8", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b8-uuid", "b8-label", 0, false, false) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := cleanLocalBooks(tx, &list); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + assert.Equal(t, bookCount, 3, "note count mismatch") + + var b1, b3, b5 database.Book + database.MustScan(t, "getting b1", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b1-uuid"), &b1.Label) + database.MustScan(t, "getting b3", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b3-uuid"), &b3.Label) + database.MustScan(t, "getting b5", db.QueryRow("SELECT label FROM books WHERE uuid = ?", "b5-uuid"), &b5.Label) +} + +func TestPrepareEmptyServerSync(t *testing.T) { + // set up + db := database.InitTestMemoryDB(t) + + // Setup: local has synced data (usn > 0, dirty = false) and some deleted items + database.MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b1-uuid", "b1-label", 5, false, false) + database.MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b2-uuid", "b2-label", 8, false, false) + database.MustExec(t, "inserting b3 deleted", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", "b3-uuid", "b3-label", 6, true, false) + database.MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n1-uuid", "b1-uuid", "note 1", 6, false, false, 1541108743) + database.MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n2-uuid", "b2-uuid", "note 2", 9, false, false, 1541108743) + database.MustExec(t, "inserting n3 deleted", db, "INSERT INTO notes (uuid, book_uuid, body, usn, deleted, dirty, added_on) VALUES (?, ?, ?, ?, ?, ?, ?)", "n3-uuid", "b1-uuid", "note 3", 7, true, false, 1541108743) + database.MustExec(t, "setting last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 9) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning transaction")) + } + + if err := prepareEmptyServerSync(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing prepareEmptyServerSync")) + } + + tx.Commit() + + // test - verify non-deleted items are marked dirty with usn=0, deleted items unchanged + var b1, b2, b3 database.Book + database.MustScan(t, "getting b1", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b1-uuid"), &b1.USN, &b1.Dirty, &b1.Deleted) + database.MustScan(t, "getting b2", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b2-uuid"), &b2.USN, &b2.Dirty, &b2.Deleted) + database.MustScan(t, "getting b3", db.QueryRow("SELECT usn, dirty, deleted FROM books WHERE uuid = ?", "b3-uuid"), &b3.USN, &b3.Dirty, &b3.Deleted) + + assert.Equal(t, b1.USN, 0, "b1 USN should be reset to 0") + assert.Equal(t, b1.Dirty, true, "b1 should be marked dirty") + assert.Equal(t, b1.Deleted, false, "b1 should not be deleted") + + assert.Equal(t, b2.USN, 0, "b2 USN should be reset to 0") + assert.Equal(t, b2.Dirty, true, "b2 should be marked dirty") + assert.Equal(t, b2.Deleted, false, "b2 should not be deleted") + + assert.Equal(t, b3.USN, 6, "b3 USN should remain unchanged (deleted item)") + assert.Equal(t, b3.Dirty, false, "b3 should not be marked dirty (deleted item)") + assert.Equal(t, b3.Deleted, true, "b3 should remain deleted") + + var n1, n2, n3 database.Note + database.MustScan(t, "getting n1", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n1-uuid"), &n1.USN, &n1.Dirty, &n1.Deleted) + database.MustScan(t, "getting n2", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n2-uuid"), &n2.USN, &n2.Dirty, &n2.Deleted) + database.MustScan(t, "getting n3", db.QueryRow("SELECT usn, dirty, deleted FROM notes WHERE uuid = ?", "n3-uuid"), &n3.USN, &n3.Dirty, &n3.Deleted) + + assert.Equal(t, n1.USN, 0, "n1 USN should be reset to 0") + assert.Equal(t, n1.Dirty, true, "n1 should be marked dirty") + assert.Equal(t, n1.Deleted, false, "n1 should not be deleted") + + assert.Equal(t, n2.USN, 0, "n2 USN should be reset to 0") + assert.Equal(t, n2.Dirty, true, "n2 should be marked dirty") + assert.Equal(t, n2.Deleted, false, "n2 should not be deleted") + + assert.Equal(t, n3.USN, 7, "n3 USN should remain unchanged (deleted item)") + assert.Equal(t, n3.Dirty, false, "n3 should not be marked dirty (deleted item)") + assert.Equal(t, n3.Deleted, true, "n3 should remain deleted") + + var lastMaxUSN int + database.MustScan(t, "getting last_max_usn", db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + assert.Equal(t, lastMaxUSN, 0, "last_max_usn should be reset to 0") +} diff --git a/pkg/cli/cmd/version/version.go b/pkg/cli/cmd/version/version.go new file mode 100644 index 00000000..79892204 --- /dev/null +++ b/pkg/cli/cmd/version/version.go @@ -0,0 +1,37 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package version + +import ( + "fmt" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/spf13/cobra" +) + +// NewCmd returns a new version command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "version", + Short: "Print the version number of Dnote", + Long: "Print the version number of Dnote", + Run: func(cmd *cobra.Command, args []string) { + fmt.Printf("dnote %s\n", ctx.Version) + }, + } + + return cmd +} diff --git a/pkg/cli/cmd/view/book.go b/pkg/cli/cmd/view/book.go new file mode 100644 index 00000000..698a7de5 --- /dev/null +++ b/pkg/cli/cmd/view/book.go @@ -0,0 +1,149 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "database/sql" + "fmt" + "io" + "strings" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/pkg/errors" +) + +// bookInfo is an information about the book to be printed on screen +type bookInfo struct { + BookLabel string + NoteCount int +} + +// noteInfo is an information about the note to be printed on screen +type noteInfo struct { + RowID int + Body string +} + +// getNewlineIdx returns the index of newline character in a string +func getNewlineIdx(str string) int { + // Check for \r\n first + if idx := strings.Index(str, "\r\n"); idx != -1 { + return idx + } + + // Then check for \n + return strings.Index(str, "\n") +} + +// formatBody returns an excerpt of the given raw note content and a boolean +// indicating if the returned string has been excertped +func formatBody(noteBody string) (string, bool) { + trimmed := strings.TrimRight(noteBody, "\r\n") + newlineIdx := getNewlineIdx(trimmed) + + if newlineIdx > -1 { + ret := strings.Trim(trimmed[0:newlineIdx], " ") + + return ret, true + } + + return strings.Trim(trimmed, " "), false +} + +func printBookLine(w io.Writer, info bookInfo, nameOnly bool) { + if nameOnly { + fmt.Fprintln(w, info.BookLabel) + } else { + fmt.Fprintf(w, "%s %s\n", info.BookLabel, log.ColorYellow.Sprintf("(%d)", info.NoteCount)) + } +} + +func listBooks(ctx context.DnoteCtx, w io.Writer, nameOnly bool) error { + db := ctx.DB + + rows, err := db.Query(`SELECT books.label, count(notes.uuid) note_count + FROM books + LEFT JOIN notes ON notes.book_uuid = books.uuid AND notes.deleted = false + WHERE books.deleted = false + GROUP BY books.uuid + ORDER BY books.label ASC;`) + if err != nil { + return errors.Wrap(err, "querying books") + } + defer rows.Close() + + infos := []bookInfo{} + for rows.Next() { + var info bookInfo + err = rows.Scan(&info.BookLabel, &info.NoteCount) + if err != nil { + return errors.Wrap(err, "scanning a row") + } + + infos = append(infos, info) + } + + for _, info := range infos { + printBookLine(w, info, nameOnly) + } + + return nil +} + +func listNotes(ctx context.DnoteCtx, w io.Writer, bookName string) error { + db := ctx.DB + + var bookUUID string + err := db.QueryRow("SELECT uuid FROM books WHERE label = ?", bookName).Scan(&bookUUID) + if err == sql.ErrNoRows { + return errors.New("book not found") + } else if err != nil { + return errors.Wrap(err, "querying the book") + } + + rows, err := db.Query(`SELECT rowid, body FROM notes WHERE book_uuid = ? AND deleted = ? ORDER BY added_on ASC;`, bookUUID, false) + if err != nil { + return errors.Wrap(err, "querying notes") + } + defer rows.Close() + + infos := []noteInfo{} + for rows.Next() { + var info noteInfo + err = rows.Scan(&info.RowID, &info.Body) + if err != nil { + return errors.Wrap(err, "scanning a row") + } + + infos = append(infos, info) + } + + fmt.Fprintf(w, "on book %s\n", bookName) + + for _, info := range infos { + body, isExcerpt := formatBody(info.Body) + + rowid := log.ColorYellow.Sprintf("(%d)", info.RowID) + if isExcerpt { + body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]")) + } + + fmt.Fprintf(w, "%s %s\n", rowid, body) + } + + return nil +} diff --git a/pkg/cli/cmd/view/book_test.go b/pkg/cli/cmd/view/book_test.go new file mode 100644 index 00000000..226d5d04 --- /dev/null +++ b/pkg/cli/cmd/view/book_test.go @@ -0,0 +1,184 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "bytes" + "fmt" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" +) + +func TestGetNewlineIdx(t *testing.T) { + testCases := []struct { + input string + expected int + }{ + { + input: "hello\nworld", + expected: 5, + }, + { + input: "hello\r\nworld", + expected: 5, + }, + { + input: "no newline here", + expected: -1, + }, + { + input: "", + expected: -1, + }, + { + input: "\n", + expected: 0, + }, + { + input: "\r\n", + expected: 0, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("input: %q", tc.input), func(t *testing.T) { + got := getNewlineIdx(tc.input) + assert.Equal(t, got, tc.expected, "newline index mismatch") + }) + } +} + +func TestFormatBody(t *testing.T) { + testCases := []struct { + input string + expectedBody string + expectedExcerpt bool + }{ + { + input: "single line", + expectedBody: "single line", + expectedExcerpt: false, + }, + { + input: "first line\nsecond line", + expectedBody: "first line", + expectedExcerpt: true, + }, + { + input: "first line\r\nsecond line", + expectedBody: "first line", + expectedExcerpt: true, + }, + { + input: " spaced line ", + expectedBody: "spaced line", + expectedExcerpt: false, + }, + { + input: " first line \nsecond line", + expectedBody: "first line", + expectedExcerpt: true, + }, + { + input: "", + expectedBody: "", + expectedExcerpt: false, + }, + { + input: "line with trailing newline\n", + expectedBody: "line with trailing newline", + expectedExcerpt: false, + }, + { + input: "line with trailing newlines\n\n", + expectedBody: "line with trailing newlines", + expectedExcerpt: false, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("input: %q", tc.input), func(t *testing.T) { + gotBody, gotExcerpt := formatBody(tc.input) + assert.Equal(t, gotBody, tc.expectedBody, "formatted body mismatch") + assert.Equal(t, gotExcerpt, tc.expectedExcerpt, "excerpt flag mismatch") + }) + } +} + +func TestListNotes(t *testing.T) { + // Setup + db := database.InitTestMemoryDB(t) + defer db.Close() + + bookUUID := "js-book-uuid" + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", bookUUID, "javascript") + database.MustExec(t, "inserting note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-1", bookUUID, "first note", 1515199943) + database.MustExec(t, "inserting note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-2", bookUUID, "multiline note\nwith second line", 1515199945) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + // Execute + err := listNotes(ctx, &buf, "javascript") + if err != nil { + t.Fatal(err) + } + + got := buf.String() + + // Verify output + assert.Equal(t, strings.Contains(got, "on book javascript"), true, "should show book name") + assert.Equal(t, strings.Contains(got, "first note"), true, "should contain first note") + assert.Equal(t, strings.Contains(got, "multiline note"), true, "should show first line of multiline note") + assert.Equal(t, strings.Contains(got, "[---More---]"), true, "should show more indicator for multiline note") + assert.Equal(t, strings.Contains(got, "with second line"), false, "should not show second line of multiline note") +} + +func TestListBooks(t *testing.T) { + // Setup + db := database.InitTestMemoryDB(t) + defer db.Close() + + b1UUID := "js-book-uuid" + b2UUID := "linux-book-uuid" + + database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "javascript") + database.MustExec(t, "inserting book 2", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "linux") + + // Add notes to test count + database.MustExec(t, "inserting note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-1", b1UUID, "note body 1", 1515199943) + database.MustExec(t, "inserting note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "note-2", b1UUID, "note body 2", 1515199944) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + // Execute + err := listBooks(ctx, &buf, false) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + + // Verify output + assert.Equal(t, strings.Contains(got, "javascript"), true, "should contain javascript book") + assert.Equal(t, strings.Contains(got, "linux"), true, "should contain linux book") + assert.Equal(t, strings.Contains(got, "(2)"), true, "should show 2 notes for javascript") +} diff --git a/pkg/cli/cmd/view/note.go b/pkg/cli/cmd/view/note.go new file mode 100644 index 00000000..f853dd9a --- /dev/null +++ b/pkg/cli/cmd/view/note.go @@ -0,0 +1,47 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "io" + "strconv" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/output" + "github.com/pkg/errors" +) + +func viewNote(ctx context.DnoteCtx, w io.Writer, noteRowIDArg string, contentOnly bool) error { + noteRowID, err := strconv.Atoi(noteRowIDArg) + if err != nil { + return errors.Wrap(err, "invalid rowid") + } + + db := ctx.DB + info, err := database.GetNoteInfo(db, noteRowID) + if err != nil { + return err + } + + if contentOnly { + output.NoteContent(w, info) + } else { + output.NoteInfo(w, info) + } + + return nil +} diff --git a/pkg/cli/cmd/view/note_test.go b/pkg/cli/cmd/view/note_test.go new file mode 100644 index 00000000..36e9aa84 --- /dev/null +++ b/pkg/cli/cmd/view/note_test.go @@ -0,0 +1,90 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "bytes" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" +) + +func TestViewNote(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + bookUUID := "test-book-uuid" + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", bookUUID, "golang") + database.MustExec(t, "inserting note", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", + "note-uuid", bookUUID, "test note content", 1515199943000000000) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "1", false) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + assert.Equal(t, strings.Contains(got, "test note content"), true, "should contain note content") +} + +func TestViewNoteContentOnly(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + bookUUID := "test-book-uuid" + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", bookUUID, "golang") + database.MustExec(t, "inserting note", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", + "note-uuid", bookUUID, "test note content", 1515199943000000000) + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "1", true) + if err != nil { + t.Fatal(err) + } + + got := buf.String() + assert.Equal(t, got, "test note content", "should contain only note content") +} + +func TestViewNoteInvalidRowID(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "not-a-number", false) + assert.NotEqual(t, err, nil, "should return error for invalid rowid") +} + +func TestViewNoteNotFound(t *testing.T) { + db := database.InitTestMemoryDB(t) + defer db.Close() + + ctx := context.DnoteCtx{DB: db} + var buf bytes.Buffer + + err := viewNote(ctx, &buf, "999", false) + assert.NotEqual(t, err, nil, "should return error for non-existent note") +} diff --git a/pkg/cli/cmd/view/view.go b/pkg/cli/cmd/view/view.go new file mode 100644 index 00000000..57b17dbd --- /dev/null +++ b/pkg/cli/cmd/view/view.go @@ -0,0 +1,92 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package view + +import ( + "os" + + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +var example = ` + * View all books + dnote view + + * List notes in a book + dnote view javascript + + * View a particular note in a book + dnote view javascript 0 + ` + +var nameOnly bool +var contentOnly bool + +func preRun(cmd *cobra.Command, args []string) error { + if len(args) > 2 { + return errors.New("Incorrect number of argument") + } + + return nil +} + +// NewCmd returns a new view command +func NewCmd(ctx context.DnoteCtx) *cobra.Command { + cmd := &cobra.Command{ + Use: "view ", + Aliases: []string{"v"}, + Short: "List books, notes or view a content", + Example: example, + RunE: newRun(ctx), + PreRunE: preRun, + } + + f := cmd.Flags() + f.BoolVarP(&nameOnly, "name-only", "", false, "print book names only") + f.BoolVarP(&contentOnly, "content-only", "", false, "print the note content only") + + return cmd +} + +func newRun(ctx context.DnoteCtx) infra.RunEFunc { + return func(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + // List all books + return listBooks(ctx, os.Stdout, nameOnly) + } else if len(args) == 1 { + if nameOnly { + return errors.New("--name-only flag is only valid when viewing books") + } + + if utils.IsNumber(args[0]) { + // View a note by index + return viewNote(ctx, os.Stdout, args[0], contentOnly) + } else { + // List notes in a book + return listNotes(ctx, os.Stdout, args[0]) + } + } else if len(args) == 2 { + // View a note in a book (book name + note index) + return viewNote(ctx, os.Stdout, args[1], contentOnly) + } + + return errors.New("Incorrect number of arguments") + } +} diff --git a/pkg/cli/config/config.go b/pkg/cli/config/config.go new file mode 100644 index 00000000..065100c0 --- /dev/null +++ b/pkg/cli/config/config.go @@ -0,0 +1,94 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package config + +import ( + "fmt" + "os" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" + "gopkg.in/yaml.v2" +) + +// Config holds dnote configuration +type Config struct { + Editor string `yaml:"editor"` + APIEndpoint string `yaml:"apiEndpoint"` + EnableUpgradeCheck bool `yaml:"enableUpgradeCheck"` +} + +func checkLegacyPath(ctx context.DnoteCtx) (string, bool) { + legacyPath := fmt.Sprintf("%s/%s", ctx.Paths.LegacyDnote, consts.ConfigFilename) + + ok, err := utils.FileExists(legacyPath) + if err != nil { + log.Error(errors.Wrapf(err, "checking legacy dnote directory at %s", legacyPath).Error()) + } + if ok { + return legacyPath, true + } + + return "", false +} + +// GetPath returns the path to the dnote config file +func GetPath(ctx context.DnoteCtx) string { + legacyPath, ok := checkLegacyPath(ctx) + if ok { + return legacyPath + } + + return fmt.Sprintf("%s/%s/%s", ctx.Paths.Config, consts.DnoteDirName, consts.ConfigFilename) +} + +// Read reads the config file +func Read(ctx context.DnoteCtx) (Config, error) { + var ret Config + + configPath := GetPath(ctx) + b, err := os.ReadFile(configPath) + if err != nil { + return ret, errors.Wrap(err, "reading config file") + } + + err = yaml.Unmarshal(b, &ret) + if err != nil { + return ret, errors.Wrap(err, "unmarshalling config") + } + + return ret, nil +} + +// Write writes the config to the config file +func Write(ctx context.DnoteCtx, cf Config) error { + path := GetPath(ctx) + + b, err := yaml.Marshal(cf) + if err != nil { + return errors.Wrap(err, "marshalling config into YAML") + } + + err = os.WriteFile(path, b, 0644) + if err != nil { + return errors.Wrap(err, "writing the config file") + } + + return nil +} diff --git a/pkg/cli/consts/consts.go b/pkg/cli/consts/consts.go new file mode 100644 index 00000000..0f49c152 --- /dev/null +++ b/pkg/cli/consts/consts.go @@ -0,0 +1,47 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package consts provides definitions of constants +package consts + +var ( + // LegacyDnoteDirName is the name of the legacy directory containing dnote files + LegacyDnoteDirName = ".dnote" + // DnoteDirName is the name of the directory containing dnote files + DnoteDirName = "dnote" + // DnoteDBFileName is a filename for the Dnote SQLite database + DnoteDBFileName = "dnote.db" + // TmpContentFileBase is the base for the filename for a temporary content + TmpContentFileBase = "DNOTE_TMPCONTENT" + // TmpContentFileExt is the extension for the temporary content file + TmpContentFileExt = "md" + // ConfigFilename is the name of the config file + ConfigFilename = "dnoterc" + + // SystemSchema is the key for schema in the system table + SystemSchema = "schema" + // SystemRemoteSchema is the key for remote schema in the system table + SystemRemoteSchema = "remote_schema" + // SystemLastSyncAt is the timestamp of the server at the last sync + SystemLastSyncAt = "last_sync_time" + // SystemLastMaxUSN is the user's max_usn from the server at the alst sync + SystemLastMaxUSN = "last_max_usn" + // SystemLastUpgrade is the timestamp at which the system more recently checked for an upgrade + SystemLastUpgrade = "last_upgrade" + // SystemSessionKey is the session key + SystemSessionKey = "session_token" + // SystemSessionKeyExpiry is the timestamp at which the session key will expire + SystemSessionKeyExpiry = "session_token_expiry" +) diff --git a/pkg/cli/context/ctx.go b/pkg/cli/context/ctx.go new file mode 100644 index 00000000..971d9145 --- /dev/null +++ b/pkg/cli/context/ctx.go @@ -0,0 +1,61 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package context defines dnote context +package context + +import ( + "net/http" + + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/clock" +) + +// Paths contain directory definitions +type Paths struct { + Home string + Config string + Data string + Cache string + LegacyDnote string +} + +// DnoteCtx is a context holding the information of the current runtime +type DnoteCtx struct { + Paths Paths + APIEndpoint string + Version string + DB *database.DB + SessionKey string + SessionKeyExpiry int64 + Editor string + Clock clock.Clock + EnableUpgradeCheck bool + HTTPClient *http.Client +} + +// Redact replaces private information from the context with a set of +// placeholder values. +func Redact(ctx DnoteCtx) DnoteCtx { + var sessionKey string + if ctx.SessionKey != "" { + sessionKey = "1" + } else { + sessionKey = "0" + } + ctx.SessionKey = sessionKey + + return ctx +} diff --git a/pkg/cli/context/files.go b/pkg/cli/context/files.go new file mode 100644 index 00000000..1abbcd47 --- /dev/null +++ b/pkg/cli/context/files.go @@ -0,0 +1,48 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package context + +import ( + "path/filepath" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +// InitDnoteDirs creates the dnote directories if they don't already exist. +func InitDnoteDirs(paths Paths) error { + if paths.Config != "" { + configDir := filepath.Join(paths.Config, consts.DnoteDirName) + if err := utils.EnsureDir(configDir); err != nil { + return errors.Wrap(err, "initializing config dir") + } + } + if paths.Data != "" { + dataDir := filepath.Join(paths.Data, consts.DnoteDirName) + if err := utils.EnsureDir(dataDir); err != nil { + return errors.Wrap(err, "initializing data dir") + } + } + if paths.Cache != "" { + cacheDir := filepath.Join(paths.Cache, consts.DnoteDirName) + if err := utils.EnsureDir(cacheDir); err != nil { + return errors.Wrap(err, "initializing cache dir") + } + } + + return nil +} diff --git a/pkg/cli/context/files_test.go b/pkg/cli/context/files_test.go new file mode 100644 index 00000000..49d62dc9 --- /dev/null +++ b/pkg/cli/context/files_test.go @@ -0,0 +1,62 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package context + +import ( + "os" + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" +) + +func assertDirsExist(t *testing.T, paths Paths) { + configDir := filepath.Join(paths.Config, consts.DnoteDirName) + info, err := os.Stat(configDir) + assert.Equal(t, err, nil, "config dir should exist") + assert.Equal(t, info.IsDir(), true, "config should be a directory") + + dataDir := filepath.Join(paths.Data, consts.DnoteDirName) + info, err = os.Stat(dataDir) + assert.Equal(t, err, nil, "data dir should exist") + assert.Equal(t, info.IsDir(), true, "data should be a directory") + + cacheDir := filepath.Join(paths.Cache, consts.DnoteDirName) + info, err = os.Stat(cacheDir) + assert.Equal(t, err, nil, "cache dir should exist") + assert.Equal(t, info.IsDir(), true, "cache should be a directory") +} + +func TestInitDnoteDirs(t *testing.T) { + tmpDir := t.TempDir() + + paths := Paths{ + Config: filepath.Join(tmpDir, "config"), + Data: filepath.Join(tmpDir, "data"), + Cache: filepath.Join(tmpDir, "cache"), + } + + // Initialize directories + err := InitDnoteDirs(paths) + assert.Equal(t, err, nil, "InitDnoteDirs should succeed") + assertDirsExist(t, paths) + + // Call again - should be idempotent + err = InitDnoteDirs(paths) + assert.Equal(t, err, nil, "InitDnoteDirs should succeed when dirs already exist") + assertDirsExist(t, paths) +} diff --git a/pkg/cli/context/testutils.go b/pkg/cli/context/testutils.go new file mode 100644 index 00000000..cb477475 --- /dev/null +++ b/pkg/cli/context/testutils.go @@ -0,0 +1,100 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package context + +import ( + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/clock" + "github.com/pkg/errors" +) + +// getDefaultTestPaths creates default test paths with all paths pointing to a temp directory +func getDefaultTestPaths(t *testing.T) Paths { + tmpDir := t.TempDir() + return Paths{ + Home: tmpDir, + Cache: tmpDir, + Config: tmpDir, + Data: tmpDir, + } +} + + +// InitTestCtx initializes a test context with an in-memory database +// and a temporary directory for all paths +func InitTestCtx(t *testing.T) DnoteCtx { + paths := getDefaultTestPaths(t) + db := database.InitTestMemoryDB(t) + + if err := InitDnoteDirs(paths); err != nil { + t.Fatal(errors.Wrap(err, "creating test directories")) + } + + return DnoteCtx{ + DB: db, + Paths: paths, + Clock: clock.NewMock(), // Use a mock clock to test times + } +} + +// InitTestCtxWithDB initializes a test context with the provided database +// and a temporary directory for all paths. +// Used when you need full control over database initialization (e.g. migration tests). +func InitTestCtxWithDB(t *testing.T, db *database.DB) DnoteCtx { + paths := getDefaultTestPaths(t) + + if err := InitDnoteDirs(paths); err != nil { + t.Fatal(errors.Wrap(err, "creating test directories")) + } + + return DnoteCtx{ + DB: db, + Paths: paths, + Clock: clock.NewMock(), // Use a mock clock to test times + } +} + +// InitTestCtxWithFileDB initializes a test context with a file-based database +// at the expected path. +func InitTestCtxWithFileDB(t *testing.T) DnoteCtx { + paths := getDefaultTestPaths(t) + + if err := InitDnoteDirs(paths); err != nil { + t.Fatal(errors.Wrap(err, "creating test directories")) + } + + dbPath := filepath.Join(paths.Data, consts.DnoteDirName, consts.DnoteDBFileName) + db, err := database.Open(dbPath) + if err != nil { + t.Fatal(errors.Wrap(err, "opening database")) + } + + if _, err := db.Exec(database.GetDefaultSchemaSQL()); err != nil { + t.Fatal(errors.Wrap(err, "running schema sql")) + } + + t.Cleanup(func() { db.Close() }) + + return DnoteCtx{ + DB: db, + Paths: paths, + Clock: clock.NewMock(), // Use a mock clock to test times + } +} diff --git a/cli/core/models.go b/pkg/cli/database/models.go similarity index 65% rename from cli/core/models.go rename to pkg/cli/database/models.go index b41e05d8..e8e463bb 100644 --- a/cli/core/models.go +++ b/pkg/cli/database/models.go @@ -1,25 +1,21 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package core +package database import ( - "github.com/dnote/dnote/cli/infra" "github.com/pkg/errors" ) @@ -35,19 +31,19 @@ type Book struct { // Note represents a note type Note struct { + RowID int `json:"rowid"` UUID string `json:"uuid"` BookUUID string `json:"book_uuid"` Body string `json:"content"` AddedOn int64 `json:"added_on"` EditedOn int64 `json:"edited_on"` USN int `json:"usn"` - Public bool `json:"public"` Deleted bool `json:"deleted"` Dirty bool `json:"dirty"` } // NewNote constructs a note with the given data -func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, public, deleted, dirty bool) Note { +func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, deleted, dirty bool) Note { return Note{ UUID: uuid, BookUUID: bookUUID, @@ -55,16 +51,15 @@ func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, publ AddedOn: addedOn, EditedOn: editedOn, USN: usn, - Public: public, Deleted: deleted, Dirty: dirty, } } // Insert inserts a new note -func (n Note) Insert(db *infra.DB) error { - _, err := db.Exec("INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", - n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, n.Dirty) +func (n Note) Insert(db *DB) error { + _, err := db.Exec("INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", + n.UUID, n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, n.Dirty) if err != nil { return errors.Wrapf(err, "inserting note with uuid %s", n.UUID) @@ -74,9 +69,9 @@ func (n Note) Insert(db *infra.DB) error { } // Update updates the note with the given data -func (n Note) Update(db *infra.DB) error { - _, err := db.Exec("UPDATE notes SET book_uuid = ?, body = ?, added_on = ?, edited_on = ?, usn = ?, public = ?, deleted = ?, dirty = ? WHERE uuid = ?", - n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Public, n.Deleted, n.Dirty, n.UUID) +func (n Note) Update(db *DB) error { + _, err := db.Exec("UPDATE notes SET book_uuid = ?, body = ?, added_on = ?, edited_on = ?, usn = ?, deleted = ?, dirty = ? WHERE uuid = ?", + n.BookUUID, n.Body, n.AddedOn, n.EditedOn, n.USN, n.Deleted, n.Dirty, n.UUID) if err != nil { return errors.Wrapf(err, "updating the note with uuid %s", n.UUID) @@ -86,7 +81,7 @@ func (n Note) Update(db *infra.DB) error { } // UpdateUUID updates the uuid of a book -func (n *Note) UpdateUUID(db *infra.DB, newUUID string) error { +func (n *Note) UpdateUUID(db *DB, newUUID string) error { _, err := db.Exec("UPDATE notes SET uuid = ? WHERE uuid = ?", newUUID, n.UUID) if err != nil { @@ -99,7 +94,7 @@ func (n *Note) UpdateUUID(db *infra.DB, newUUID string) error { } // Expunge hard-deletes the note from the database -func (n Note) Expunge(db *infra.DB) error { +func (n Note) Expunge(db *DB) error { _, err := db.Exec("DELETE FROM notes WHERE uuid = ?", n.UUID) if err != nil { return errors.Wrap(err, "expunging a note locally") @@ -120,7 +115,7 @@ func NewBook(uuid, label string, usn int, deleted, dirty bool) Book { } // Insert inserts a new book -func (b Book) Insert(db *infra.DB) error { +func (b Book) Insert(db *DB) error { _, err := db.Exec("INSERT INTO books (uuid, label, usn, dirty, deleted) VALUES (?, ?, ?, ?, ?)", b.UUID, b.Label, b.USN, b.Dirty, b.Deleted) @@ -132,7 +127,7 @@ func (b Book) Insert(db *infra.DB) error { } // Update updates the book with the given data -func (b Book) Update(db *infra.DB) error { +func (b Book) Update(db *DB) error { _, err := db.Exec("UPDATE books SET label = ?, usn = ?, dirty = ?, deleted = ? WHERE uuid = ?", b.Label, b.USN, b.Dirty, b.Deleted, b.UUID) @@ -144,7 +139,7 @@ func (b Book) Update(db *infra.DB) error { } // UpdateUUID updates the uuid of a book -func (b *Book) UpdateUUID(db *infra.DB, newUUID string) error { +func (b *Book) UpdateUUID(db *DB, newUUID string) error { _, err := db.Exec("UPDATE books SET uuid = ? WHERE uuid = ?", newUUID, b.UUID) if err != nil { @@ -157,7 +152,7 @@ func (b *Book) UpdateUUID(db *infra.DB, newUUID string) error { } // Expunge hard-deletes the book from the database -func (b Book) Expunge(db *infra.DB) error { +func (b Book) Expunge(db *DB) error { _, err := db.Exec("DELETE FROM books WHERE uuid = ?", b.UUID) if err != nil { return errors.Wrap(err, "expunging a book locally") diff --git a/pkg/cli/database/models_test.go b/pkg/cli/database/models_test.go new file mode 100644 index 00000000..6d0a45f0 --- /dev/null +++ b/pkg/cli/database/models_test.go @@ -0,0 +1,851 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/pkg/errors" +) + +func TestNewNote(t *testing.T) { + testCases := []struct { + uuid string + bookUUID string + body string + addedOn int64 + editedOn int64 + usn int + deleted bool + dirty bool + }{ + { + uuid: "n1-uuid", + bookUUID: "b1-uuid", + body: "n1-body", + addedOn: 1542058875, + editedOn: 0, + usn: 0, + deleted: false, + dirty: false, + }, + { + uuid: "n2-uuid", + bookUUID: "b2-uuid", + body: "n2-body", + addedOn: 1542058875, + editedOn: 1542058876, + usn: 1008, + deleted: true, + dirty: true, + }, + } + + for idx, tc := range testCases { + got := NewNote(tc.uuid, tc.bookUUID, tc.body, tc.addedOn, tc.editedOn, tc.usn, tc.deleted, tc.dirty) + + assert.Equal(t, got.UUID, tc.uuid, fmt.Sprintf("UUID mismatch for test case %d", idx)) + assert.Equal(t, got.BookUUID, tc.bookUUID, fmt.Sprintf("BookUUID mismatch for test case %d", idx)) + assert.Equal(t, got.Body, tc.body, fmt.Sprintf("Body mismatch for test case %d", idx)) + assert.Equal(t, got.AddedOn, tc.addedOn, fmt.Sprintf("AddedOn mismatch for test case %d", idx)) + assert.Equal(t, got.EditedOn, tc.editedOn, fmt.Sprintf("EditedOn mismatch for test case %d", idx)) + assert.Equal(t, got.USN, tc.usn, fmt.Sprintf("USN mismatch for test case %d", idx)) + assert.Equal(t, got.Deleted, tc.deleted, fmt.Sprintf("Deleted mismatch for test case %d", idx)) + assert.Equal(t, got.Dirty, tc.dirty, fmt.Sprintf("Dirty mismatch for test case %d", idx)) + } +} + +func TestNoteInsert(t *testing.T) { + testCases := []struct { + uuid string + bookUUID string + body string + addedOn int64 + editedOn int64 + usn int + deleted bool + dirty bool + }{ + { + uuid: "n1-uuid", + bookUUID: "b1-uuid", + body: "n1-body", + addedOn: 1542058875, + editedOn: 0, + usn: 0, + deleted: false, + dirty: false, + }, + { + uuid: "n2-uuid", + bookUUID: "b2-uuid", + body: "n2-body", + addedOn: 1542058875, + editedOn: 1542058876, + usn: 1008, + deleted: true, + dirty: true, + }, + } + + for idx, tc := range testCases { + func() { + // Setup + db := InitTestMemoryDB(t) + + n := Note{ + UUID: tc.uuid, + BookUUID: tc.bookUUID, + Body: tc.body, + AddedOn: tc.addedOn, + EditedOn: tc.editedOn, + USN: tc.usn, + Deleted: tc.deleted, + Dirty: tc.dirty, + } + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + if err := n.Insert(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var uuid, bookUUID, body string + var addedOn, editedOn int64 + var usn int + var deleted, dirty bool + MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), + &uuid, &bookUUID, &body, &addedOn, &editedOn, &usn, &deleted, &dirty) + + assert.Equal(t, uuid, tc.uuid, fmt.Sprintf("uuid mismatch for test case %d", idx)) + assert.Equal(t, bookUUID, tc.bookUUID, fmt.Sprintf("bookUUID mismatch for test case %d", idx)) + assert.Equal(t, body, tc.body, fmt.Sprintf("body mismatch for test case %d", idx)) + assert.Equal(t, addedOn, tc.addedOn, fmt.Sprintf("addedOn mismatch for test case %d", idx)) + assert.Equal(t, editedOn, tc.editedOn, fmt.Sprintf("editedOn mismatch for test case %d", idx)) + assert.Equal(t, usn, tc.usn, fmt.Sprintf("usn mismatch for test case %d", idx)) + assert.Equal(t, deleted, tc.deleted, fmt.Sprintf("deleted mismatch for test case %d", idx)) + assert.Equal(t, dirty, tc.dirty, fmt.Sprintf("dirty mismatch for test case %d", idx)) + }() + } +} + +func TestNoteUpdate(t *testing.T) { + testCases := []struct { + uuid string + bookUUID string + body string + addedOn int64 + editedOn int64 + usn int + deleted bool + dirty bool + newBookUUID string + newBody string + newEditedOn int64 + newUSN int + newDeleted bool + newDirty bool + }{ + { + uuid: "n1-uuid", + bookUUID: "b1-uuid", + body: "n1-body", + addedOn: 1542058875, + editedOn: 0, + usn: 0, + deleted: false, + dirty: false, + newBookUUID: "b1-uuid", + newBody: "n1-body edited", + newEditedOn: 1542058879, + newUSN: 0, + newDeleted: false, + newDirty: false, + }, + { + uuid: "n1-uuid", + bookUUID: "b1-uuid", + body: "n1-body", + addedOn: 1542058875, + editedOn: 0, + usn: 0, + deleted: false, + dirty: true, + newBookUUID: "b2-uuid", + newBody: "n1-body", + newEditedOn: 1542058879, + newUSN: 0, + newDeleted: false, + newDirty: false, + }, + { + uuid: "n1-uuid", + bookUUID: "b1-uuid", + body: "n1-body", + addedOn: 1542058875, + editedOn: 0, + usn: 10, + deleted: false, + dirty: false, + newBookUUID: "", + newBody: "", + newEditedOn: 1542058879, + newUSN: 151, + newDeleted: true, + newDirty: false, + }, + { + uuid: "n1-uuid", + bookUUID: "b1-uuid", + body: "n1-body", + addedOn: 1542058875, + editedOn: 0, + usn: 0, + deleted: false, + dirty: false, + newBookUUID: "", + newBody: "", + newEditedOn: 1542058879, + newUSN: 15, + newDeleted: true, + newDirty: false, + }, + } + + for idx, tc := range testCases { + func() { + // Setup + db := InitTestMemoryDB(t) + + n1 := Note{ + UUID: tc.uuid, + BookUUID: tc.bookUUID, + Body: tc.body, + AddedOn: tc.addedOn, + EditedOn: tc.editedOn, + USN: tc.usn, + Deleted: tc.deleted, + Dirty: tc.dirty, + } + n2 := Note{ + UUID: "n2-uuid", + BookUUID: "b10-uuid", + Body: "n2 body", + AddedOn: 1542058875, + EditedOn: 0, + USN: 39, + Deleted: false, + Dirty: false, + } + + MustExec(t, fmt.Sprintf("inserting n1 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty) + MustExec(t, fmt.Sprintf("inserting n2 for test case %d", idx), db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + n1.BookUUID = tc.newBookUUID + n1.Body = tc.newBody + n1.EditedOn = tc.newEditedOn + n1.USN = tc.newUSN + n1.Deleted = tc.newDeleted + n1.Dirty = tc.newDirty + + if err := n1.Update(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var n1Record, n2Record Note + MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", tc.uuid), + &n1Record.UUID, &n1Record.BookUUID, &n1Record.Body, &n1Record.AddedOn, &n1Record.EditedOn, &n1Record.USN, &n1Record.Deleted, &n1Record.Dirty) + MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Deleted, &n2Record.Dirty) + + assert.Equal(t, n1Record.UUID, n1.UUID, fmt.Sprintf("n1 uuid mismatch for test case %d", idx)) + assert.Equal(t, n1Record.BookUUID, tc.newBookUUID, fmt.Sprintf("n1 bookUUID mismatch for test case %d", idx)) + assert.Equal(t, n1Record.Body, tc.newBody, fmt.Sprintf("n1 body mismatch for test case %d", idx)) + assert.Equal(t, n1Record.AddedOn, n1.AddedOn, fmt.Sprintf("n1 addedOn mismatch for test case %d", idx)) + assert.Equal(t, n1Record.EditedOn, tc.newEditedOn, fmt.Sprintf("n1 editedOn mismatch for test case %d", idx)) + assert.Equal(t, n1Record.USN, tc.newUSN, fmt.Sprintf("n1 usn mismatch for test case %d", idx)) + assert.Equal(t, n1Record.Deleted, tc.newDeleted, fmt.Sprintf("n1 deleted mismatch for test case %d", idx)) + assert.Equal(t, n1Record.Dirty, tc.newDirty, fmt.Sprintf("n1 dirty mismatch for test case %d", idx)) + + assert.Equal(t, n2Record.UUID, n2.UUID, fmt.Sprintf("n2 uuid mismatch for test case %d", idx)) + assert.Equal(t, n2Record.BookUUID, n2.BookUUID, fmt.Sprintf("n2 bookUUID mismatch for test case %d", idx)) + assert.Equal(t, n2Record.Body, n2.Body, fmt.Sprintf("n2 body mismatch for test case %d", idx)) + assert.Equal(t, n2Record.AddedOn, n2.AddedOn, fmt.Sprintf("n2 addedOn mismatch for test case %d", idx)) + assert.Equal(t, n2Record.EditedOn, n2.EditedOn, fmt.Sprintf("n2 editedOn mismatch for test case %d", idx)) + assert.Equal(t, n2Record.USN, n2.USN, fmt.Sprintf("n2 usn mismatch for test case %d", idx)) + assert.Equal(t, n2Record.Deleted, n2.Deleted, fmt.Sprintf("n2 deleted mismatch for test case %d", idx)) + assert.Equal(t, n2Record.Dirty, n2.Dirty, fmt.Sprintf("n2 dirty mismatch for test case %d", idx)) + }() + } +} + +func TestNoteUpdateUUID(t *testing.T) { + testCases := []struct { + newUUID string + }{ + { + newUUID: "n1-new-uuid", + }, + { + newUUID: "n2-new-uuid", + }, + } + + for idx, tc := range testCases { + t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + n1 := Note{ + UUID: "n1-uuid", + BookUUID: "b1-uuid", + AddedOn: 1542058874, + Body: "n1-body", + USN: 1, + Deleted: true, + Dirty: false, + } + n2 := Note{ + UUID: "n2-uuid", + BookUUID: "b1-uuid", + AddedOn: 1542058874, + Body: "n2-body", + USN: 1, + Deleted: true, + Dirty: false, + } + + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.Body, n1.AddedOn, n1.USN, n1.Deleted, n1.Dirty) + MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.Body, n2.AddedOn, n2.USN, n2.Deleted, n2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + if err := n1.UpdateUUID(tx, tc.newUUID); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var n1Record, n2Record Note + MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, body, usn, deleted, dirty FROM notes WHERE body = ?", "n1-body"), + &n1Record.UUID, &n1Record.Body, &n1Record.USN, &n1Record.Deleted, &n1Record.Dirty) + MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, body, usn, deleted, dirty FROM notes WHERE body = ?", "n2-body"), + &n2Record.UUID, &n2Record.Body, &n2Record.USN, &n2Record.Deleted, &n2Record.Dirty) + + assert.Equal(t, n1.UUID, tc.newUUID, "n1 original reference uuid mismatch") + assert.Equal(t, n1Record.UUID, tc.newUUID, "n1 uuid mismatch") + assert.Equal(t, n2Record.UUID, n2.UUID, "n2 uuid mismatch") + }) + } +} + +func TestNoteExpunge(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + n1 := Note{ + UUID: "n1-uuid", + BookUUID: "b9-uuid", + Body: "n1 body", + AddedOn: 1542058874, + EditedOn: 0, + USN: 22, + Deleted: false, + Dirty: false, + } + n2 := Note{ + UUID: "n2-uuid", + BookUUID: "b10-uuid", + Body: "n2 body", + AddedOn: 1542058875, + EditedOn: 0, + USN: 39, + Deleted: false, + Dirty: false, + } + + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Deleted, n1.Dirty) + MustExec(t, "inserting n2", db, "INSERT INTO notes (uuid, book_uuid, usn, added_on, edited_on, body, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Deleted, n2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := n1.Expunge(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var noteCount int + MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, noteCount, 1, "note count mismatch") + + var n2Record Note + MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty FROM notes WHERE uuid = ?", n2.UUID), + &n2Record.UUID, &n2Record.BookUUID, &n2Record.Body, &n2Record.AddedOn, &n2Record.EditedOn, &n2Record.USN, &n2Record.Deleted, &n2Record.Dirty) + + assert.Equal(t, n2Record.UUID, n2.UUID, "n2 uuid mismatch") + assert.Equal(t, n2Record.BookUUID, n2.BookUUID, "n2 bookUUID mismatch") + assert.Equal(t, n2Record.Body, n2.Body, "n2 body mismatch") + assert.Equal(t, n2Record.AddedOn, n2.AddedOn, "n2 addedOn mismatch") + assert.Equal(t, n2Record.EditedOn, n2.EditedOn, "n2 editedOn mismatch") + assert.Equal(t, n2Record.USN, n2.USN, "n2 usn mismatch") + assert.Equal(t, n2Record.Deleted, n2.Deleted, "n2 deleted mismatch") + assert.Equal(t, n2Record.Dirty, n2.Dirty, "n2 dirty mismatch") +} + +func TestNewBook(t *testing.T) { + testCases := []struct { + uuid string + label string + usn int + deleted bool + dirty bool + }{ + { + uuid: "b1-uuid", + label: "b1-label", + usn: 0, + deleted: false, + dirty: false, + }, + { + uuid: "b2-uuid", + label: "b2-label", + usn: 1008, + deleted: false, + dirty: true, + }, + } + + for idx, tc := range testCases { + got := NewBook(tc.uuid, tc.label, tc.usn, tc.deleted, tc.dirty) + + assert.Equal(t, got.UUID, tc.uuid, fmt.Sprintf("UUID mismatch for test case %d", idx)) + assert.Equal(t, got.Label, tc.label, fmt.Sprintf("Label mismatch for test case %d", idx)) + assert.Equal(t, got.USN, tc.usn, fmt.Sprintf("USN mismatch for test case %d", idx)) + assert.Equal(t, got.Deleted, tc.deleted, fmt.Sprintf("Deleted mismatch for test case %d", idx)) + assert.Equal(t, got.Dirty, tc.dirty, fmt.Sprintf("Dirty mismatch for test case %d", idx)) + } +} + +func TestBookInsert(t *testing.T) { + testCases := []struct { + uuid string + label string + usn int + deleted bool + dirty bool + }{ + { + uuid: "b1-uuid", + label: "b1-label", + usn: 10808, + deleted: false, + dirty: false, + }, + { + uuid: "b1-uuid", + label: "b1-label", + usn: 10808, + deleted: false, + dirty: true, + }, + } + + for idx, tc := range testCases { + func() { + // Setup + db := InitTestMemoryDB(t) + + b := Book{ + UUID: tc.uuid, + Label: tc.label, + USN: tc.usn, + Dirty: tc.dirty, + Deleted: tc.deleted, + } + + // execute + + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + if err := b.Insert(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var uuid, label string + var usn int + var deleted, dirty bool + MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", tc.uuid), + &uuid, &label, &usn, &deleted, &dirty) + + assert.Equal(t, uuid, tc.uuid, fmt.Sprintf("uuid mismatch for test case %d", idx)) + assert.Equal(t, label, tc.label, fmt.Sprintf("label mismatch for test case %d", idx)) + assert.Equal(t, usn, tc.usn, fmt.Sprintf("usn mismatch for test case %d", idx)) + assert.Equal(t, deleted, tc.deleted, fmt.Sprintf("deleted mismatch for test case %d", idx)) + assert.Equal(t, dirty, tc.dirty, fmt.Sprintf("dirty mismatch for test case %d", idx)) + }() + } +} + +func TestBookUpdate(t *testing.T) { + testCases := []struct { + uuid string + label string + usn int + deleted bool + dirty bool + newLabel string + newUSN int + newDeleted bool + newDirty bool + }{ + { + uuid: "b1-uuid", + label: "b1-label", + usn: 0, + deleted: false, + dirty: false, + newLabel: "b1-label-edited", + newUSN: 0, + newDeleted: false, + newDirty: true, + }, + { + uuid: "b1-uuid", + label: "b1-label", + usn: 0, + deleted: false, + dirty: false, + newLabel: "", + newUSN: 10, + newDeleted: true, + newDirty: false, + }, + } + + for idx, tc := range testCases { + func() { + // Setup + db := InitTestMemoryDB(t) + + b1 := Book{ + UUID: "b1-uuid", + Label: "b1-label", + USN: 1, + Deleted: true, + Dirty: false, + } + b2 := Book{ + UUID: "b2-uuid", + Label: "b2-label", + USN: 1, + Deleted: true, + Dirty: false, + } + + MustExec(t, fmt.Sprintf("inserting b1 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1.UUID, b1.Label, b1.USN, b1.Deleted, b1.Dirty) + MustExec(t, fmt.Sprintf("inserting b2 for test case %d", idx), db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2.UUID, b2.Label, b2.USN, b2.Deleted, b2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, fmt.Sprintf("beginning a transaction for test case %d", idx)).Error()) + } + + b1.Label = tc.newLabel + b1.USN = tc.newUSN + b1.Deleted = tc.newDeleted + b1.Dirty = tc.newDirty + + if err := b1.Update(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, fmt.Sprintf("executing for test case %d", idx)).Error()) + } + + tx.Commit() + + // test + var b1Record, b2Record Book + MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", tc.uuid), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Deleted, &b1Record.Dirty) + MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", b2.UUID), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Deleted, &b2Record.Dirty) + + assert.Equal(t, b1Record.UUID, b1.UUID, fmt.Sprintf("b1 uuid mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Label, tc.newLabel, fmt.Sprintf("b1 label mismatch for test case %d", idx)) + assert.Equal(t, b1Record.USN, tc.newUSN, fmt.Sprintf("b1 usn mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Deleted, tc.newDeleted, fmt.Sprintf("b1 deleted mismatch for test case %d", idx)) + assert.Equal(t, b1Record.Dirty, tc.newDirty, fmt.Sprintf("b1 dirty mismatch for test case %d", idx)) + + assert.Equal(t, b2Record.UUID, b2.UUID, fmt.Sprintf("b2 uuid mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Label, b2.Label, fmt.Sprintf("b2 label mismatch for test case %d", idx)) + assert.Equal(t, b2Record.USN, b2.USN, fmt.Sprintf("b2 usn mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Deleted, b2.Deleted, fmt.Sprintf("b2 deleted mismatch for test case %d", idx)) + assert.Equal(t, b2Record.Dirty, b2.Dirty, fmt.Sprintf("b2 dirty mismatch for test case %d", idx)) + }() + } +} + +func TestBookUpdateUUID(t *testing.T) { + testCases := []struct { + newUUID string + }{ + { + newUUID: "b1-new-uuid", + }, + { + newUUID: "b2-new-uuid", + }, + } + + for idx, tc := range testCases { + t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) { + + // Setup + db := InitTestMemoryDB(t) + + b1 := Book{ + UUID: "b1-uuid", + Label: "b1-label", + USN: 1, + Deleted: true, + Dirty: false, + } + b2 := Book{ + UUID: "b2-uuid", + Label: "b2-label", + USN: 1, + Deleted: true, + Dirty: false, + } + + MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1.UUID, b1.Label, b1.USN, b1.Deleted, b1.Dirty) + MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2.UUID, b2.Label, b2.USN, b2.Deleted, b2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + if err := b1.UpdateUUID(tx, tc.newUUID); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var b1Record, b2Record Book + MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE label = ?", "b1-label"), + &b1Record.UUID, &b1Record.Label, &b1Record.USN, &b1Record.Deleted, &b1Record.Dirty) + MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE label = ?", "b2-label"), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Deleted, &b2Record.Dirty) + + assert.Equal(t, b1.UUID, tc.newUUID, "b1 original reference uuid mismatch") + assert.Equal(t, b1Record.UUID, tc.newUUID, "b1 uuid mismatch") + assert.Equal(t, b2Record.UUID, b2.UUID, "b2 uuid mismatch") + }) + } +} + +func TestBookExpunge(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + b1 := Book{ + UUID: "b1-uuid", + Label: "b1-label", + USN: 1, + Deleted: true, + Dirty: false, + } + b2 := Book{ + UUID: "b2-uuid", + Label: "b2-label", + USN: 1, + Deleted: true, + Dirty: false, + } + + MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1.UUID, b1.Label, b1.USN, b1.Deleted, b1.Dirty) + MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2.UUID, b2.Label, b2.USN, b2.Deleted, b2.Dirty) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := b1.Expunge(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing").Error()) + } + + tx.Commit() + + // test + var bookCount int + MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + + assert.Equalf(t, bookCount, 1, "book count mismatch") + + var b2Record Book + MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, deleted, dirty FROM books WHERE uuid = ?", "b2-uuid"), + &b2Record.UUID, &b2Record.Label, &b2Record.USN, &b2Record.Deleted, &b2Record.Dirty) + + assert.Equal(t, b2Record.UUID, b2.UUID, "b2 uuid mismatch") + assert.Equal(t, b2Record.Label, b2.Label, "b2 label mismatch") + assert.Equal(t, b2Record.USN, b2.USN, "b2 usn mismatch") + assert.Equal(t, b2Record.Deleted, b2.Deleted, "b2 deleted mismatch") + assert.Equal(t, b2Record.Dirty, b2.Dirty, "b2 dirty mismatch") +} + +// TestNoteFTS tests that note full text search indices stay in sync with the notes after insert, update and delete +func TestNoteFTS(t *testing.T) { + // set up + db := InitTestMemoryDB(t) + + // execute - insert + n := Note{ + UUID: "n1-uuid", + BookUUID: "b1-uuid", + Body: "foo bar", + AddedOn: 1542058875, + EditedOn: 0, + USN: 0, + Deleted: false, + Dirty: false, + } + + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := n.Insert(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "inserting").Error()) + } + + tx.Commit() + + // test + var noteCount, noteFtsCount, noteSearchCount int + MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) + MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "foo"), ¬eSearchCount) + + assert.Equal(t, noteCount, 1, "noteCount mismatch") + assert.Equal(t, noteFtsCount, 1, "noteFtsCount mismatch") + assert.Equal(t, noteSearchCount, 1, "noteSearchCount mismatch") + + // execute - update + tx, err = db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + n.Body = "baz quz" + if err := n.Update(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "updating").Error()) + } + + tx.Commit() + + // test + MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) + assert.Equal(t, noteCount, 1, "noteCount mismatch") + assert.Equal(t, noteFtsCount, 1, "noteFtsCount mismatch") + + MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "foo"), ¬eSearchCount) + assert.Equal(t, noteSearchCount, 0, "noteSearchCount for foo mismatch") + MustScan(t, "counting search results", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "baz"), ¬eSearchCount) + assert.Equal(t, noteSearchCount, 1, "noteSearchCount for baz mismatch") + + // execute - delete + tx, err = db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := n.Expunge(tx); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "expunging").Error()) + } + + tx.Commit() + + // test + MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts"), ¬eFtsCount) + + assert.Equal(t, noteCount, 0, "noteCount mismatch") + assert.Equal(t, noteFtsCount, 0, "noteFtsCount mismatch") +} diff --git a/pkg/cli/database/queries.go b/pkg/cli/database/queries.go new file mode 100644 index 00000000..2209c8f3 --- /dev/null +++ b/pkg/cli/database/queries.go @@ -0,0 +1,219 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import ( + "database/sql" + + "github.com/dnote/dnote/pkg/clock" + "github.com/pkg/errors" +) + +// GetSystem scans the given system configuration record onto the destination +func GetSystem(db *DB, key string, dest interface{}) error { + if err := db.QueryRow("SELECT value FROM system WHERE key = ?", key).Scan(dest); err != nil { + return errors.Wrap(err, "finding system configuration record") + } + + return nil +} + +// InsertSystem inserets a system configuration +func InsertSystem(db *DB, key, val string) error { + if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", key, val); err != nil { + return errors.Wrap(err, "saving system config") + } + + return nil +} + +// UpsertSystem inserts or updates a system configuration +func UpsertSystem(db *DB, key, val string) error { + var count int + if err := db.QueryRow("SELECT count(*) FROM system WHERE key = ?", key).Scan(&count); err != nil { + return errors.Wrap(err, "counting system record") + } + + if count == 0 { + if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", key, val); err != nil { + return errors.Wrap(err, "saving system config") + } + } else { + if _, err := db.Exec("UPDATE system SET value = ? WHERE key = ?", val, key); err != nil { + return errors.Wrap(err, "updating system config") + } + } + + return nil +} + +// UpdateSystem updates a system configuration +func UpdateSystem(db *DB, key, val interface{}) error { + if _, err := db.Exec("UPDATE system SET value = ? WHERE key = ?", val, key); err != nil { + return errors.Wrap(err, "updating system config") + } + + return nil +} + +// DeleteSystem delets the given system record +func DeleteSystem(db *DB, key string) error { + if _, err := db.Exec("DELETE FROM system WHERE key = ?", key); err != nil { + return errors.Wrap(err, "deleting system config") + } + + return nil +} + +// NoteInfo is a basic information about a note +type NoteInfo struct { + RowID int + BookLabel string + UUID string + Content string + AddedOn int64 + EditedOn int64 +} + +// GetNoteInfo returns a NoteInfo for the note with the given noteRowID +func GetNoteInfo(db *DB, noteRowID int) (NoteInfo, error) { + var ret NoteInfo + + err := db.QueryRow(`SELECT books.label, notes.uuid, notes.body, notes.added_on, notes.edited_on, notes.rowid + FROM notes + INNER JOIN books ON books.uuid = notes.book_uuid + WHERE notes.rowid = ? AND notes.deleted = false`, noteRowID). + Scan(&ret.BookLabel, &ret.UUID, &ret.Content, &ret.AddedOn, &ret.EditedOn, &ret.RowID) + if err == sql.ErrNoRows { + return ret, errors.Errorf("note %d not found", noteRowID) + } else if err != nil { + return ret, errors.Wrap(err, "querying the note") + } + + return ret, nil +} + +// BookInfo is a basic information about a book +type BookInfo struct { + RowID int + UUID string + Name string +} + +// GetBookInfo returns a BookInfo for the book with the given uuid +func GetBookInfo(db *DB, uuid string) (BookInfo, error) { + var ret BookInfo + + err := db.QueryRow(`SELECT books.rowid, books.uuid, books.label + FROM books + WHERE books.uuid = ? AND books.deleted = false`, uuid). + Scan(&ret.RowID, &ret.UUID, &ret.Name) + if err == sql.ErrNoRows { + return ret, errors.Errorf("book %s not found", uuid) + } else if err != nil { + return ret, errors.Wrap(err, "querying the note") + } + + return ret, nil +} + +// GetBookUUID returns a uuid of a book given a label +func GetBookUUID(db *DB, label string) (string, error) { + var ret string + err := db.QueryRow("SELECT uuid FROM books WHERE label = ?", label).Scan(&ret) + if err == sql.ErrNoRows { + return ret, errors.Errorf("book '%s' not found", label) + } else if err != nil { + return ret, errors.Wrap(err, "querying the book") + } + + return ret, nil +} + +// UpdateBookName updates a book name +func UpdateBookName(db *DB, uuid string, name string) error { + _, err := db.Exec(`UPDATE books + SET label = ?, dirty = ? + WHERE uuid = ?`, name, true, uuid) + if err != nil { + return errors.Wrap(err, "updating the book") + } + + return nil +} + +// GetActiveNote gets the note which has the given rowid and is not deleted +func GetActiveNote(db *DB, rowid int) (Note, error) { + var ret Note + + err := db.QueryRow(`SELECT + rowid, + uuid, + book_uuid, + body, + added_on, + edited_on, + usn, + deleted, + dirty + FROM notes WHERE rowid = ? AND deleted = false;`, rowid).Scan( + &ret.RowID, + &ret.UUID, + &ret.BookUUID, + &ret.Body, + &ret.AddedOn, + &ret.EditedOn, + &ret.USN, + &ret.Deleted, + &ret.Dirty, + ) + + if err == sql.ErrNoRows { + return ret, err + } else if err != nil { + return ret, errors.Wrap(err, "finding the note") + } + + return ret, nil +} + +// UpdateNoteContent updates the note content and marks the note as dirty +func UpdateNoteContent(db *DB, c clock.Clock, rowID int, content string) error { + ts := c.Now().UnixNano() + + _, err := db.Exec(`UPDATE notes + SET body = ?, edited_on = ?, dirty = ? + WHERE rowid = ?`, content, ts, true, rowID) + if err != nil { + return errors.Wrap(err, "updating the note") + } + + return nil +} + +// UpdateNoteBook moves the note to a different book and marks the note as dirty +func UpdateNoteBook(db *DB, c clock.Clock, rowID int, bookUUID string) error { + ts := c.Now().UnixNano() + + _, err := db.Exec(`UPDATE notes + SET book_uuid = ?, edited_on = ?, dirty = ? + WHERE rowid = ?`, bookUUID, ts, true, rowID) + if err != nil { + return errors.Wrap(err, "updating the note") + } + + return nil +} diff --git a/pkg/cli/database/queries_test.go b/pkg/cli/database/queries_test.go new file mode 100644 index 00000000..e394fe95 --- /dev/null +++ b/pkg/cli/database/queries_test.go @@ -0,0 +1,370 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import ( + "database/sql" + "fmt" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/clock" + "github.com/pkg/errors" +) + +func TestInsertSystem(t *testing.T) { + testCases := []struct { + key string + val string + }{ + { + key: "foo", + val: "1558089284", + }, + { + key: "baz", + val: "quz", + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := InsertSystem(tx, tc.key, tc.val); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing for test case").Error()) + } + + tx.Commit() + + // test + var key, val string + MustScan(t, "getting the saved record", + db.QueryRow("SELECT key, value FROM system WHERE key = ?", tc.key), &key, &val) + + assert.Equal(t, key, tc.key, "key mismatch for test case") + assert.Equal(t, val, tc.val, "val mismatch for test case") + }) + } +} + +func TestUpsertSystem(t *testing.T) { + testCases := []struct { + key string + val string + countDelta int + }{ + { + key: "foo", + val: "1558089284", + countDelta: 1, + }, + { + key: "baz", + val: "quz2", + countDelta: 0, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz") + + var initialSystemCount int + MustScan(t, "counting records", db.QueryRow("SELECT count(*) FROM system"), &initialSystemCount) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := UpsertSystem(tx, tc.key, tc.val); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing for test case").Error()) + } + + tx.Commit() + + // test + var key, val string + MustScan(t, "getting the saved record", + db.QueryRow("SELECT key, value FROM system WHERE key = ?", tc.key), &key, &val) + var systemCount int + MustScan(t, "counting records", + db.QueryRow("SELECT count(*) FROM system"), &systemCount) + + assert.Equal(t, key, tc.key, "key mismatch") + assert.Equal(t, val, tc.val, "val mismatch") + assert.Equal(t, systemCount, initialSystemCount+tc.countDelta, "count mismatch") + }) + } +} + +func TestGetSystem(t *testing.T) { + t.Run(fmt.Sprintf("get string value"), func(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + // execute + MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "bar") + + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + var dest string + if err := GetSystem(tx, "foo", &dest); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing for test case").Error()) + } + tx.Commit() + + // test + assert.Equal(t, dest, "bar", "dest mismatch") + }) + + t.Run(fmt.Sprintf("get int64 value"), func(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + // execute + MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", 1234) + + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + var dest int64 + if err := GetSystem(tx, "foo", &dest); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing for test case").Error()) + } + tx.Commit() + + // test + assert.Equal(t, dest, int64(1234), "dest mismatch") + }) +} + +func TestUpdateSystem(t *testing.T) { + testCases := []struct { + key string + val string + countDelta int + }{ + { + key: "foo", + val: "1558089284", + }, + { + key: "foo", + val: "bar", + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("update %s %s", tc.key, tc.val), func(t *testing.T) { + // Setup + db := InitTestMemoryDB(t) + + MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "fuz") + MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz") + + var initialSystemCount int + MustScan(t, "counting records", db.QueryRow("SELECT count(*) FROM system"), &initialSystemCount) + + // execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction").Error()) + } + + if err := UpdateSystem(tx, tc.key, tc.val); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing for test case").Error()) + } + + tx.Commit() + + // test + var key, val string + MustScan(t, "getting the saved record", + db.QueryRow("SELECT key, value FROM system WHERE key = ?", tc.key), &key, &val) + var systemCount int + MustScan(t, "counting records", + db.QueryRow("SELECT count(*) FROM system"), &systemCount) + + assert.Equal(t, key, tc.key, "key mismatch") + assert.Equal(t, val, tc.val, "val mismatch") + assert.Equal(t, systemCount, initialSystemCount, "count mismatch") + }) + } +} + +func TestGetActiveNote(t *testing.T) { + t.Run("not deleted", func(t *testing.T) { + // set up + db := InitTestMemoryDB(t) + + n1UUID := "n1-uuid" + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, false, true) + + var n1RowID int + MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", n1UUID), &n1RowID) + + // execute + got, err := GetActiveNote(db, n1RowID) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + // test + assert.Equal(t, got.RowID, n1RowID, "RowID mismatch") + assert.Equal(t, got.UUID, n1UUID, "UUID mismatch") + assert.Equal(t, got.BookUUID, "b1-uuid", "BookUUID mismatch") + assert.Equal(t, got.Body, "n1 content", "Body mismatch") + assert.Equal(t, got.AddedOn, int64(1542058875), "AddedOn mismatch") + assert.Equal(t, got.EditedOn, int64(1542058876), "EditedOn mismatch") + assert.Equal(t, got.USN, 1, "USN mismatch") + assert.Equal(t, got.Deleted, false, "Deleted mismatch") + assert.Equal(t, got.Dirty, true, "Dirty mismatch") + }) + + t.Run("deleted", func(t *testing.T) { + // set up + db := InitTestMemoryDB(t) + + n1UUID := "n1-uuid" + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, true) + + var n1RowID int + MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", n1UUID), &n1RowID) + + // execute + _, err := GetActiveNote(db, n1RowID) + + // test + if err == nil { + t.Error("Should have returned an error") + } + if err != nil && err != sql.ErrNoRows { + t.Error(errors.Wrap(err, "executing")) + } + }) +} + +func TestUpdateNoteContent(t *testing.T) { + // set up + db := InitTestMemoryDB(t) + + uuid := "n1-uuid" + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", uuid, "b1-uuid", "n1 content", 1542058875, 0, 1, false, false) + + var rowid int + MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", uuid), &rowid) + + // execute + c := clock.NewMock() + now := time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC) + c.SetNow(now) + + err := UpdateNoteContent(db, c, rowid, "n1 content updated") + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + var content string + var editedOn int + var dirty bool + + MustScan(t, "getting the note record", db.QueryRow("SELECT body, edited_on, dirty FROM notes WHERE rowid = ?", rowid), &content, &editedOn, &dirty) + + assert.Equal(t, content, "n1 content updated", "content mismatch") + assert.Equal(t, int64(editedOn), now.UnixNano(), "editedOn mismatch") + assert.Equal(t, dirty, true, "dirty mismatch") +} + +func TestUpdateNoteBook(t *testing.T) { + // set up + db := InitTestMemoryDB(t) + + b1UUID := "b1-uuid" + b2UUID := "b2-uuid" + MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 8, false, false) + MustExec(t, "inserting b2", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b2UUID, "b2-label", 9, false, false) + + uuid := "n1-uuid" + MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", uuid, b1UUID, "n1 content", 1542058875, 0, 1, false, false) + + var rowid int + MustScan(t, "getting rowid", db.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", uuid), &rowid) + + // execute + c := clock.NewMock() + now := time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC) + c.SetNow(now) + + err := UpdateNoteBook(db, c, rowid, b2UUID) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + var bookUUID string + var editedOn int + var dirty bool + + MustScan(t, "getting the note record", db.QueryRow("SELECT book_uuid, edited_on, dirty FROM notes WHERE rowid = ?", rowid), &bookUUID, &editedOn, &dirty) + + assert.Equal(t, bookUUID, b2UUID, "content mismatch") + assert.Equal(t, int64(editedOn), now.UnixNano(), "editedOn mismatch") + assert.Equal(t, dirty, true, "dirty mismatch") +} + +func TestUpdateBookName(t *testing.T) { + // set up + db := InitTestMemoryDB(t) + + b1UUID := "b1-uuid" + MustExec(t, "inserting b1", db, "INSERT INTO books (uuid, label, usn, deleted, dirty) VALUES (?, ?, ?, ?, ?)", b1UUID, "b1-label", 8, false, false) + + // execute + err := UpdateBookName(db, b1UUID, "b1-label-edited") + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + // test + var b1 Book + MustScan(t, "getting the note record", db.QueryRow("SELECT uuid, label, dirty, usn, deleted FROM books WHERE uuid = ?", b1UUID), &b1.UUID, &b1.Label, &b1.Dirty, &b1.USN, &b1.Deleted) + assert.Equal(t, b1.UUID, b1UUID, "UUID mismatch") + assert.Equal(t, b1.Label, "b1-label-edited", "Label mismatch") + assert.Equal(t, b1.Dirty, true, "Dirty mismatch") + assert.Equal(t, b1.USN, 8, "USN mismatch") + assert.Equal(t, b1.Deleted, false, "Deleted mismatch") +} diff --git a/pkg/cli/database/schema.sql b/pkg/cli/database/schema.sql new file mode 100644 index 00000000..9a9de094 --- /dev/null +++ b/pkg/cli/database/schema.sql @@ -0,0 +1,40 @@ +-- This is the final state of the CLI database after all migrations. +-- Auto-generated by generate-schema.go. Do not edit manually. +CREATE TABLE books + ( + uuid text PRIMARY KEY, + label text NOT NULL + , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); +CREATE TABLE system + ( + key string NOT NULL, + value text NOT NULL + ); +CREATE UNIQUE INDEX idx_books_label ON books(label); +CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); +CREATE TABLE "notes" + ( + uuid text NOT NULL, + book_uuid text NOT NULL, + body text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + dirty bool DEFAULT false, + usn int DEFAULT 0 NOT NULL, + deleted bool DEFAULT false + ); +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'"); +CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + END; +CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; + +-- Migration version data. +INSERT INTO system (key, value) VALUES ('schema', 14); +INSERT INTO system (key, value) VALUES ('remote_schema', 1); diff --git a/pkg/cli/database/schema/main.go b/pkg/cli/database/schema/main.go new file mode 100644 index 00000000..6c75e4d0 --- /dev/null +++ b/pkg/cli/database/schema/main.go @@ -0,0 +1,163 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Command schema generates the CLI database schema.sql file. +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/dnote/dnote/pkg/cli/config" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/migrate" +) + +func main() { + tmpDir, err := os.MkdirTemp("", "dnote-schema-gen-*") + if err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } + defer os.RemoveAll(tmpDir) + + schemaPath := filepath.Join("pkg", "cli", "database", "schema.sql") + + if err := run(tmpDir, schemaPath); err != nil { + fmt.Fprintf(os.Stderr, "Error: %v\n", err) + os.Exit(1) + } +} + +func run(tmpDir, outputPath string) error { + schema, err := generateSchema(tmpDir) + if err != nil { + return err + } + + if err := os.WriteFile(outputPath, []byte(schema), 0644); err != nil { + return fmt.Errorf("writing schema file: %w", err) + } + + fmt.Printf("Schema generated successfully at %s\n", outputPath) + return nil +} + +// generateSchema creates a fresh database, runs all migrations, and extracts the schema +func generateSchema(tmpDir string) (string, error) { + // Create dnote directory structure in temp dir + dnoteDir := filepath.Join(tmpDir, "dnote") + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + return "", fmt.Errorf("creating dnote dir: %w", err) + } + + // Use a file-based database + dbPath := filepath.Join(tmpDir, "schema.db") + + // Create context + ctx := context.DnoteCtx{ + Paths: context.Paths{ + Home: tmpDir, + Config: tmpDir, + Data: tmpDir, + Cache: tmpDir, + }, + Version: "schema-gen", + } + + // Open database + db, err := database.Open(dbPath) + if err != nil { + return "", fmt.Errorf("opening database: %w", err) + } + defer db.Close() + ctx.DB = db + + // Initialize database with base tables + if err := infra.InitDB(ctx); err != nil { + return "", fmt.Errorf("initializing database: %w", err) + } + + // Initialize system data + if err := infra.InitSystem(ctx); err != nil { + return "", fmt.Errorf("initializing system: %w", err) + } + + // Create minimal config file + if err := config.Write(ctx, config.Config{}); err != nil { + return "", fmt.Errorf("writing initial config: %w", err) + } + + // Run all local migrations + if err := migrate.Run(ctx, migrate.LocalSequence, migrate.LocalMode); err != nil { + return "", fmt.Errorf("running migrations: %w", err) + } + + // Extract schema before closing database + schema, err := extractSchema(db) + if err != nil { + return "", fmt.Errorf("extracting schema: %w", err) + } + + // Add INSERT statements for migration versions. + systemData := "\n-- Migration version data.\n" + systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemSchema, len(migrate.LocalSequence)) + systemData += fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s', %d);\n", consts.SystemRemoteSchema, len(migrate.RemoteSequence)) + + return schema + systemData, nil +} + +// extractSchema extracts the complete schema by querying sqlite_master +func extractSchema(db *database.DB) (string, error) { + // Query sqlite_master for all schema objects, excluding FTS shadow tables + // FTS shadow tables are internal tables automatically created by FTS virtual tables + rows, err := db.Conn.Query(`SELECT sql FROM sqlite_master + WHERE sql IS NOT NULL + AND name NOT LIKE 'sqlite_%' + AND (type != 'table' + OR (type = 'table' AND name NOT IN ( + SELECT m1.name FROM sqlite_master m1 + JOIN sqlite_master m2 ON m1.name LIKE m2.name || '_%' + WHERE m2.type = 'table' AND m2.sql LIKE '%VIRTUAL TABLE%' + )))`) + if err != nil { + return "", fmt.Errorf("querying sqlite_master: %w", err) + } + defer rows.Close() + + var schemas []string + for rows.Next() { + var sql string + if err := rows.Scan(&sql); err != nil { + return "", fmt.Errorf("scanning row: %w", err) + } + schemas = append(schemas, sql) + } + + if err := rows.Err(); err != nil { + return "", fmt.Errorf("iterating rows: %w", err) + } + + // Add autogenerated header comment + header := `-- This is the final state of the CLI database after all migrations. +-- Auto-generated by generate-schema.go. Do not edit manually. +` + return header + strings.Join(schemas, ";\n") + ";\n", nil +} diff --git a/pkg/cli/database/schema/main_test.go b/pkg/cli/database/schema/main_test.go new file mode 100644 index 00000000..26898ef3 --- /dev/null +++ b/pkg/cli/database/schema/main_test.go @@ -0,0 +1,81 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" +) + +func TestRun(t *testing.T) { + tmpDir := t.TempDir() + outputPath := filepath.Join(tmpDir, "schema.sql") + + // Run the function + if err := run(tmpDir, outputPath); err != nil { + t.Fatalf("run() failed: %v", err) + } + + // Verify schema.sql was created + content, err := os.ReadFile(outputPath) + if err != nil { + t.Fatalf("reading schema.sql: %v", err) + } + + schema := string(content) + + // Verify it has the header + assert.Equal(t, strings.HasPrefix(schema, "-- This is the final state"), true, "schema.sql should have header comment") + + // Verify schema contains expected tables + expectedTables := []string{ + "CREATE TABLE books", + "CREATE TABLE system", + "CREATE TABLE \"notes\"", + "CREATE VIRTUAL TABLE note_fts", + } + + for _, expected := range expectedTables { + assert.Equal(t, strings.Contains(schema, expected), true, fmt.Sprintf("schema should contain %s", expected)) + } + + // Verify schema contains triggers + expectedTriggers := []string{ + "CREATE TRIGGER notes_after_insert", + "CREATE TRIGGER notes_after_delete", + "CREATE TRIGGER notes_after_update", + } + + for _, expected := range expectedTriggers { + assert.Equal(t, strings.Contains(schema, expected), true, fmt.Sprintf("schema should contain %s", expected)) + } + + // Verify schema does not contain sqlite internal tables + assert.Equal(t, strings.Contains(schema, "sqlite_sequence"), false, "schema should not contain sqlite_sequence") + + // Verify system key-value pairs for schema versions are present + expectedSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemSchema) + assert.Equal(t, strings.Contains(schema, expectedSchemaKey), true, "schema should contain schema version INSERT statement") + + expectedRemoteSchemaKey := fmt.Sprintf("INSERT INTO system (key, value) VALUES ('%s',", consts.SystemRemoteSchema) + assert.Equal(t, strings.Contains(schema, expectedRemoteSchemaKey), true, "schema should contain remote_schema version INSERT statement") +} diff --git a/cli/infra/sql.go b/pkg/cli/database/sql.go similarity index 66% rename from cli/infra/sql.go rename to pkg/cli/database/sql.go index 17327e27..0af3c7fd 100644 --- a/cli/infra/sql.go +++ b/pkg/cli/database/sql.go @@ -1,27 +1,26 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package infra +package database import ( "database/sql" "github.com/pkg/errors" + // use sqlite + _ "github.com/mattn/go-sqlite3" ) // SQLCommon is the minimal interface required by a db connection @@ -45,27 +44,8 @@ type sqlTx interface { // DB contains information about the current database connection type DB struct { - Conn SQLCommon -} - -// OpenDB initializes a new connection to the sqlite database -func OpenDB(dbPath string) (*DB, error) { - dbConn, err := sql.Open("sqlite3", dbPath) - if err != nil { - return nil, errors.Wrap(err, "opening db connection") - } - - // Send a ping to ensure that the connection is established - // if err := dbConn.Ping(); err != nil { - // dbConn.Close() - // return nil, errors.Wrap(err, "ping") - // } - - db := &DB{ - Conn: dbConn, - } - - return db, nil + Conn SQLCommon + Filepath string } // Begin begins a transaction @@ -85,9 +65,7 @@ func (d *DB) Begin() (*DB, error) { // Commit commits a transaction func (d *DB) Commit() error { if db, ok := d.Conn.(sqlTx); ok && db != nil { - if err := db.Commit(); err != nil { - return err - } + return db.Commit() } return errors.New("invalid transaction") @@ -136,3 +114,18 @@ func (d *DB) Close() error { return errors.New("can't close db") } + +// Open initializes a new connection to the sqlite database +func Open(dbPath string) (*DB, error) { + dbConn, err := sql.Open("sqlite3", dbPath) + if err != nil { + return nil, errors.Wrap(err, "opening db connection") + } + + db := &DB{ + Conn: dbConn, + Filepath: dbPath, + } + + return db, nil +} diff --git a/pkg/cli/database/testutils.go b/pkg/cli/database/testutils.go new file mode 100644 index 00000000..23633d53 --- /dev/null +++ b/pkg/cli/database/testutils.go @@ -0,0 +1,129 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import ( + "database/sql" + _ "embed" + "fmt" + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +//go:embed schema.sql +var defaultSchemaSQL string + +// GetDefaultSchemaSQL returns the default schema SQL for tests +func GetDefaultSchemaSQL() string { + return defaultSchemaSQL +} + +// MustScan scans the given row and fails a test in case of any errors +func MustScan(t *testing.T, message string, row *sql.Row, args ...interface{}) { + err := row.Scan(args...) + if err != nil { + t.Fatal(errors.Wrap(errors.Wrap(err, "scanning a row"), message)) + } +} + +// MustExec executes the given SQL query and fails a test if an error occurs +func MustExec(t *testing.T, message string, db *DB, query string, args ...interface{}) sql.Result { + result, err := db.Exec(query, args...) + if err != nil { + t.Fatal(errors.Wrap(errors.Wrap(err, "executing sql"), message)) + } + + return result +} + +// InitTestMemoryDB initializes an in-memory test database with the default schema. +func InitTestMemoryDB(t *testing.T) *DB { + return InitTestMemoryDBRaw(t, "") +} + +// InitTestFileDB initializes a file-based test database with the default schema. +func InitTestFileDB(t *testing.T) (*DB, string) { + uuid := mustGenerateTestUUID(t) + dbPath := filepath.Join(t.TempDir(), fmt.Sprintf("dnote-%s.db", uuid)) + db := InitTestFileDBRaw(t, dbPath) + return db, dbPath +} + +// InitTestFileDBRaw initializes a file-based test database at the specified path with the default schema. +func InitTestFileDBRaw(t *testing.T, dbPath string) *DB { + db, err := Open(dbPath) + if err != nil { + t.Fatal(errors.Wrap(err, "opening database")) + } + + if _, err := db.Exec(defaultSchemaSQL); err != nil { + t.Fatal(errors.Wrap(err, "running schema sql")) + } + + t.Cleanup(func() { db.Close() }) + return db +} + +// InitTestMemoryDBRaw initializes an in-memory test database without marking migrations complete. +// If schemaPath is empty, uses the default schema. Used for migration testing. +func InitTestMemoryDBRaw(t *testing.T, schemaPath string) *DB { + uuid := mustGenerateTestUUID(t) + dbName := fmt.Sprintf("file:%s?mode=memory&cache=shared", uuid) + + db, err := Open(dbName) + if err != nil { + t.Fatal(errors.Wrap(err, "opening in-memory database")) + } + + var schemaSQL string + if schemaPath != "" { + schemaSQL = string(utils.ReadFileAbs(schemaPath)) + } else { + schemaSQL = defaultSchemaSQL + } + + if _, err := db.Exec(schemaSQL); err != nil { + t.Fatal(errors.Wrap(err, "running schema sql")) + } + + t.Cleanup(func() { db.Close() }) + return db +} + +// OpenTestDB opens the database connection to a test database +// without initializing any schema +func OpenTestDB(t *testing.T, dnoteDir string) *DB { + dbPath := fmt.Sprintf("%s/%s/%s", dnoteDir, consts.DnoteDirName, consts.DnoteDBFileName) + db, err := Open(dbPath) + if err != nil { + t.Fatal(errors.Wrap(err, "opening database connection to the test database")) + } + + return db +} + +// mustGenerateTestUUID generates a UUID for test databases and fails the test on error +func mustGenerateTestUUID(t *testing.T) string { + uuid, err := utils.GenerateUUID() + if err != nil { + t.Fatal(errors.Wrap(err, "generating UUID for test database")) + } + return uuid +} diff --git a/pkg/cli/dnote-completion.bash b/pkg/cli/dnote-completion.bash new file mode 100644 index 00000000..34a996b4 --- /dev/null +++ b/pkg/cli/dnote-completion.bash @@ -0,0 +1,48 @@ +#/usr/bin/env bash +__dnote_debug() +{ + if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then + echo "$*" >> "${BASH_COMP_DEBUG_FILE}" + fi +} + +# commands are the valid commands +commands=("add" "view" "edit" "remove" "find" "sync" "login" "logout" "help" "version") + +_complete_root_command() { + COMPREPLY=($(compgen -W "${commands[*]}" "${current_word}")) +} + +_complete_view_command() { + names=$(dnote view --name-only) + query=${COMP_WORDS[*]:2} + + __dnote_debug "${FUNCNAME[0]} query: ${query}" + + while read -r line; do + if [[ ${line} == *"${query}"* ]]; then + COMPREPLY+=("${line}") + fi + done <<< "$names" +} + +_dnote_completions() { + local current_word="${COMP_WORDS[${COMP_CWORD}]}" + + __dnote_debug "COMP_WORDS: ${COMP_WORDS[*]} COMP_CWORD: ${COMP_CWORD} current_word: ${current_word}" + + if [[ "${COMP_CWORD}" -eq 1 ]]; then + _complete_root_command + elif [[ "${COMP_CWORD}" -ge 2 ]]; then + cmd=${COMP_WORDS[1]} + + __dnote_debug "cmd: ${cmd}" + + if [[ ( "${cmd}" == view ) || ( "${cmd}" == v ) || ( "${cmd}" == add ) || ( "${cmd}" == a ) ]]; then + _complete_view_command + fi + fi + +} + +complete -F _dnote_completions dnote diff --git a/pkg/cli/dnote-completion.zsh b/pkg/cli/dnote-completion.zsh new file mode 100644 index 00000000..c8b33486 --- /dev/null +++ b/pkg/cli/dnote-completion.zsh @@ -0,0 +1,39 @@ +#compdef dnote + +local -a _1st_arguments + +_1st_arguments=( + 'add:add a new note' + 'view:list books, notes, or view a content' + 'edit:edit a note or a book' + 'remove:remove a note or a book' + 'find:find notes by keywords' + 'sync:sync data with the server' + 'login:login to the dnote server' + 'logout:logout from the dnote server' + 'version:print the current version' + 'help:get help about any command' +) + +get_booknames() { + local names=$(dnote view --name-only) + local -a ret + + while read -r line; do + ret+=("${line}") + done <<< "$names" + + echo "$ret" +} + +if (( CURRENT == 2 )); then + _describe -t commands "dnote subcommand" _1st_arguments + return +elif (( CURRENT == 3 )); then + case "$words[2]" in + v|view|a|add) + _alternative \ + "names:book names:($(get_booknames))" + esac +fi + diff --git a/pkg/cli/infra/init.go b/pkg/cli/infra/init.go new file mode 100644 index 00000000..a6cbd1aa --- /dev/null +++ b/pkg/cli/infra/init.go @@ -0,0 +1,371 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package infra provides operations and definitions for the +// local infrastructure for Dnote +package infra + +import ( + "database/sql" + "fmt" + "os" + "strconv" + "time" + + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/config" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/migrate" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/dirs" + "github.com/pkg/errors" + "github.com/spf13/cobra" +) + +const ( + // DefaultAPIEndpoint is the default API endpoint used when none is configured + DefaultAPIEndpoint = "http://localhost:3001/api" +) + +// RunEFunc is a function type of dnote commands +type RunEFunc func(*cobra.Command, []string) error + +func checkLegacyDBPath() (string, bool) { + legacyDnoteDir := getLegacyDnotePath(dirs.Home) + ok, err := utils.FileExists(legacyDnoteDir) + if ok { + return legacyDnoteDir, true + } + + if err != nil { + log.Error(errors.Wrapf(err, "checking legacy dnote directory at %s", legacyDnoteDir).Error()) + } + + return "", false +} + +func getDBPath(paths context.Paths, customPath string) string { + // If custom path is provided, use it + if customPath != "" { + return customPath + } + + legacyDnoteDir, ok := checkLegacyDBPath() + if ok { + return fmt.Sprintf("%s/%s", legacyDnoteDir, consts.DnoteDBFileName) + } + + return fmt.Sprintf("%s/%s/%s", paths.Data, consts.DnoteDirName, consts.DnoteDBFileName) +} + +// newBaseCtx creates a minimal context with paths and database connection. +// This base context is used for file and database initialization before +// being enriched with config values by setupCtx. +func newBaseCtx(versionTag, customDBPath string) (context.DnoteCtx, error) { + dnoteDir := getLegacyDnotePath(dirs.Home) + paths := context.Paths{ + Home: dirs.Home, + Config: dirs.ConfigHome, + Data: dirs.DataHome, + Cache: dirs.CacheHome, + LegacyDnote: dnoteDir, + } + + dbPath := getDBPath(paths, customDBPath) + + db, err := database.Open(dbPath) + if err != nil { + return context.DnoteCtx{}, errors.Wrap(err, "conntecting to db") + } + + ctx := context.DnoteCtx{ + Paths: paths, + Version: versionTag, + DB: db, + } + + return ctx, nil +} + +// Init initializes the Dnote environment and returns a new dnote context +// apiEndpoint is used when creating a new config file (e.g., from ldflags during tests) +func Init(versionTag, apiEndpoint, dbPath string) (*context.DnoteCtx, error) { + ctx, err := newBaseCtx(versionTag, dbPath) + if err != nil { + return nil, errors.Wrap(err, "initializing a context") + } + + if err := initFiles(ctx, apiEndpoint); err != nil { + return nil, errors.Wrap(err, "initializing files") + } + + if err := InitDB(ctx); err != nil { + return nil, errors.Wrap(err, "initializing database") + } + if err := InitSystem(ctx); err != nil { + return nil, errors.Wrap(err, "initializing system data") + } + + if err := migrate.Legacy(ctx); err != nil { + return nil, errors.Wrap(err, "running legacy migration") + } + if err := migrate.Run(ctx, migrate.LocalSequence, migrate.LocalMode); err != nil { + return nil, errors.Wrap(err, "running migration") + } + + ctx, err = setupCtx(ctx) + if err != nil { + return nil, errors.Wrap(err, "setting up the context") + } + + log.Debug("context: %+v\n", context.Redact(ctx)) + + return &ctx, nil +} + +// setupCtx enriches the base context with values from config file and database. +// This is called after files and database have been initialized. +func setupCtx(ctx context.DnoteCtx) (context.DnoteCtx, error) { + db := ctx.DB + + var sessionKey string + var sessionKeyExpiry int64 + + err := db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemSessionKey).Scan(&sessionKey) + if err != nil && err != sql.ErrNoRows { + return ctx, errors.Wrap(err, "finding sesison key") + } + err = db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemSessionKeyExpiry).Scan(&sessionKeyExpiry) + if err != nil && err != sql.ErrNoRows { + return ctx, errors.Wrap(err, "finding sesison key expiry") + } + + cf, err := config.Read(ctx) + if err != nil { + return ctx, errors.Wrap(err, "reading config") + } + + ret := context.DnoteCtx{ + Paths: ctx.Paths, + Version: ctx.Version, + DB: ctx.DB, + SessionKey: sessionKey, + SessionKeyExpiry: sessionKeyExpiry, + APIEndpoint: cf.APIEndpoint, + Editor: cf.Editor, + Clock: clock.New(), + EnableUpgradeCheck: cf.EnableUpgradeCheck, + HTTPClient: client.NewRateLimitedHTTPClient(), + } + + return ret, nil +} + +// getLegacyDnotePath returns a legacy dnote directory path placed under +// the user's home directory +func getLegacyDnotePath(homeDir string) string { + return fmt.Sprintf("%s/%s", homeDir, consts.LegacyDnoteDirName) +} + +// InitDB initializes the database. +// Ideally this process must be a part of migration sequence. But it is performed +// seaprately because it is a prerequisite for legacy migration. +func InitDB(ctx context.DnoteCtx) error { + log.Debug("initializing the database\n") + + db := ctx.DB + + _, err := db.Exec(`CREATE TABLE IF NOT EXISTS notes + ( + id integer PRIMARY KEY AUTOINCREMENT, + uuid text NOT NULL, + book_uuid text NOT NULL, + content text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + public bool DEFAULT false + )`) + if err != nil { + return errors.Wrap(err, "creating notes table") + } + + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS books + ( + uuid text PRIMARY KEY, + label text NOT NULL + )`) + if err != nil { + return errors.Wrap(err, "creating books table") + } + + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS system + ( + key string NOT NULL, + value text NOT NULL + )`) + if err != nil { + return errors.Wrap(err, "creating system table") + } + + _, err = db.Exec(`CREATE TABLE IF NOT EXISTS actions + ( + uuid text PRIMARY KEY, + schema integer NOT NULL, + type text NOT NULL, + data text NOT NULL, + timestamp integer NOT NULL + )`) + if err != nil { + return errors.Wrap(err, "creating actions table") + } + + _, err = db.Exec(` + CREATE UNIQUE INDEX IF NOT EXISTS idx_books_label ON books(label); + CREATE UNIQUE INDEX IF NOT EXISTS idx_notes_uuid ON notes(uuid); + CREATE UNIQUE INDEX IF NOT EXISTS idx_books_uuid ON books(uuid); + CREATE INDEX IF NOT EXISTS idx_notes_book_uuid ON notes(book_uuid);`) + if err != nil { + return errors.Wrap(err, "creating indices") + } + + return nil +} + +func initSystemKV(db *database.DB, key string, val string) error { + var count int + if err := db.QueryRow("SELECT count(*) FROM system WHERE key = ?", key).Scan(&count); err != nil { + return errors.Wrapf(err, "counting %s", key) + } + + if count > 0 { + return nil + } + + if _, err := db.Exec("INSERT INTO system (key, value) VALUES (?, ?)", key, val); err != nil { + db.Rollback() + return errors.Wrapf(err, "inserting %s %s", key, val) + } + + return nil +} + +// InitSystem inserts system data if missing +func InitSystem(ctx context.DnoteCtx) error { + log.Debug("initializing the system\n") + + db := ctx.DB + + tx, err := db.Begin() + if err != nil { + return errors.Wrap(err, "beginning a transaction") + } + + nowStr := strconv.FormatInt(time.Now().Unix(), 10) + if err := initSystemKV(tx, consts.SystemLastUpgrade, nowStr); err != nil { + return errors.Wrapf(err, "initializing system config for %s", consts.SystemLastUpgrade) + } + if err := initSystemKV(tx, consts.SystemLastMaxUSN, "0"); err != nil { + return errors.Wrapf(err, "initializing system config for %s", consts.SystemLastMaxUSN) + } + if err := initSystemKV(tx, consts.SystemLastSyncAt, "0"); err != nil { + return errors.Wrapf(err, "initializing system config for %s", consts.SystemLastSyncAt) + } + + if err := tx.Commit(); err != nil { + return errors.Wrap(err, "committing transaction") + } + + return nil +} + +// getEditorCommand returns the system's editor command with appropriate flags, +// if necessary, to make the command wait until editor is close to exit. +func getEditorCommand() string { + editor := os.Getenv("EDITOR") + + var ret string + + switch editor { + case "atom": + ret = "atom -w" + case "subl": + ret = "subl -n -w" + case "code": + ret = "code -n -w" + case "mate": + ret = "mate -w" + case "vim": + ret = "vim" + case "nano": + ret = "nano" + case "emacs": + ret = "emacs" + case "nvim": + ret = "nvim" + default: + ret = "vi" + } + + return ret +} + + +// initConfigFile populates a new config file if it does not exist yet +func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error { + path := config.GetPath(ctx) + ok, err := utils.FileExists(path) + if err != nil { + return errors.Wrap(err, "checking if config exists") + } + if ok { + return nil + } + + editor := getEditorCommand() + + // Use default API endpoint if none provided + endpoint := apiEndpoint + if endpoint == "" { + endpoint = DefaultAPIEndpoint + } + + cf := config.Config{ + Editor: editor, + APIEndpoint: endpoint, + EnableUpgradeCheck: true, + } + + if err := config.Write(ctx, cf); err != nil { + return errors.Wrap(err, "writing config") + } + + return nil +} + +// initFiles creates, if necessary, the dnote directory and files inside +func initFiles(ctx context.DnoteCtx, apiEndpoint string) error { + if err := context.InitDnoteDirs(ctx.Paths); err != nil { + return errors.Wrap(err, "creating the dnote dir") + } + if err := initConfigFile(ctx, apiEndpoint); err != nil { + return errors.Wrap(err, "generating the config file") + } + + return nil +} diff --git a/pkg/cli/infra/init_test.go b/pkg/cli/infra/init_test.go new file mode 100644 index 00000000..8c698624 --- /dev/null +++ b/pkg/cli/infra/init_test.go @@ -0,0 +1,126 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package infra + +import ( + "fmt" + "os" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/config" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/dirs" + "github.com/pkg/errors" +) + +func TestInitSystemKV(t *testing.T) { + // Setup + db := database.InitTestMemoryDB(t) + + var originalCount int + database.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + if err := initSystemKV(tx, "testKey", "testVal"); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing")) + } + + tx.Commit() + + // Test + var count int + database.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &count) + assert.Equal(t, count, originalCount+1, "system count mismatch") + + var val string + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val) + assert.Equal(t, val, "testVal", "system value mismatch") +} + +func TestInitSystemKV_existing(t *testing.T) { + // Setup + db := database.InitTestMemoryDB(t) + + database.MustExec(t, "inserting a system config", db, "INSERT INTO system (key, value) VALUES (?, ?)", "testKey", "testVal") + + var originalCount int + database.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + if err := initSystemKV(tx, "testKey", "newTestVal"); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "executing")) + } + + tx.Commit() + + // Test + var count int + database.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &count) + assert.Equal(t, count, originalCount, "system count mismatch") + + var val string + database.MustScan(t, "getting system value", + db.QueryRow("SELECT value FROM system WHERE key = ?", "testKey"), &val) + assert.Equal(t, val, "testVal", "system value should not have been updated") +} + +func TestInit_APIEndpoint(t *testing.T) { + // Create a temporary directory for test + tmpDir, err := os.MkdirTemp("", "dnote-init-test-*") + if err != nil { + t.Fatal(errors.Wrap(err, "creating temp dir")) + } + defer os.RemoveAll(tmpDir) + + // Set up environment to use our temp directory + t.Setenv("XDG_CONFIG_HOME", fmt.Sprintf("%s/config", tmpDir)) + t.Setenv("XDG_DATA_HOME", fmt.Sprintf("%s/data", tmpDir)) + t.Setenv("XDG_CACHE_HOME", fmt.Sprintf("%s/cache", tmpDir)) + + // Force dirs package to reload with new environment + dirs.Reload() + + // Initialize - should create config with default apiEndpoint + ctx, err := Init("test-version", "", "") + if err != nil { + t.Fatal(errors.Wrap(err, "initializing")) + } + defer ctx.DB.Close() + + // Read the config that was created + cf, err := config.Read(*ctx) + if err != nil { + t.Fatal(errors.Wrap(err, "reading config")) + } + + // Context should use the apiEndpoint from config + assert.Equal(t, ctx.APIEndpoint, DefaultAPIEndpoint, "context should use apiEndpoint from config") + assert.Equal(t, cf.APIEndpoint, DefaultAPIEndpoint, "context should use apiEndpoint from config") +} diff --git a/cli/log/log.go b/pkg/cli/log/log.go similarity index 71% rename from cli/log/log.go rename to pkg/cli/log/log.go index 358eadc7..0bc569b6 100644 --- a/cli/log/log.go +++ b/pkg/cli/log/log.go @@ -1,27 +1,30 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package log import ( "fmt" - "github.com/dnote/color" "os" + + "github.com/fatih/color" +) + +const ( + debugEnvName = "DNOTE_DEBUG" + debugEnvValue = "1" ) var ( @@ -81,7 +84,7 @@ func Error(msg string) { // Errorf prints an error message with optional format verbs func Errorf(msg string, v ...interface{}) { - fmt.Fprintf(color.Output, "%s%s %s", indent, ColorRed.Sprintf("⨯"), fmt.Sprintf(msg, v...)) + fmt.Fprintf(color.Output, "%s%s %s", indent, ColorRed.Sprintf("%s", "⨯"), fmt.Sprintf(msg, v...)) } // Printf prints an normal message @@ -96,17 +99,29 @@ func Askf(msg string, masked bool, v ...interface{}) { var symbol string if masked { - symbol = ColorGray.Sprintf(symbolChar) + symbol = ColorGray.Sprintf("%s", symbolChar) } else { - symbol = ColorGreen.Sprintf(symbolChar) + symbol = ColorGreen.Sprintf("%s", symbolChar) } fmt.Fprintf(color.Output, "%s%s %s: ", indent, symbol, fmt.Sprintf(msg, v...)) } +// isDebug returns true if debug mode is enabled +func isDebug() bool { + return os.Getenv(debugEnvName) == debugEnvValue +} + // Debug prints to the console if DNOTE_DEBUG is set func Debug(msg string, v ...interface{}) { - if os.Getenv("DNOTE_DEBUG") == "1" { + if isDebug() { fmt.Fprintf(color.Output, "%s %s", ColorGray.Sprint("DEBUG:"), fmt.Sprintf(msg, v...)) } } + +// DebugNewline prints a newline only in debug mode +func DebugNewline() { + if isDebug() { + fmt.Println() + } +} diff --git a/pkg/cli/main.go b/pkg/cli/main.go new file mode 100644 index 00000000..2fb1c564 --- /dev/null +++ b/pkg/cli/main.go @@ -0,0 +1,89 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "os" + "strings" + + "github.com/dnote/dnote/pkg/cli/infra" + "github.com/dnote/dnote/pkg/cli/log" + _ "github.com/mattn/go-sqlite3" + "github.com/pkg/errors" + + // commands + "github.com/dnote/dnote/pkg/cli/cmd/add" + "github.com/dnote/dnote/pkg/cli/cmd/edit" + "github.com/dnote/dnote/pkg/cli/cmd/find" + "github.com/dnote/dnote/pkg/cli/cmd/login" + "github.com/dnote/dnote/pkg/cli/cmd/logout" + "github.com/dnote/dnote/pkg/cli/cmd/remove" + "github.com/dnote/dnote/pkg/cli/cmd/root" + "github.com/dnote/dnote/pkg/cli/cmd/sync" + "github.com/dnote/dnote/pkg/cli/cmd/version" + "github.com/dnote/dnote/pkg/cli/cmd/view" +) + +// apiEndpoint and versionTag are populated during link time +var apiEndpoint string +var versionTag = "master" + +// parseDBPath extracts --dbPath flag value from command line arguments +// regardless of where it appears (before or after subcommand). +// Returns empty string if not found. +func parseDBPath(args []string) string { + for i, arg := range args { + // Handle --dbPath=value + if strings.HasPrefix(arg, "--dbPath=") { + return strings.TrimPrefix(arg, "--dbPath=") + } + // Handle --dbPath value + if arg == "--dbPath" && i+1 < len(args) { + return args[i+1] + } + } + return "" +} + +func main() { + // Parse flags early to get --dbPath before initializing database + // We need to manually parse --dbPath because it can appear after the subcommand + // (e.g., "dnote sync --full --dbPath=./custom.db") and root.ParseFlags only + // parses flags before the subcommand. + dbPath := parseDBPath(os.Args[1:]) + + // Initialize context - defaultAPIEndpoint is used when creating new config file + ctx, err := infra.Init(versionTag, apiEndpoint, dbPath) + if err != nil { + panic(errors.Wrap(err, "initializing context")) + } + defer ctx.DB.Close() + + root.Register(remove.NewCmd(*ctx)) + root.Register(edit.NewCmd(*ctx)) + root.Register(login.NewCmd(*ctx)) + root.Register(logout.NewCmd(*ctx)) + root.Register(add.NewCmd(*ctx)) + root.Register(sync.NewCmd(*ctx)) + root.Register(version.NewCmd(*ctx)) + root.Register(view.NewCmd(*ctx)) + root.Register(find.NewCmd(*ctx)) + + if err := root.Execute(); err != nil { + log.Errorf("%s\n", err.Error()) + os.Exit(1) + } +} diff --git a/pkg/cli/main_test.go b/pkg/cli/main_test.go new file mode 100644 index 00000000..727a5c3e --- /dev/null +++ b/pkg/cli/main_test.go @@ -0,0 +1,633 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "fmt" + "log" + "os" + "os/exec" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +var binaryName = "test-dnote" + +// setupTestEnv creates a unique test directory for parallel test execution +func setupTestEnv(t *testing.T) (string, testutils.RunDnoteCmdOptions) { + testDir := t.TempDir() + opts := testutils.RunDnoteCmdOptions{ + Env: []string{ + fmt.Sprintf("XDG_CONFIG_HOME=%s", testDir), + fmt.Sprintf("XDG_DATA_HOME=%s", testDir), + fmt.Sprintf("XDG_CACHE_HOME=%s", testDir), + }, + } + return testDir, opts +} + +func TestMain(m *testing.M) { + if err := exec.Command("go", "build", "--tags", "fts5", "-o", binaryName).Run(); err != nil { + log.Print(errors.Wrap(err, "building a binary").Error()) + os.Exit(1) + } + + os.Exit(m.Run()) +} + +func TestInit(t *testing.T) { + testDir, opts := setupTestEnv(t) + + // Execute + // run an arbitrary command "view" due to https://github.com/spf13/cobra/issues/1056 + testutils.RunDnoteCmd(t, opts, binaryName, "view") + + db := database.OpenTestDB(t, testDir) + + // Test + ok, err := utils.FileExists(testDir) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if dnote dir exists")) + } + if !ok { + t.Errorf("dnote directory was not initialized") + } + + ok, err = utils.FileExists(fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.ConfigFilename)) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if dnote config exists")) + } + if !ok { + t.Errorf("config file was not initialized") + } + + var notesTableCount, booksTableCount, systemTableCount int + database.MustScan(t, "counting notes", + db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", "table", "notes"), ¬esTableCount) + database.MustScan(t, "counting books", + db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", "table", "books"), &booksTableCount) + database.MustScan(t, "counting system", + db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type = ? AND name = ?", "table", "system"), &systemTableCount) + + assert.Equal(t, notesTableCount, 1, "notes table count mismatch") + assert.Equal(t, booksTableCount, 1, "books table count mismatch") + assert.Equal(t, systemTableCount, 1, "system table count mismatch") + + // test that all default system configurations are generated + var lastUpgrade, lastMaxUSN, lastSyncAt string + database.MustScan(t, "scanning last upgrade", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastUpgrade), &lastUpgrade) + database.MustScan(t, "scanning last max usn", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + database.MustScan(t, "scanning last sync at", + db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &lastSyncAt) + + assert.NotEqual(t, lastUpgrade, "", "last upgrade should not be empty") + assert.NotEqual(t, lastMaxUSN, "", "last max usn should not be empty") + assert.NotEqual(t, lastSyncAt, "", "last sync at should not be empty") +} + +func TestAddNote(t *testing.T) { + t.Run("new book", func(t *testing.T) { + testDir, opts := setupTestEnv(t) + + // Set up and execute + testutils.RunDnoteCmd(t, opts, binaryName, "add", "js", "-c", "foo") + testutils.MustWaitDnoteCmd(t, opts, testutils.UserContent, binaryName, "add", "js") + + db := database.OpenTestDB(t, testDir) + + // Test + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 1, "book count mismatch") + assert.Equalf(t, noteCount, 2, "note count mismatch") + + var book database.Book + database.MustScan(t, "getting book", db.QueryRow("SELECT uuid, dirty FROM books where label = ?", "js"), &book.UUID, &book.Dirty) + var note database.Note + database.MustScan(t, "getting note", + db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes where book_uuid = ?", book.UUID), ¬e.UUID, ¬e.Body, ¬e.AddedOn, ¬e.Dirty) + + assert.Equal(t, book.Dirty, true, "Book dirty mismatch") + + assert.NotEqual(t, note.UUID, "", "Note should have UUID") + assert.Equal(t, note.Body, "foo", "Note body mismatch") + assert.Equal(t, note.Dirty, true, "Note dirty mismatch") + assert.NotEqual(t, note.AddedOn, int64(0), "Note added_on mismatch") + }) + + t.Run("existing book", func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup3(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "add", "js", "-c", "foo") + + // Test + + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 1, "book count mismatch") + assert.Equalf(t, noteCount, 2, "note count mismatch") + + var n1, n2 database.Note + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Dirty) + database.MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes WHERE book_uuid = ? AND body = ?", "js-book-uuid", "foo"), &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Dirty) + + var book database.Book + database.MustScan(t, "getting book", db.QueryRow("SELECT dirty FROM books where label = ?", "js"), &book.Dirty) + + assert.Equal(t, book.Dirty, false, "Book dirty mismatch") + + assert.NotEqual(t, n1.UUID, "", "n1 should have UUID") + assert.Equal(t, n1.Body, "Booleans have toString()", "n1 body mismatch") + assert.Equal(t, n1.AddedOn, int64(1515199943), "n1 added_on mismatch") + assert.Equal(t, n1.Dirty, false, "n1 dirty mismatch") + + assert.NotEqual(t, n2.UUID, "", "n2 should have UUID") + assert.Equal(t, n2.Body, "foo", "n2 body mismatch") + assert.Equal(t, n2.Dirty, true, "n2 dirty mismatch") + }) +} + +func TestEditNote(t *testing.T) { + t.Run("content flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-c", "foo bar") + + // Test + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 1, "book count mismatch") + assert.Equalf(t, noteCount, 2, "note count mismatch") + + var n1, n2 database.Note + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes where book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Dirty) + database.MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, body, added_on, dirty FROM notes where book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Dirty) + + assert.Equal(t, n1.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n1 should have UUID") + assert.Equal(t, n1.Body, "Booleans have toString()", "n1 body mismatch") + assert.Equal(t, n1.Dirty, false, "n1 dirty mismatch") + + assert.Equal(t, n2.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "Note should have UUID") + assert.Equal(t, n2.Body, "foo bar", "Note body mismatch") + assert.Equal(t, n2.Dirty, true, "n2 dirty mismatch") + assert.NotEqual(t, n2.EditedOn, 0, "Note edited_on mismatch") + }) + + t.Run("book flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup5(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-b", "linux") + + // Test + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 2, "book count mismatch") + assert.Equalf(t, noteCount, 2, "note count mismatch") + + var n1, n2 database.Note + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, dirty FROM notes where uuid = ?", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), &n1.UUID, &n1.BookUUID, &n1.Body, &n1.AddedOn, &n1.Dirty) + database.MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, dirty FROM notes where uuid = ?", "43827b9a-c2b0-4c06-a290-97991c896653"), &n2.UUID, &n2.BookUUID, &n2.Body, &n2.AddedOn, &n2.Dirty) + + assert.Equal(t, n1.BookUUID, "js-book-uuid", "n1 BookUUID mismatch") + assert.Equal(t, n1.Body, "n1 body", "n1 Body mismatch") + assert.Equal(t, n1.Dirty, false, "n1 Dirty mismatch") + assert.Equal(t, n1.EditedOn, int64(0), "n1 EditedOn mismatch") + + assert.Equal(t, n2.BookUUID, "linux-book-uuid", "n2 BookUUID mismatch") + assert.Equal(t, n2.Body, "n2 body", "n2 Body mismatch") + assert.Equal(t, n2.Dirty, true, "n2 Dirty mismatch") + assert.NotEqual(t, n2.EditedOn, 0, "n2 EditedOn mismatch") + }) + + t.Run("book flag and content flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup5(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-b", "linux", "-c", "n2 body updated") + + // Test + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 2, "book count mismatch") + assert.Equalf(t, noteCount, 2, "note count mismatch") + + var n1, n2 database.Note + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, dirty FROM notes where uuid = ?", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), &n1.UUID, &n1.BookUUID, &n1.Body, &n1.AddedOn, &n1.Dirty) + database.MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, book_uuid, body, added_on, dirty FROM notes where uuid = ?", "43827b9a-c2b0-4c06-a290-97991c896653"), &n2.UUID, &n2.BookUUID, &n2.Body, &n2.AddedOn, &n2.Dirty) + + assert.Equal(t, n1.BookUUID, "js-book-uuid", "n1 BookUUID mismatch") + assert.Equal(t, n1.Body, "n1 body", "n1 Body mismatch") + assert.Equal(t, n1.Dirty, false, "n1 Dirty mismatch") + assert.Equal(t, n1.EditedOn, int64(0), "n1 EditedOn mismatch") + + assert.Equal(t, n2.BookUUID, "linux-book-uuid", "n2 BookUUID mismatch") + assert.Equal(t, n2.Body, "n2 body updated", "n2 Body mismatch") + assert.Equal(t, n2.Dirty, true, "n2 Dirty mismatch") + assert.NotEqual(t, n2.EditedOn, 0, "n2 EditedOn mismatch") + }) +} + +func TestEditBook(t *testing.T) { + t.Run("name flag", func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup1(t, db) + + // Execute + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "js", "-n", "js-edited") + + // Test + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 2, "book count mismatch") + assert.Equalf(t, noteCount, 1, "note count mismatch") + + var b1, b2 database.Book + var n1 database.Note + database.MustScan(t, "getting b1", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "js-book-uuid"), &b1.UUID, &b1.Label, &b1.USN, &b1.Dirty) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT uuid, label, usn, dirty FROM books WHERE uuid = ?", "linux-book-uuid"), &b2.UUID, &b2.Label, &b2.USN, &b2.Dirty) + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), + &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN) + + assert.Equal(t, b1.UUID, "js-book-uuid", "b1 should have UUID") + assert.Equal(t, b1.Label, "js-edited", "b1 Label mismatch") + assert.Equal(t, b1.USN, 0, "b1 USN mismatch") + assert.Equal(t, b1.Dirty, true, "b1 Dirty mismatch") + + assert.Equal(t, b2.UUID, "linux-book-uuid", "b2 should have UUID") + assert.Equal(t, b2.Label, "linux", "b2 Label mismatch") + assert.Equal(t, b2.USN, 0, "b2 USN mismatch") + assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch") + + assert.Equal(t, n1.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n1 UUID mismatch") + assert.Equal(t, n1.Body, "Booleans have toString()", "n1 Body mismatch") + assert.Equal(t, n1.AddedOn, int64(1515199943), "n1 AddedOn mismatch") + assert.Equal(t, n1.Deleted, false, "n1 Deleted mismatch") + assert.Equal(t, n1.Dirty, false, "n1 Dirty mismatch") + assert.Equal(t, n1.USN, 0, "n1 USN mismatch") + }) +} + +func TestRemoveNote(t *testing.T) { + testCases := []struct { + yesFlag bool + }{ + { + yesFlag: false, + }, + { + yesFlag: true, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup2(t, db) + + // Execute + if tc.yesFlag { + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "remove", "-y", "1") + } else { + testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveNote, binaryName, "--dbPath", dbPath, "remove", "1") + } + + // Test + var noteCount, bookCount, jsNoteCount, linuxNoteCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount) + database.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount) + + assert.Equalf(t, bookCount, 2, "book count mismatch") + assert.Equalf(t, noteCount, 3, "note count mismatch") + assert.Equal(t, jsNoteCount, 2, "js book should have 2 notes") + assert.Equal(t, linuxNoteCount, 1, "linux book book should have 1 note") + + var b1, b2 database.Book + var n1, n2, n3 database.Note + database.MustScan(t, "getting b1", + db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"), + &b1.Label, &b1.Deleted, &b1.USN) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT label, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"), + &b2.Label, &b2.Deleted, &b2.USN) + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), + &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN) + database.MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), + &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN) + database.MustScan(t, "getting n3", + db.QueryRow("SELECT uuid, body, added_on, deleted, dirty, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"), + &n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN) + + assert.Equal(t, b1.Label, "js", "b1 label mismatch") + assert.Equal(t, b1.Deleted, false, "b1 deleted mismatch") + assert.Equal(t, b1.Dirty, false, "b1 Dirty mismatch") + assert.Equal(t, b1.USN, 111, "b1 usn mismatch") + + assert.Equal(t, b2.Label, "linux", "b2 label mismatch") + assert.Equal(t, b2.Deleted, false, "b2 deleted mismatch") + assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch") + assert.Equal(t, b2.USN, 122, "b2 usn mismatch") + + assert.Equal(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID") + assert.Equal(t, n1.Body, "", "n1 body mismatch") + assert.Equal(t, n1.Deleted, true, "n1 deleted mismatch") + assert.Equal(t, n1.Dirty, true, "n1 Dirty mismatch") + assert.Equal(t, n1.USN, 11, "n1 usn mismatch") + + assert.Equal(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID") + assert.Equal(t, n2.Body, "n2 body", "n2 body mismatch") + assert.Equal(t, n2.Deleted, false, "n2 deleted mismatch") + assert.Equal(t, n2.Dirty, false, "n2 Dirty mismatch") + assert.Equal(t, n2.USN, 12, "n2 usn mismatch") + + assert.Equal(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID") + assert.Equal(t, n3.Body, "n3 body", "n3 body mismatch") + assert.Equal(t, n3.Deleted, false, "n3 deleted mismatch") + assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch") + assert.Equal(t, n3.USN, 13, "n3 usn mismatch") + }) + } +} + +func TestRemoveBook(t *testing.T) { + testCases := []struct { + yesFlag bool + }{ + { + yesFlag: false, + }, + { + yesFlag: true, + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) { + _, opts := setupTestEnv(t) + + // Setup + db, dbPath := database.InitTestFileDB(t) + testutils.Setup2(t, db) + + // Execute + if tc.yesFlag { + testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "remove", "-y", "js") + } else { + testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveBook, binaryName, "--dbPath", dbPath, "remove", "js") + } + + // Test + var noteCount, bookCount, jsNoteCount, linuxNoteCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + database.MustScan(t, "counting js notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "js-book-uuid"), &jsNoteCount) + database.MustScan(t, "counting linux notes", db.QueryRow("SELECT count(*) FROM notes WHERE book_uuid = ?", "linux-book-uuid"), &linuxNoteCount) + + assert.Equalf(t, bookCount, 2, "book count mismatch") + assert.Equalf(t, noteCount, 3, "note count mismatch") + assert.Equal(t, jsNoteCount, 2, "js book should have 2 notes") + assert.Equal(t, linuxNoteCount, 1, "linux book book should have 1 note") + + var b1, b2 database.Book + var n1, n2, n3 database.Note + database.MustScan(t, "getting b1", + db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "js-book-uuid"), + &b1.Label, &b1.Dirty, &b1.Deleted, &b1.USN) + database.MustScan(t, "getting b2", + db.QueryRow("SELECT label, dirty, deleted, usn FROM books WHERE uuid = ?", "linux-book-uuid"), + &b2.Label, &b2.Dirty, &b2.Deleted, &b2.USN) + database.MustScan(t, "getting n1", + db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f"), + &n1.UUID, &n1.Body, &n1.AddedOn, &n1.Deleted, &n1.Dirty, &n1.USN) + database.MustScan(t, "getting n2", + db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "js-book-uuid", "43827b9a-c2b0-4c06-a290-97991c896653"), + &n2.UUID, &n2.Body, &n2.AddedOn, &n2.Deleted, &n2.Dirty, &n2.USN) + database.MustScan(t, "getting n3", + db.QueryRow("SELECT uuid, body, added_on, dirty, deleted, usn FROM notes WHERE book_uuid = ? AND uuid = ?", "linux-book-uuid", "3e065d55-6d47-42f2-a6bf-f5844130b2d2"), + &n3.UUID, &n3.Body, &n3.AddedOn, &n3.Deleted, &n3.Dirty, &n3.USN) + + assert.NotEqual(t, b1.Label, "js", "b1 label mismatch") + assert.Equal(t, b1.Dirty, true, "b1 Dirty mismatch") + assert.Equal(t, b1.Deleted, true, "b1 deleted mismatch") + assert.Equal(t, b1.USN, 111, "b1 usn mismatch") + + assert.Equal(t, b2.Label, "linux", "b2 label mismatch") + assert.Equal(t, b2.Dirty, false, "b2 Dirty mismatch") + assert.Equal(t, b2.Deleted, false, "b2 deleted mismatch") + assert.Equal(t, b2.USN, 122, "b2 usn mismatch") + + assert.Equal(t, n1.UUID, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", "n1 should have UUID") + assert.Equal(t, n1.Body, "", "n1 body mismatch") + assert.Equal(t, n1.Dirty, true, "n1 Dirty mismatch") + assert.Equal(t, n1.Deleted, true, "n1 deleted mismatch") + assert.Equal(t, n1.USN, 11, "n1 usn mismatch") + + assert.Equal(t, n2.UUID, "43827b9a-c2b0-4c06-a290-97991c896653", "n2 should have UUID") + assert.Equal(t, n2.Body, "", "n2 body mismatch") + assert.Equal(t, n2.Dirty, true, "n2 Dirty mismatch") + assert.Equal(t, n2.Deleted, true, "n2 deleted mismatch") + assert.Equal(t, n2.USN, 12, "n2 usn mismatch") + + assert.Equal(t, n3.UUID, "3e065d55-6d47-42f2-a6bf-f5844130b2d2", "n3 should have UUID") + assert.Equal(t, n3.Body, "n3 body", "n3 body mismatch") + assert.Equal(t, n3.Dirty, false, "n3 Dirty mismatch") + assert.Equal(t, n3.Deleted, false, "n3 deleted mismatch") + assert.Equal(t, n3.USN, 13, "n3 usn mismatch") + }) + } +} + +func TestDBPathFlag(t *testing.T) { + // Helper function to verify database contents + verifyDatabase := func(t *testing.T, dbPath, expectedBook, expectedNote string) *database.DB { + ok, err := utils.FileExists(dbPath) + if err != nil { + t.Fatal(errors.Wrapf(err, "checking if custom db exists at %s", dbPath)) + } + if !ok { + t.Errorf("custom database was not created at %s", dbPath) + } + + db, err := database.Open(dbPath) + if err != nil { + t.Fatal(errors.Wrapf(err, "opening db at %s", dbPath)) + } + + var noteCount, bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + database.MustScan(t, "counting notes", db.QueryRow("SELECT count(*) FROM notes"), ¬eCount) + + assert.Equalf(t, bookCount, 1, fmt.Sprintf("%s book count mismatch", dbPath)) + assert.Equalf(t, noteCount, 1, fmt.Sprintf("%s note count mismatch", dbPath)) + + var book database.Book + database.MustScan(t, "getting book", db.QueryRow("SELECT label FROM books"), &book.Label) + assert.Equalf(t, book.Label, expectedBook, fmt.Sprintf("%s book label mismatch", dbPath)) + + var note database.Note + database.MustScan(t, "getting note", db.QueryRow("SELECT body FROM notes"), ¬e.Body) + assert.Equalf(t, note.Body, expectedNote, fmt.Sprintf("%s note body mismatch", dbPath)) + + return db + } + + // Setup - use two different custom database paths + testDir, customOpts := setupTestEnv(t) + customDBPath1 := fmt.Sprintf("%s/custom-test1.db", testDir) + customDBPath2 := fmt.Sprintf("%s/custom-test2.db", testDir) + + // Execute - add different notes to each database + testutils.RunDnoteCmd(t, customOpts, binaryName, "--dbPath", customDBPath1, "add", "db1-book", "-c", "content in db1") + testutils.RunDnoteCmd(t, customOpts, binaryName, "--dbPath", customDBPath2, "add", "db2-book", "-c", "content in db2") + + // Test both databases + db1 := verifyDatabase(t, customDBPath1, "db1-book", "content in db1") + defer db1.Close() + + db2 := verifyDatabase(t, customDBPath2, "db2-book", "content in db2") + defer db2.Close() + + // Verify that the databases are independent + var db1HasDB2Book int + db1.QueryRow("SELECT count(*) FROM books WHERE label = ?", "db2-book").Scan(&db1HasDB2Book) + assert.Equal(t, db1HasDB2Book, 0, "db1 should not have db2's book") + + var db2HasDB1Book int + db2.QueryRow("SELECT count(*) FROM books WHERE label = ?", "db1-book").Scan(&db2HasDB1Book) + assert.Equal(t, db2HasDB1Book, 0, "db2 should not have db1's book") +} + +func TestView(t *testing.T) { + t.Run("view note by rowid", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "1") + + assert.Equal(t, strings.Contains(output, "Booleans have toString()"), true, "should contain note content") + assert.Equal(t, strings.Contains(output, "book name"), true, "should show metadata") + }) + + t.Run("view note content only", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "1", "--content-only") + + assert.Equal(t, strings.Contains(output, "Booleans have toString()"), true, "should contain note content") + assert.Equal(t, strings.Contains(output, "book name"), false, "should not show metadata") + }) + + t.Run("list books", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup1(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view") + + assert.Equal(t, strings.Contains(output, "js"), true, "should list js book") + assert.Equal(t, strings.Contains(output, "linux"), true, "should list linux book") + }) + + t.Run("list notes in book", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup2(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "js") + + assert.Equal(t, strings.Contains(output, "n1 body"), true, "should list note 1") + assert.Equal(t, strings.Contains(output, "n2 body"), true, "should list note 2") + }) + + t.Run("view note by book name and rowid", func(t *testing.T) { + _, opts := setupTestEnv(t) + + db, dbPath := database.InitTestFileDB(t) + testutils.Setup4(t, db) + + output := testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "view", "js", "2") + + assert.Equal(t, strings.Contains(output, "Date object implements mathematical comparisons"), true, "should contain note content") + assert.Equal(t, strings.Contains(output, "book name"), true, "should show metadata") + }) +} diff --git a/cli/migrate/fixtures/legacy-2-pre-dnote.json b/pkg/cli/migrate/fixtures/legacy-2-pre-dnote.json similarity index 100% rename from cli/migrate/fixtures/legacy-2-pre-dnote.json rename to pkg/cli/migrate/fixtures/legacy-2-pre-dnote.json diff --git a/cli/migrate/fixtures/legacy-3-pre-dnote.json b/pkg/cli/migrate/fixtures/legacy-3-pre-dnote.json similarity index 100% rename from cli/migrate/fixtures/legacy-3-pre-dnote.json rename to pkg/cli/migrate/fixtures/legacy-3-pre-dnote.json diff --git a/cli/migrate/fixtures/legacy-4-pre-dnoterc.yaml b/pkg/cli/migrate/fixtures/legacy-4-pre-dnoterc.yaml similarity index 100% rename from cli/migrate/fixtures/legacy-4-pre-dnoterc.yaml rename to pkg/cli/migrate/fixtures/legacy-4-pre-dnoterc.yaml diff --git a/cli/migrate/fixtures/legacy-5-post-actions.json b/pkg/cli/migrate/fixtures/legacy-5-post-actions.json similarity index 100% rename from cli/migrate/fixtures/legacy-5-post-actions.json rename to pkg/cli/migrate/fixtures/legacy-5-post-actions.json diff --git a/cli/migrate/fixtures/legacy-5-pre-actions.json b/pkg/cli/migrate/fixtures/legacy-5-pre-actions.json similarity index 100% rename from cli/migrate/fixtures/legacy-5-pre-actions.json rename to pkg/cli/migrate/fixtures/legacy-5-pre-actions.json diff --git a/cli/migrate/fixtures/legacy-6-post-dnote.json b/pkg/cli/migrate/fixtures/legacy-6-post-dnote.json similarity index 100% rename from cli/migrate/fixtures/legacy-6-post-dnote.json rename to pkg/cli/migrate/fixtures/legacy-6-post-dnote.json diff --git a/cli/migrate/fixtures/legacy-6-pre-dnote.json b/pkg/cli/migrate/fixtures/legacy-6-pre-dnote.json similarity index 100% rename from cli/migrate/fixtures/legacy-6-pre-dnote.json rename to pkg/cli/migrate/fixtures/legacy-6-pre-dnote.json diff --git a/cli/migrate/fixtures/legacy-7-post-actions.json b/pkg/cli/migrate/fixtures/legacy-7-post-actions.json similarity index 100% rename from cli/migrate/fixtures/legacy-7-post-actions.json rename to pkg/cli/migrate/fixtures/legacy-7-post-actions.json diff --git a/cli/migrate/fixtures/legacy-7-pre-actions.json b/pkg/cli/migrate/fixtures/legacy-7-pre-actions.json similarity index 100% rename from cli/migrate/fixtures/legacy-7-pre-actions.json rename to pkg/cli/migrate/fixtures/legacy-7-pre-actions.json diff --git a/cli/migrate/fixtures/legacy-8-actions.json b/pkg/cli/migrate/fixtures/legacy-8-actions.json similarity index 100% rename from cli/migrate/fixtures/legacy-8-actions.json rename to pkg/cli/migrate/fixtures/legacy-8-actions.json diff --git a/cli/migrate/fixtures/legacy-8-dnote.json b/pkg/cli/migrate/fixtures/legacy-8-dnote.json similarity index 100% rename from cli/migrate/fixtures/legacy-8-dnote.json rename to pkg/cli/migrate/fixtures/legacy-8-dnote.json diff --git a/cli/migrate/fixtures/legacy-8-dnoterc.yaml b/pkg/cli/migrate/fixtures/legacy-8-dnoterc.yaml similarity index 100% rename from cli/migrate/fixtures/legacy-8-dnoterc.yaml rename to pkg/cli/migrate/fixtures/legacy-8-dnoterc.yaml diff --git a/cli/migrate/fixtures/legacy-8-schema.yaml b/pkg/cli/migrate/fixtures/legacy-8-schema.yaml similarity index 100% rename from cli/migrate/fixtures/legacy-8-schema.yaml rename to pkg/cli/migrate/fixtures/legacy-8-schema.yaml diff --git a/cli/migrate/fixtures/legacy-8-timestamps.yaml b/pkg/cli/migrate/fixtures/legacy-8-timestamps.yaml similarity index 100% rename from cli/migrate/fixtures/legacy-8-timestamps.yaml rename to pkg/cli/migrate/fixtures/legacy-8-timestamps.yaml diff --git a/cli/migrate/fixtures/local-1-pre-schema.sql b/pkg/cli/migrate/fixtures/local-1-pre-schema.sql similarity index 100% rename from cli/migrate/fixtures/local-1-pre-schema.sql rename to pkg/cli/migrate/fixtures/local-1-pre-schema.sql diff --git a/cli/testutils/fixtures/schema.sql b/pkg/cli/migrate/fixtures/local-10-pre-schema.sql similarity index 93% rename from cli/testutils/fixtures/schema.sql rename to pkg/cli/migrate/fixtures/local-10-pre-schema.sql index 6723608b..a9679e45 100644 --- a/cli/testutils/fixtures/schema.sql +++ b/pkg/cli/migrate/fixtures/local-10-pre-schema.sql @@ -10,14 +10,6 @@ CREATE TABLE system ); CREATE UNIQUE INDEX idx_books_label ON books(label); CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); -CREATE TABLE actions - ( - uuid text PRIMARY KEY, - schema integer NOT NULL, - type text NOT NULL, - data text NOT NULL, - timestamp integer NOT NULL - ); CREATE TABLE IF NOT EXISTS "notes" ( uuid text NOT NULL, @@ -30,8 +22,7 @@ CREATE TABLE IF NOT EXISTS "notes" usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false ); -CREATE VIRTUAL TABLE note_fts - USING fts5(content=notes, body, tokenize = "unicode61") +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'") /* note_fts(body) */; CREATE TABLE IF NOT EXISTS 'note_fts_data'(id INTEGER PRIMARY KEY, block BLOB); CREATE TABLE IF NOT EXISTS 'note_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; @@ -47,5 +38,13 @@ CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); END; +CREATE TABLE actions + ( + uuid text PRIMARY KEY, + schema integer NOT NULL, + type text NOT NULL, + data text NOT NULL, + timestamp integer NOT NULL + ); CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); CREATE INDEX idx_notes_book_uuid ON notes(book_uuid); diff --git a/pkg/cli/migrate/fixtures/local-11-pre-schema.sql b/pkg/cli/migrate/fixtures/local-11-pre-schema.sql new file mode 100644 index 00000000..a9679e45 --- /dev/null +++ b/pkg/cli/migrate/fixtures/local-11-pre-schema.sql @@ -0,0 +1,50 @@ +CREATE TABLE books + ( + uuid text PRIMARY KEY, + label text NOT NULL + , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); +CREATE TABLE system + ( + key string NOT NULL, + value text NOT NULL + ); +CREATE UNIQUE INDEX idx_books_label ON books(label); +CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); +CREATE TABLE IF NOT EXISTS "notes" + ( + uuid text NOT NULL, + book_uuid text NOT NULL, + body text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + public bool DEFAULT false, + dirty bool DEFAULT false, + usn int DEFAULT 0 NOT NULL, + deleted bool DEFAULT false + ); +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'") +/* note_fts(body) */; +CREATE TABLE IF NOT EXISTS 'note_fts_data'(id INTEGER PRIMARY KEY, block BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +CREATE TABLE IF NOT EXISTS 'note_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + END; +CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TABLE actions + ( + uuid text PRIMARY KEY, + schema integer NOT NULL, + type text NOT NULL, + data text NOT NULL, + timestamp integer NOT NULL + ); +CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); +CREATE INDEX idx_notes_book_uuid ON notes(book_uuid); diff --git a/pkg/cli/migrate/fixtures/local-12-pre-schema.sql b/pkg/cli/migrate/fixtures/local-12-pre-schema.sql new file mode 100644 index 00000000..5560af3d --- /dev/null +++ b/pkg/cli/migrate/fixtures/local-12-pre-schema.sql @@ -0,0 +1,42 @@ +CREATE TABLE books + ( + uuid text PRIMARY KEY, + label text NOT NULL + , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); +CREATE TABLE system + ( + key string NOT NULL, + value text NOT NULL + ); +CREATE UNIQUE INDEX idx_books_label ON books(label); +CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); +CREATE TABLE IF NOT EXISTS "notes" + ( + uuid text NOT NULL, + book_uuid text NOT NULL, + body text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + public bool DEFAULT false, + dirty bool DEFAULT false, + usn int DEFAULT 0 NOT NULL, + deleted bool DEFAULT false + ); +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'") +/* note_fts(body) */; +CREATE TABLE IF NOT EXISTS 'note_fts_data'(id INTEGER PRIMARY KEY, block BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +CREATE TABLE IF NOT EXISTS 'note_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + END; +CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); +CREATE INDEX idx_notes_book_uuid ON notes(book_uuid); diff --git a/pkg/cli/migrate/fixtures/local-14-pre-schema.sql b/pkg/cli/migrate/fixtures/local-14-pre-schema.sql new file mode 100644 index 00000000..5560af3d --- /dev/null +++ b/pkg/cli/migrate/fixtures/local-14-pre-schema.sql @@ -0,0 +1,42 @@ +CREATE TABLE books + ( + uuid text PRIMARY KEY, + label text NOT NULL + , dirty bool DEFAULT false, usn int DEFAULT 0 NOT NULL, deleted bool DEFAULT false); +CREATE TABLE system + ( + key string NOT NULL, + value text NOT NULL + ); +CREATE UNIQUE INDEX idx_books_label ON books(label); +CREATE UNIQUE INDEX idx_books_uuid ON books(uuid); +CREATE TABLE IF NOT EXISTS "notes" + ( + uuid text NOT NULL, + book_uuid text NOT NULL, + body text NOT NULL, + added_on integer NOT NULL, + edited_on integer DEFAULT 0, + public bool DEFAULT false, + dirty bool DEFAULT false, + usn int DEFAULT 0 NOT NULL, + deleted bool DEFAULT false + ); +CREATE VIRTUAL TABLE note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'") +/* note_fts(body) */; +CREATE TABLE IF NOT EXISTS 'note_fts_data'(id INTEGER PRIMARY KEY, block BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_idx'(segid, term, pgno, PRIMARY KEY(segid, term)) WITHOUT ROWID; +CREATE TABLE IF NOT EXISTS 'note_fts_docsize'(id INTEGER PRIMARY KEY, sz BLOB); +CREATE TABLE IF NOT EXISTS 'note_fts_config'(k PRIMARY KEY, v) WITHOUT ROWID; +CREATE TRIGGER notes_after_insert AFTER INSERT ON notes BEGIN + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE TRIGGER notes_after_delete AFTER DELETE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + END; +CREATE TRIGGER notes_after_update AFTER UPDATE ON notes BEGIN + INSERT INTO note_fts(note_fts, rowid, body) VALUES ('delete', old.rowid, old.body); + INSERT INTO note_fts(rowid, body) VALUES (new.rowid, new.body); + END; +CREATE UNIQUE INDEX idx_notes_uuid ON notes(uuid); +CREATE INDEX idx_notes_book_uuid ON notes(book_uuid); diff --git a/cli/migrate/fixtures/local-5-pre-schema.sql b/pkg/cli/migrate/fixtures/local-5-pre-schema.sql similarity index 100% rename from cli/migrate/fixtures/local-5-pre-schema.sql rename to pkg/cli/migrate/fixtures/local-5-pre-schema.sql diff --git a/cli/migrate/fixtures/local-7-pre-schema.sql b/pkg/cli/migrate/fixtures/local-7-pre-schema.sql similarity index 100% rename from cli/migrate/fixtures/local-7-pre-schema.sql rename to pkg/cli/migrate/fixtures/local-7-pre-schema.sql diff --git a/cli/migrate/fixtures/local-8-pre-schema.sql b/pkg/cli/migrate/fixtures/local-8-pre-schema.sql similarity index 100% rename from cli/migrate/fixtures/local-8-pre-schema.sql rename to pkg/cli/migrate/fixtures/local-8-pre-schema.sql diff --git a/cli/migrate/fixtures/local-9-pre-schema.sql b/pkg/cli/migrate/fixtures/local-9-pre-schema.sql similarity index 100% rename from cli/migrate/fixtures/local-9-pre-schema.sql rename to pkg/cli/migrate/fixtures/local-9-pre-schema.sql diff --git a/cli/migrate/fixtures/remote-1-pre-schema.sql b/pkg/cli/migrate/fixtures/remote-1-pre-schema.sql similarity index 100% rename from cli/migrate/fixtures/remote-1-pre-schema.sql rename to pkg/cli/migrate/fixtures/remote-1-pre-schema.sql diff --git a/cli/migrate/legacy.go b/pkg/cli/migrate/legacy.go similarity index 82% rename from cli/migrate/legacy.go rename to pkg/cli/migrate/legacy.go index ee79a07b..4399f7fb 100644 --- a/cli/migrate/legacy.go +++ b/pkg/cli/migrate/legacy.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package migrate provides migration logic for both sqlite and @@ -23,15 +20,14 @@ package migrate import ( "encoding/json" "fmt" - "io/ioutil" "os" "time" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" - "github.com/dnote/dnote/cli/utils" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/google/uuid" "github.com/pkg/errors" - "github.com/satori/go.uuid" "gopkg.in/yaml.v2" ) @@ -81,11 +77,25 @@ func makeSchema(complete bool) schema { return s } +func genUUID() (string, error) { + res, err := uuid.NewRandom() + if err != nil { + return "", errors.Wrap(err, "generating a random uuid") + } + + return res.String(), nil + +} + // Legacy performs migration on JSON-based dnote if necessary -func Legacy(ctx infra.DnoteCtx) error { +func Legacy(ctx context.DnoteCtx) error { // If schema does not exist, no need run a legacy migration schemaPath := getSchemaPath(ctx) - if ok := utils.FileExists(schemaPath); !ok { + ok, err := utils.FileExists(schemaPath) + if err != nil { + return errors.Wrap(err, "checking if schema exists") + } + if !ok { return nil } @@ -106,7 +116,7 @@ func Legacy(ctx infra.DnoteCtx) error { // performMigration backs up current .dnote data, performs migration, and // restores or cleans backups depending on if there is an error -func performMigration(ctx infra.DnoteCtx, migrationID int) error { +func performMigration(ctx context.DnoteCtx, migrationID int) error { // legacyMigrationV8 is the final migration of the legacy JSON Dnote migration // migrate to sqlite and return if migrationID == legacyMigrationV8 { @@ -162,9 +172,9 @@ func performMigration(ctx infra.DnoteCtx, migrationID int) error { } // backupDnoteDir backs up the dnote directory to a temporary backup directory -func backupDnoteDir(ctx infra.DnoteCtx) error { - srcPath := fmt.Sprintf("%s/.dnote", ctx.HomeDir) - tmpPath := fmt.Sprintf("%s/%s", ctx.HomeDir, backupDirName) +func backupDnoteDir(ctx context.DnoteCtx) error { + srcPath := fmt.Sprintf("%s/.dnote", ctx.Paths.Home) + tmpPath := fmt.Sprintf("%s/%s", ctx.Paths.Home, backupDirName) if err := utils.CopyDir(srcPath, tmpPath); err != nil { return errors.Wrap(err, "Failed to copy the .dnote directory") @@ -173,19 +183,19 @@ func backupDnoteDir(ctx infra.DnoteCtx) error { return nil } -func restoreBackup(ctx infra.DnoteCtx) error { +func restoreBackup(ctx context.DnoteCtx) error { var err error defer func() { if err != nil { log.Printf(`Failed to restore backup for a failed migration. Don't worry. Your data is still intact in the backup directory. - Get help on https://github.com/dnote/dnote/cli/issues`) + Get help on https://github.com/dnote/dnote/pkg/cli/issues`) } }() - srcPath := fmt.Sprintf("%s/.dnote", ctx.HomeDir) - backupPath := fmt.Sprintf("%s/%s", ctx.HomeDir, backupDirName) + srcPath := fmt.Sprintf("%s/.dnote", ctx.Paths.Home) + backupPath := fmt.Sprintf("%s/%s", ctx.Paths.Home, backupDirName) if err = os.RemoveAll(srcPath); err != nil { return errors.Wrapf(err, "Failed to clear current dnote data at %s", backupPath) @@ -198,8 +208,8 @@ func restoreBackup(ctx infra.DnoteCtx) error { return nil } -func clearBackup(ctx infra.DnoteCtx) error { - backupPath := fmt.Sprintf("%s/%s", ctx.HomeDir, backupDirName) +func clearBackup(ctx context.DnoteCtx) error { + backupPath := fmt.Sprintf("%s/%s", ctx.Paths.Home, backupDirName) if err := os.RemoveAll(backupPath); err != nil { return errors.Wrapf(err, "Failed to remove backup at %s", backupPath) @@ -209,16 +219,16 @@ func clearBackup(ctx infra.DnoteCtx) error { } // getSchemaPath returns the path to the file containing schema info -func getSchemaPath(ctx infra.DnoteCtx) string { - return fmt.Sprintf("%s/%s", ctx.DnoteDir, schemaFilename) +func getSchemaPath(ctx context.DnoteCtx) string { + return fmt.Sprintf("%s/%s", ctx.Paths.LegacyDnote, schemaFilename) } -func readSchema(ctx infra.DnoteCtx) (schema, error) { +func readSchema(ctx context.DnoteCtx) (schema, error) { var ret schema path := getSchemaPath(ctx) - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return ret, errors.Wrap(err, "Failed to read schema file") } @@ -231,21 +241,21 @@ func readSchema(ctx infra.DnoteCtx) (schema, error) { return ret, nil } -func writeSchema(ctx infra.DnoteCtx, s schema) error { +func writeSchema(ctx context.DnoteCtx, s schema) error { path := getSchemaPath(ctx) d, err := yaml.Marshal(&s) if err != nil { return errors.Wrap(err, "Failed to marshal schema into yaml") } - if err := ioutil.WriteFile(path, d, 0644); err != nil { + if err := os.WriteFile(path, d, 0644); err != nil { return errors.Wrap(err, "Failed to write schema file") } return nil } -func getUnrunMigrations(ctx infra.DnoteCtx) ([]int, error) { +func getUnrunMigrations(ctx context.DnoteCtx) ([]int, error) { var ret []int schema, err := readSchema(ctx) @@ -265,7 +275,7 @@ func getUnrunMigrations(ctx infra.DnoteCtx) ([]int, error) { return ret, nil } -func updateSchemaVersion(ctx infra.DnoteCtx, mID int) error { +func updateSchemaVersion(ctx context.DnoteCtx, mID int) error { s, err := readSchema(ctx) if err != nil { return errors.Wrap(err, "Failed to read schema") @@ -470,9 +480,13 @@ var migrateToV8SystemKeyBookMark = "bookmark" /***** migrations **/ // migrateToV1 deletes YAML archive if exists -func migrateToV1(ctx infra.DnoteCtx) error { - yamlPath := fmt.Sprintf("%s/%s", ctx.HomeDir, ".dnote-yaml-archived") - if !utils.FileExists(yamlPath) { +func migrateToV1(ctx context.DnoteCtx) error { + yamlPath := fmt.Sprintf("%s/%s", ctx.Paths.Home, ".dnote-yaml-archived") + ok, err := utils.FileExists(yamlPath) + if err != nil { + return errors.Wrap(err, "checking if yaml file exists") + } + if !ok { return nil } @@ -483,10 +497,10 @@ func migrateToV1(ctx infra.DnoteCtx) error { return nil } -func migrateToV2(ctx infra.DnoteCtx) error { - notePath := fmt.Sprintf("%s/dnote", ctx.DnoteDir) +func migrateToV2(ctx context.DnoteCtx) error { + notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(notePath) + b, err := os.ReadFile(notePath) if err != nil { return errors.Wrap(err, "Failed to read the note file") } @@ -502,8 +516,13 @@ func migrateToV2(ctx infra.DnoteCtx) error { for bookName, book := range preDnote { var notes = make([]migrateToV2PostNote, 0, len(book)) for _, note := range book { + noteUUID, err := genUUID() + if err != nil { + return errors.Wrap(err, "generating uuid") + } + newNote := migrateToV2PostNote{ - UUID: uuid.NewV4().String(), + UUID: noteUUID, Content: note.Content, AddedOn: note.AddedOn, EditedOn: 0, @@ -525,7 +544,7 @@ func migrateToV2(ctx infra.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal new dnote into JSON") } - err = ioutil.WriteFile(notePath, d, 0644) + err = os.WriteFile(notePath, d, 0644) if err != nil { return errors.Wrap(err, "Failed to write the new dnote into the file") } @@ -534,11 +553,11 @@ func migrateToV2(ctx infra.DnoteCtx) error { } // migrateToV3 generates actions for existing dnote -func migrateToV3(ctx infra.DnoteCtx) error { - notePath := fmt.Sprintf("%s/dnote", ctx.DnoteDir) - actionsPath := fmt.Sprintf("%s/actions", ctx.DnoteDir) +func migrateToV3(ctx context.DnoteCtx) error { + notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) + actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(notePath) + b, err := os.ReadFile(notePath) if err != nil { return errors.Wrap(err, "Failed to read the note file") } @@ -592,7 +611,7 @@ func migrateToV3(ctx infra.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal actions into JSON") } - err = ioutil.WriteFile(actionsPath, a, 0644) + err = os.WriteFile(actionsPath, a, 0644) if err != nil { return errors.Wrap(err, "Failed to write the actions into a file") } @@ -621,10 +640,10 @@ func getEditorCommand() string { } } -func migrateToV4(ctx infra.DnoteCtx) error { - configPath := fmt.Sprintf("%s/dnoterc", ctx.DnoteDir) +func migrateToV4(ctx context.DnoteCtx) error { + configPath := fmt.Sprintf("%s/dnoterc", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(configPath) + b, err := os.ReadFile(configPath) if err != nil { return errors.Wrap(err, "Failed to read the config file") } @@ -645,7 +664,7 @@ func migrateToV4(ctx infra.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal config into JSON") } - err = ioutil.WriteFile(configPath, data, 0644) + err = os.WriteFile(configPath, data, 0644) if err != nil { return errors.Wrap(err, "Failed to write the config into a file") } @@ -654,10 +673,10 @@ func migrateToV4(ctx infra.DnoteCtx) error { } // migrateToV5 migrates actions -func migrateToV5(ctx infra.DnoteCtx) error { - actionsPath := fmt.Sprintf("%s/actions", ctx.DnoteDir) +func migrateToV5(ctx context.DnoteCtx) error { + actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(actionsPath) + b, err := os.ReadFile(actionsPath) if err != nil { return errors.Wrap(err, "reading the actions file") } @@ -695,8 +714,13 @@ func migrateToV5(ctx infra.DnoteCtx) error { data = action.Data } + actionUUID, err := genUUID() + if err != nil { + return errors.Wrap(err, "generating UUID") + } + migrated := migrateToV5PostAction{ - UUID: uuid.NewV4().String(), + UUID: actionUUID, Schema: 1, Type: action.Type, Data: data, @@ -710,7 +734,7 @@ func migrateToV5(ctx infra.DnoteCtx) error { if err != nil { return errors.Wrap(err, "marshalling result into JSON") } - err = ioutil.WriteFile(actionsPath, a, 0644) + err = os.WriteFile(actionsPath, a, 0644) if err != nil { return errors.Wrap(err, "writing the result into a file") } @@ -719,10 +743,10 @@ func migrateToV5(ctx infra.DnoteCtx) error { } // migrateToV6 adds a 'public' field to notes -func migrateToV6(ctx infra.DnoteCtx) error { - notePath := fmt.Sprintf("%s/dnote", ctx.DnoteDir) +func migrateToV6(ctx context.DnoteCtx) error { + notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(notePath) + b, err := os.ReadFile(notePath) if err != nil { return errors.Wrap(err, "Failed to read the note file") } @@ -763,7 +787,7 @@ func migrateToV6(ctx infra.DnoteCtx) error { return errors.Wrap(err, "Failed to marshal new dnote into JSON") } - err = ioutil.WriteFile(notePath, d, 0644) + err = os.WriteFile(notePath, d, 0644) if err != nil { return errors.Wrap(err, "Failed to write the new dnote into the file") } @@ -773,11 +797,11 @@ func migrateToV6(ctx infra.DnoteCtx) error { // migrateToV7 migrates data of edit_note action to the proper version which is // EditNoteDataV2. Due to a bug, edit logged actions with schema version '2' -// but with a data of EditNoteDataV1. https://github.com/dnote/dnote/cli/issues/107 -func migrateToV7(ctx infra.DnoteCtx) error { - actionPath := fmt.Sprintf("%s/actions", ctx.DnoteDir) +// but with a data of EditNoteDataV1. https://github.com/dnote/dnote/pkg/cli/issues/107 +func migrateToV7(ctx context.DnoteCtx) error { + actionPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) - b, err := ioutil.ReadFile(actionPath) + b, err := os.ReadFile(actionPath) if err != nil { return errors.Wrap(err, "reading actions file") } @@ -829,7 +853,7 @@ func migrateToV7(ctx infra.DnoteCtx) error { return errors.Wrap(err, "marshalling new actions") } - err = ioutil.WriteFile(actionPath, d, 0644) + err = os.WriteFile(actionPath, d, 0644) if err != nil { return errors.Wrap(err, "writing new actions to a file") } @@ -838,15 +862,15 @@ func migrateToV7(ctx infra.DnoteCtx) error { } // migrateToV8 migrates dnote data to sqlite database -func migrateToV8(ctx infra.DnoteCtx) error { +func migrateToV8(ctx context.DnoteCtx) error { tx, err := ctx.DB.Begin() if err != nil { return errors.Wrap(err, "beginning a transaction") } // 1. Migrate the the dnote file - dnoteFilePath := fmt.Sprintf("%s/dnote", ctx.DnoteDir) - b, err := ioutil.ReadFile(dnoteFilePath) + dnoteFilePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) + b, err := os.ReadFile(dnoteFilePath) if err != nil { return errors.Wrap(err, "reading the notes") } @@ -858,7 +882,13 @@ func migrateToV8(ctx infra.DnoteCtx) error { } for bookName, book := range dnote { - bookUUID := uuid.NewV4().String() + bookUUIDResult, err := uuid.NewRandom() + if err != nil { + return errors.Wrap(err, "generating uuid") + } + + bookUUID := bookUUIDResult.String() + _, err = tx.Exec(`INSERT INTO books (uuid, label) VALUES (?, ?)`, bookUUID, bookName) if err != nil { tx.Rollback() @@ -879,8 +909,8 @@ func migrateToV8(ctx infra.DnoteCtx) error { } // 2. Migrate the actions file - actionsPath := fmt.Sprintf("%s/actions", ctx.DnoteDir) - b, err = ioutil.ReadFile(actionsPath) + actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) + b, err = os.ReadFile(actionsPath) if err != nil { return errors.Wrap(err, "reading the actions") } @@ -904,8 +934,8 @@ func migrateToV8(ctx infra.DnoteCtx) error { } // 3. Migrate the timestamps file - timestampsPath := fmt.Sprintf("%s/timestamps", ctx.DnoteDir) - b, err = ioutil.ReadFile(timestampsPath) + timestampsPath := fmt.Sprintf("%s/timestamps", ctx.Paths.LegacyDnote) + b, err = os.ReadFile(timestampsPath) if err != nil { return errors.Wrap(err, "reading the timestamps") } @@ -946,7 +976,7 @@ func migrateToV8(ctx infra.DnoteCtx) error { if err := os.RemoveAll(timestampsPath); err != nil { return errors.Wrap(err, "removing the timestamps file") } - schemaPath := fmt.Sprintf("%s/schema", ctx.DnoteDir) + schemaPath := fmt.Sprintf("%s/schema", ctx.Paths.LegacyDnote) if err := os.RemoveAll(schemaPath); err != nil { return errors.Wrap(err, "removing the schema file") } diff --git a/pkg/cli/migrate/legacy_test.go b/pkg/cli/migrate/legacy_test.go new file mode 100644 index 00000000..73d8b063 --- /dev/null +++ b/pkg/cli/migrate/legacy_test.go @@ -0,0 +1,656 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package migrate + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" + "gopkg.in/yaml.v2" +) + +func setupEnv(t *testing.T, homeDir string) context.DnoteCtx { + dnoteDir := fmt.Sprintf("%s/.dnote", homeDir) + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + t.Fatal(errors.Wrap(err, "preparing dnote dir")) + } + + return context.DnoteCtx{ + Paths: context.Paths{ + Home: homeDir, + LegacyDnote: dnoteDir, + }, + } +} + +func teardownEnv(t *testing.T, ctx context.DnoteCtx) { + if err := os.RemoveAll(ctx.Paths.LegacyDnote); err != nil { + t.Fatal(errors.Wrap(err, "tearing down the dnote dir")) + } +} + +func TestMigrateToV1(t *testing.T) { + t.Run("yaml exists", func(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + yamlPath, err := filepath.Abs(filepath.Join(ctx.Paths.Home, ".dnote-yaml-archived")) + if err != nil { + panic(errors.Wrap(err, "Failed to get absolute YAML path").Error()) + } + os.WriteFile(yamlPath, []byte{}, 0644) + + // execute + if err := migrateToV1(ctx); err != nil { + t.Fatal(errors.Wrapf(err, "Failed to migrate").Error()) + } + + // test + ok, err := utils.FileExists(yamlPath) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if yaml file exists")) + } + if ok { + t.Fatal("YAML archive file has not been deleted") + } + }) + + t.Run("yaml does not exist", func(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + yamlPath, err := filepath.Abs(filepath.Join(ctx.Paths.Home, ".dnote-yaml-archived")) + if err != nil { + panic(errors.Wrap(err, "Failed to get absolute YAML path").Error()) + } + + // execute + if err := migrateToV1(ctx); err != nil { + t.Fatal(errors.Wrapf(err, "Failed to migrate").Error()) + } + + // test + ok, err := utils.FileExists(yamlPath) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if yaml file exists")) + } + if ok { + t.Fatal("YAML archive file must not exist") + } + }) +} + +func TestMigrateToV2(t *testing.T) { + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + testutils.CopyFixture(t, ctx, "./fixtures/legacy-2-pre-dnote.json", "dnote") + + // execute + if err := migrateToV2(ctx); err != nil { + t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) + } + + // test + b := testutils.ReadFile(ctx, "dnote") + + var postDnote migrateToV2PostDnote + if err := json.Unmarshal(b, &postDnote); err != nil { + t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) + } + + for _, book := range postDnote { + assert.NotEqual(t, book.Name, "", "Book name was not populated") + + for _, note := range book.Notes { + if len(note.UUID) == 8 { + t.Errorf("Note UUID was not migrated. It has length of %d", len(note.UUID)) + } + + assert.NotEqual(t, note.AddedOn, int64(0), "AddedOn was not carried over") + assert.Equal(t, note.EditedOn, int64(0), "EditedOn was not created properly") + } + } +} + +func TestMigrateToV3(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + testutils.CopyFixture(t, ctx, "./fixtures/legacy-3-pre-dnote.json", "dnote") + + // execute + if err := migrateToV3(ctx); err != nil { + t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) + } + + // test + b := testutils.ReadFile(ctx, "dnote") + var postDnote migrateToV3Dnote + if err := json.Unmarshal(b, &postDnote); err != nil { + t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) + } + + b = testutils.ReadFile(ctx, "actions") + var actions []migrateToV3Action + if err := json.Unmarshal(b, &actions); err != nil { + t.Fatal(errors.Wrap(err, "Failed to unmarshal the actions").Error()) + } + + assert.Equal(t, len(actions), 6, "actions length mismatch") + + for _, book := range postDnote { + for _, note := range book.Notes { + assert.NotEqual(t, note.AddedOn, int64(0), "AddedOn was not carried over") + } + } +} + +func TestMigrateToV4(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + defer os.Setenv("EDITOR", "") + + testutils.CopyFixture(t, ctx, "./fixtures/legacy-4-pre-dnoterc.yaml", "dnoterc") + + // execute + os.Setenv("EDITOR", "vim") + if err := migrateToV4(ctx); err != nil { + t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) + } + + // test + b := testutils.ReadFile(ctx, "dnoterc") + var config migrateToV4PostConfig + if err := yaml.Unmarshal(b, &config); err != nil { + t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) + } + + assert.Equal(t, config.APIKey, "Oev6e1082ORasdf9rjkfjkasdfjhgei", "api key mismatch") + assert.Equal(t, config.Editor, "vim", "editor mismatch") +} + +func TestMigrateToV5(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + testutils.CopyFixture(t, ctx, "./fixtures/legacy-5-pre-actions.json", "actions") + + // execute + if err := migrateToV5(ctx); err != nil { + t.Fatal(errors.Wrap(err, "migrating").Error()) + } + + // test + var oldActions []migrateToV5PreAction + testutils.ReadJSON("./fixtures/legacy-5-pre-actions.json", &oldActions) + + b := testutils.ReadFile(ctx, "actions") + var migratedActions []migrateToV5PostAction + if err := json.Unmarshal(b, &migratedActions); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling migrated actions").Error()) + } + + if len(oldActions) != len(migratedActions) { + t.Fatalf("There were %d actions but after migration there were %d", len(oldActions), len(migratedActions)) + } + + for idx := range migratedActions { + migrated := migratedActions[idx] + old := oldActions[idx] + + assert.NotEqual(t, migrated.UUID, "", fmt.Sprintf("uuid mismatch for migrated item with index %d", idx)) + assert.Equal(t, migrated.Schema, 1, fmt.Sprintf("schema mismatch for migrated item with index %d", idx)) + assert.Equal(t, migrated.Timestamp, old.Timestamp, fmt.Sprintf("timestamp mismatch for migrated item with index %d", idx)) + assert.Equal(t, migrated.Type, old.Type, fmt.Sprintf("timestamp mismatch for migrated item with index %d", idx)) + + switch migrated.Type { + case migrateToV5ActionAddNote: + var oldData, migratedData migrateToV5AddNoteData + if err := json.Unmarshal(old.Data, &oldData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) + } + if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) + } + + assert.Equal(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) + assert.Equal(t, oldData.Content, migratedData.Content, fmt.Sprintf("data content mismatch for item idx %d", idx)) + assert.Equal(t, oldData.NoteUUID, migratedData.NoteUUID, fmt.Sprintf("data note_uuid mismatch for item idx %d", idx)) + case migrateToV5ActionRemoveNote: + var oldData, migratedData migrateToV5RemoveNoteData + if err := json.Unmarshal(old.Data, &oldData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) + } + if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) + } + + assert.Equal(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) + assert.Equal(t, oldData.NoteUUID, migratedData.NoteUUID, fmt.Sprintf("data note_uuid mismatch for item idx %d", idx)) + case migrateToV5ActionAddBook: + var oldData, migratedData migrateToV5AddBookData + if err := json.Unmarshal(old.Data, &oldData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) + } + if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) + } + + assert.Equal(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) + case migrateToV5ActionRemoveBook: + var oldData, migratedData migrateToV5RemoveBookData + if err := json.Unmarshal(old.Data, &oldData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) + } + if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) + } + + assert.Equal(t, oldData.BookName, migratedData.BookName, fmt.Sprintf("data book_name mismatch for item idx %d", idx)) + case migrateToV5ActionEditNote: + var oldData migrateToV5PreEditNoteData + var migratedData migrateToV5PostEditNoteData + if err := json.Unmarshal(old.Data, &oldData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling old data").Error()) + } + if err := json.Unmarshal(migrated.Data, &migratedData); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling new data").Error()) + } + + assert.Equal(t, oldData.NoteUUID, migratedData.NoteUUID, fmt.Sprintf("data note_uuid mismatch for item idx %d", idx)) + assert.Equal(t, oldData.Content, migratedData.Content, fmt.Sprintf("data content mismatch for item idx %d", idx)) + assert.Equal(t, oldData.BookName, migratedData.FromBook, "book_name should have been renamed to from_book") + assert.Equal(t, migratedData.ToBook, "", "to_book should be empty") + } + } +} + +func TestMigrateToV6(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + testutils.CopyFixture(t, ctx, "./fixtures/legacy-6-pre-dnote.json", "dnote") + + // execute + if err := migrateToV6(ctx); err != nil { + t.Fatal(errors.Wrap(err, "Failed to migrate").Error()) + } + + // test + b := testutils.ReadFile(ctx, "dnote") + var got migrateToV6PostDnote + if err := json.Unmarshal(b, &got); err != nil { + t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) + } + + b = utils.ReadFileAbs("./fixtures/legacy-6-post-dnote.json") + var expected migrateToV6PostDnote + if err := json.Unmarshal(b, &expected); err != nil { + t.Fatal(errors.Wrap(err, "Failed to unmarshal the result into Dnote").Error()) + } + + assert.DeepEqual(t, expected, got, "payload mismatch") +} + +func TestMigrateToV7(t *testing.T) { + // set up + ctx := setupEnv(t, "../tmp") + defer teardownEnv(t, ctx) + + testutils.CopyFixture(t, ctx, "./fixtures/legacy-7-pre-actions.json", "actions") + + // execute + if err := migrateToV7(ctx); err != nil { + t.Fatal(errors.Wrap(err, "migrating").Error()) + } + + // test + b := testutils.ReadFile(ctx, "actions") + var got []migrateToV7Action + if err := json.Unmarshal(b, &got); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling the result").Error()) + } + + b2 := utils.ReadFileAbs("./fixtures/legacy-7-post-actions.json") + var expected []migrateToV7Action + if err := json.Unmarshal(b, &expected); err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling the result into Dnote").Error()) + } + + assert.EqualJSON(t, string(b), string(b2), "Result does not match") +} + +func TestMigrateToV8(t *testing.T) { + tmpDir := t.TempDir() + dnoteDir := tmpDir + "/.dnote" + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + t.Fatal(errors.Wrap(err, "creating legacy dnote directory")) + } + + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + + ctx := context.DnoteCtx{ + Paths: context.Paths{ + Home: tmpDir, + LegacyDnote: dnoteDir, + }, + DB: db, + } + + // set up + testutils.CopyFixture(t, ctx, "./fixtures/legacy-8-actions.json", "actions") + testutils.CopyFixture(t, ctx, "./fixtures/legacy-8-dnote.json", "dnote") + testutils.CopyFixture(t, ctx, "./fixtures/legacy-8-dnoterc.yaml", "dnoterc") + testutils.CopyFixture(t, ctx, "./fixtures/legacy-8-schema.yaml", "schema") + testutils.CopyFixture(t, ctx, "./fixtures/legacy-8-timestamps.yaml", "timestamps") + + // execute + if err := migrateToV8(ctx); err != nil { + t.Fatal(errors.Wrap(err, "migrating").Error()) + } + + // test + + // 1. test if files are migrated + dnoteFilePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) + dnotercPath := fmt.Sprintf("%s/dnoterc", ctx.Paths.LegacyDnote) + schemaFilePath := fmt.Sprintf("%s/schema", ctx.Paths.LegacyDnote) + timestampFilePath := fmt.Sprintf("%s/timestamps", ctx.Paths.LegacyDnote) + + ok, err := utils.FileExists(dnoteFilePath) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if file exists")) + } + if ok { + t.Errorf("%s still exists", dnoteFilePath) + } + + ok, err = utils.FileExists(schemaFilePath) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if file exists")) + } + if ok { + t.Errorf("%s still exists", schemaFilePath) + } + + ok, err = utils.FileExists(timestampFilePath) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if file exists")) + } + if ok { + t.Errorf("%s still exists", timestampFilePath) + } + + ok, err = utils.FileExists(dnotercPath) + if err != nil { + t.Fatal(errors.Wrap(err, "checking if file exists")) + } + if !ok { + t.Errorf("%s still exists", dnotercPath) + } + + // 2. test if notes and books are migrated + + var bookCount, noteCount int + err = db.QueryRow("SELECT count(*) FROM books").Scan(&bookCount) + if err != nil { + panic(errors.Wrap(err, "counting books")) + } + err = db.QueryRow("SELECT count(*) FROM notes").Scan(¬eCount) + if err != nil { + panic(errors.Wrap(err, "counting notes")) + } + assert.Equal(t, bookCount, 2, "book count mismatch") + assert.Equal(t, noteCount, 3, "note count mismatch") + + type bookInfo struct { + label string + uuid string + } + type noteInfo struct { + id int + uuid string + bookUUID string + content string + addedOn int64 + editedOn int64 + public bool + } + + var b1, b2 bookInfo + var n1, n2, n3 noteInfo + err = db.QueryRow("SELECT label, uuid FROM books WHERE label = ?", "js").Scan(&b1.label, &b1.uuid) + if err != nil { + panic(errors.Wrap(err, "finding book 1")) + } + err = db.QueryRow("SELECT label, uuid FROM books WHERE label = ?", "css").Scan(&b2.label, &b2.uuid) + if err != nil { + panic(errors.Wrap(err, "finding book 2")) + } + err = db.QueryRow("SELECT id, uuid, book_uuid, content, added_on, edited_on, public FROM notes WHERE uuid = ?", "d69edb54-5b31-4cdd-a4a5-34f0a0bfa153").Scan(&n1.id, &n1.uuid, &n1.bookUUID, &n1.content, &n1.addedOn, &n1.editedOn, &n1.public) + if err != nil { + panic(errors.Wrap(err, "finding note 1")) + } + err = db.QueryRow("SELECT id, uuid, book_uuid, content, added_on, edited_on, public FROM notes WHERE uuid = ?", "35cbcab1-6a2a-4cc8-97e0-e73bbbd54626").Scan(&n2.id, &n2.uuid, &n2.bookUUID, &n2.content, &n2.addedOn, &n2.editedOn, &n2.public) + if err != nil { + panic(errors.Wrap(err, "finding note 2")) + } + err = db.QueryRow("SELECT id, uuid, book_uuid, content, added_on, edited_on, public FROM notes WHERE uuid = ?", "7c1fcfb2-de8b-4350-88f0-fb3cbaf6630a").Scan(&n3.id, &n3.uuid, &n3.bookUUID, &n3.content, &n3.addedOn, &n3.editedOn, &n3.public) + if err != nil { + panic(errors.Wrap(err, "finding note 3")) + } + + assert.NotEqual(t, b1.uuid, "", "book 1 uuid should have been generated") + assert.Equal(t, b1.label, "js", "book 1 label mismatch") + assert.NotEqual(t, b2.uuid, "", "book 2 uuid should have been generated") + assert.Equal(t, b2.label, "css", "book 2 label mismatch") + + assert.Equal(t, n1.uuid, "d69edb54-5b31-4cdd-a4a5-34f0a0bfa153", "note 1 uuid mismatch") + assert.NotEqual(t, n1.id, 0, "note 1 id should have been generated") + assert.Equal(t, n1.bookUUID, b2.uuid, "note 1 book_uuid mismatch") + assert.Equal(t, n1.content, "css test 1", "note 1 content mismatch") + assert.Equal(t, n1.addedOn, int64(1536977237), "note 1 added_on mismatch") + assert.Equal(t, n1.editedOn, int64(1536977253), "note 1 edited_on mismatch") + assert.Equal(t, n1.public, false, "note 1 public mismatch") + + assert.Equal(t, n2.uuid, "35cbcab1-6a2a-4cc8-97e0-e73bbbd54626", "note 2 uuid mismatch") + assert.NotEqual(t, n2.id, 0, "note 2 id should have been generated") + assert.Equal(t, n2.bookUUID, b1.uuid, "note 2 book_uuid mismatch") + assert.Equal(t, n2.content, "js test 1", "note 2 content mismatch") + assert.Equal(t, n2.addedOn, int64(1536977229), "note 2 added_on mismatch") + assert.Equal(t, n2.editedOn, int64(0), "note 2 edited_on mismatch") + assert.Equal(t, n2.public, false, "note 2 public mismatch") + + assert.Equal(t, n3.uuid, "7c1fcfb2-de8b-4350-88f0-fb3cbaf6630a", "note 3 uuid mismatch") + assert.NotEqual(t, n3.id, 0, "note 3 id should have been generated") + assert.Equal(t, n3.bookUUID, b1.uuid, "note 3 book_uuid mismatch") + assert.Equal(t, n3.content, "js test 2", "note 3 content mismatch") + assert.Equal(t, n3.addedOn, int64(1536977230), "note 3 added_on mismatch") + assert.Equal(t, n3.editedOn, int64(0), "note 3 edited_on mismatch") + assert.Equal(t, n3.public, false, "note 3 public mismatch") + + // 3. test if actions are migrated + var actionCount int + err = db.QueryRow("SELECT count(*) FROM actions").Scan(&actionCount) + if err != nil { + panic(errors.Wrap(err, "counting actions")) + } + + assert.Equal(t, actionCount, 11, "action count mismatch") + + type actionInfo struct { + uuid string + schema int + actionType string + data string + timestamp int + } + + var a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11 actionInfo + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "6145c1b7-f286-4d9f-b0f6-00d274baefc6").Scan(&a1.uuid, &a1.schema, &a1.actionType, &a1.data, &a1.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a1")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "c048a56b-179c-4f31-9995-81e9b32b7dd6").Scan(&a2.uuid, &a2.schema, &a2.actionType, &a2.data, &a2.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a2")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "f557ef48-c304-47dc-adfb-46b7306e701f").Scan(&a3.uuid, &a3.schema, &a3.actionType, &a3.data, &a3.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a3")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "8d79db34-343d-4331-ae5b-24743f17ca7f").Scan(&a4.uuid, &a4.schema, &a4.actionType, &a4.data, &a4.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a4")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "b9c1ed4a-e6b3-41f2-983b-593ec7b8b7a1").Scan(&a5.uuid, &a5.schema, &a5.actionType, &a5.data, &a5.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a5")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "06ed7ef0-f171-4bd7-ae8e-97b5d06a4c49").Scan(&a6.uuid, &a6.schema, &a6.actionType, &a6.data, &a6.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a6")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "7f173cef-1688-4177-a373-145fcd822b2f").Scan(&a7.uuid, &a7.schema, &a7.actionType, &a7.data, &a7.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a7")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "64352e08-aa7a-45f4-b760-b3f38b5e11fa").Scan(&a8.uuid, &a8.schema, &a8.actionType, &a8.data, &a8.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a8")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "82e20a12-bda8-45f7-ac42-b453b6daa5ec").Scan(&a9.uuid, &a9.schema, &a9.actionType, &a9.data, &a9.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a9")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "a29055f4-ace4-44fd-8800-3396edbccaef").Scan(&a10.uuid, &a10.schema, &a10.actionType, &a10.data, &a10.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a10")) + } + err = db.QueryRow("SELECT uuid, schema, type, data, timestamp FROM actions WHERE uuid = ?", "871a5562-1bd0-43c1-b550-5bbb727ac7c4").Scan(&a11.uuid, &a11.schema, &a11.actionType, &a11.data, &a11.timestamp) + if err != nil { + panic(errors.Wrap(err, "finding a11")) + } + + assert.Equal(t, a1.uuid, "6145c1b7-f286-4d9f-b0f6-00d274baefc6", "action 1 uuid mismatch") + assert.Equal(t, a1.schema, 1, "action 1 schema mismatch") + assert.Equal(t, a1.actionType, "add_book", "action 1 type mismatch") + assert.Equal(t, a1.data, `{"book_name":"js"}`, "action 1 data mismatch") + assert.Equal(t, a1.timestamp, 1536977229, "action 1 timestamp mismatch") + + assert.Equal(t, a2.uuid, "c048a56b-179c-4f31-9995-81e9b32b7dd6", "action 2 uuid mismatch") + assert.Equal(t, a2.schema, 2, "action 2 schema mismatch") + assert.Equal(t, a2.actionType, "add_note", "action 2 type mismatch") + assert.Equal(t, a2.data, `{"note_uuid":"35cbcab1-6a2a-4cc8-97e0-e73bbbd54626","book_name":"js","content":"js test 1","public":false}`, "action 2 data mismatch") + assert.Equal(t, a2.timestamp, 1536977229, "action 2 timestamp mismatch") + + assert.Equal(t, a3.uuid, "f557ef48-c304-47dc-adfb-46b7306e701f", "action 3 uuid mismatch") + assert.Equal(t, a3.schema, 2, "action 3 schema mismatch") + assert.Equal(t, a3.actionType, "add_note", "action 3 type mismatch") + assert.Equal(t, a3.data, `{"note_uuid":"7c1fcfb2-de8b-4350-88f0-fb3cbaf6630a","book_name":"js","content":"js test 2","public":false}`, "action 3 data mismatch") + assert.Equal(t, a3.timestamp, 1536977230, "action 3 timestamp mismatch") + + assert.Equal(t, a4.uuid, "8d79db34-343d-4331-ae5b-24743f17ca7f", "action 4 uuid mismatch") + assert.Equal(t, a4.schema, 2, "action 4 schema mismatch") + assert.Equal(t, a4.actionType, "add_note", "action 4 type mismatch") + assert.Equal(t, a4.data, `{"note_uuid":"b23a88ba-b291-4294-9795-86b394db5dcf","book_name":"js","content":"js test 3","public":false}`, "action 4 data mismatch") + assert.Equal(t, a4.timestamp, 1536977234, "action 4 timestamp mismatch") + + assert.Equal(t, a5.uuid, "b9c1ed4a-e6b3-41f2-983b-593ec7b8b7a1", "action 5 uuid mismatch") + assert.Equal(t, a5.schema, 1, "action 5 schema mismatch") + assert.Equal(t, a5.actionType, "add_book", "action 5 type mismatch") + assert.Equal(t, a5.data, `{"book_name":"css"}`, "action 5 data mismatch") + assert.Equal(t, a5.timestamp, 1536977237, "action 5 timestamp mismatch") + + assert.Equal(t, a6.uuid, "06ed7ef0-f171-4bd7-ae8e-97b5d06a4c49", "action 6 uuid mismatch") + assert.Equal(t, a6.schema, 2, "action 6 schema mismatch") + assert.Equal(t, a6.actionType, "add_note", "action 6 type mismatch") + assert.Equal(t, a6.data, `{"note_uuid":"d69edb54-5b31-4cdd-a4a5-34f0a0bfa153","book_name":"css","content":"js test 3","public":false}`, "action 6 data mismatch") + assert.Equal(t, a6.timestamp, 1536977237, "action 6 timestamp mismatch") + + assert.Equal(t, a7.uuid, "7f173cef-1688-4177-a373-145fcd822b2f", "action 7 uuid mismatch") + assert.Equal(t, a7.schema, 2, "action 7 schema mismatch") + assert.Equal(t, a7.actionType, "edit_note", "action 7 type mismatch") + assert.Equal(t, a7.data, `{"note_uuid":"d69edb54-5b31-4cdd-a4a5-34f0a0bfa153","from_book":"css","to_book":null,"content":"css test 1","public":null}`, "action 7 data mismatch") + assert.Equal(t, a7.timestamp, 1536977253, "action 7 timestamp mismatch") + + assert.Equal(t, a8.uuid, "64352e08-aa7a-45f4-b760-b3f38b5e11fa", "action 8 uuid mismatch") + assert.Equal(t, a8.schema, 1, "action 8 schema mismatch") + assert.Equal(t, a8.actionType, "add_book", "action 8 type mismatch") + assert.Equal(t, a8.data, `{"book_name":"sql"}`, "action 8 data mismatch") + assert.Equal(t, a8.timestamp, 1536977261, "action 8 timestamp mismatch") + + assert.Equal(t, a9.uuid, "82e20a12-bda8-45f7-ac42-b453b6daa5ec", "action 9 uuid mismatch") + assert.Equal(t, a9.schema, 2, "action 9 schema mismatch") + assert.Equal(t, a9.actionType, "add_note", "action 9 type mismatch") + assert.Equal(t, a9.data, `{"note_uuid":"2f47d390-685b-4b84-89ac-704c6fb8d3fb","book_name":"sql","content":"blah","public":false}`, "action 9 data mismatch") + assert.Equal(t, a9.timestamp, 1536977261, "action 9 timestamp mismatch") + + assert.Equal(t, a10.uuid, "a29055f4-ace4-44fd-8800-3396edbccaef", "action 10 uuid mismatch") + assert.Equal(t, a10.schema, 1, "action 10 schema mismatch") + assert.Equal(t, a10.actionType, "remove_book", "action 10 type mismatch") + assert.Equal(t, a10.data, `{"book_name":"sql"}`, "action 10 data mismatch") + assert.Equal(t, a10.timestamp, 1536977268, "action 10 timestamp mismatch") + + assert.Equal(t, a11.uuid, "871a5562-1bd0-43c1-b550-5bbb727ac7c4", "action 11 uuid mismatch") + assert.Equal(t, a11.schema, 1, "action 11 schema mismatch") + assert.Equal(t, a11.actionType, "remove_note", "action 11 type mismatch") + assert.Equal(t, a11.data, `{"note_uuid":"b23a88ba-b291-4294-9795-86b394db5dcf","book_name":"js"}`, "action 11 data mismatch") + assert.Equal(t, a11.timestamp, 1536977274, "action 11 timestamp mismatch") + + // 3. test if system is migrated + var systemCount int + err = db.QueryRow("SELECT count(*) FROM system").Scan(&systemCount) + if err != nil { + panic(errors.Wrap(err, "counting system")) + } + + assert.Equal(t, systemCount, 3, "action count mismatch") + + var lastUpgrade, lastAction, bookmark int + err = db.QueryRow("SELECT value FROM system WHERE key = ?", "last_upgrade").Scan(&lastUpgrade) + if err != nil { + panic(errors.Wrap(err, "finding last_upgrade")) + } + err = db.QueryRow("SELECT value FROM system WHERE key = ?", "last_action").Scan(&lastAction) + if err != nil { + panic(errors.Wrap(err, "finding last_action")) + } + err = db.QueryRow("SELECT value FROM system WHERE key = ?", "bookmark").Scan(&bookmark) + if err != nil { + panic(errors.Wrap(err, "finding bookmark")) + } + + assert.Equal(t, lastUpgrade, 1536977220, "last_upgrade mismatch") + assert.Equal(t, lastAction, 1536977274, "last_action mismatch") + assert.Equal(t, bookmark, 9, "bookmark mismatch") +} diff --git a/cli/migrate/migrate.go b/pkg/cli/migrate/migrate.go similarity index 66% rename from cli/migrate/migrate.go rename to pkg/cli/migrate/migrate.go index b604dedb..9b4b0f50 100644 --- a/cli/migrate/migrate.go +++ b/pkg/cli/migrate/migrate.go @@ -1,27 +1,26 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrate import ( "database/sql" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/log" "github.com/pkg/errors" ) @@ -43,6 +42,11 @@ var LocalSequence = []migration{ lm7, lm8, lm9, + lm10, + lm11, + lm12, + lm13, + lm14, } // RemoteSequence is a list of remote migrations to be run @@ -50,7 +54,7 @@ var RemoteSequence = []migration{ rm1, } -func initSchema(ctx infra.DnoteCtx, schemaKey string) (int, error) { +func initSchema(ctx context.DnoteCtx, schemaKey string) (int, error) { // schemaVersion is the index of the latest run migration in the sequence schemaVersion := 0 @@ -65,17 +69,17 @@ func initSchema(ctx infra.DnoteCtx, schemaKey string) (int, error) { func getSchemaKey(mode int) (string, error) { if mode == LocalMode { - return infra.SystemSchema, nil + return consts.SystemSchema, nil } if mode == RemoteMode { - return infra.SystemRemoteSchema, nil + return consts.SystemRemoteSchema, nil } return "", errors.Errorf("unsupported migration type '%d'", mode) } -func getSchema(ctx infra.DnoteCtx, schemaKey string) (int, error) { +func getSchema(ctx context.DnoteCtx, schemaKey string) (int, error) { var ret int db := ctx.DB @@ -93,7 +97,7 @@ func getSchema(ctx infra.DnoteCtx, schemaKey string) (int, error) { return ret, nil } -func execute(ctx infra.DnoteCtx, m migration, schemaKey string) error { +func execute(ctx context.DnoteCtx, m migration, schemaKey string) error { log.Debug("running migration %s\n", m.name) tx, err := ctx.DB.Begin() @@ -126,7 +130,7 @@ func execute(ctx infra.DnoteCtx, m migration, schemaKey string) error { } // Run performs unrun migrations -func Run(ctx infra.DnoteCtx, migrations []migration, mode int) error { +func Run(ctx context.DnoteCtx, migrations []migration, mode int) error { schemaKey, err := getSchemaKey(mode) if err != nil { return errors.Wrap(err, "getting schema key") @@ -137,7 +141,7 @@ func Run(ctx infra.DnoteCtx, migrations []migration, mode int) error { return errors.Wrap(err, "getting the current schema") } - log.Debug("current schema: %s %d of %d\n", infra.SystemSchema, schema, len(migrations)) + log.Debug("%s: %d of %d\n", schemaKey, schema, len(migrations)) toRun := migrations[schema:] diff --git a/pkg/cli/migrate/migrate_test.go b/pkg/cli/migrate/migrate_test.go new file mode 100644 index 00000000..7b31a91e --- /dev/null +++ b/pkg/cli/migrate/migrate_test.go @@ -0,0 +1,1217 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package migrate + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "gopkg.in/yaml.v2" + + "github.com/dnote/actions" + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/pkg/errors" +) + +// initTestDBNoMigration initializes a test database with schema.sql but removes +// migration version data so tests can control the migration state themselves. +func initTestDBNoMigration(t *testing.T) *database.DB { + db := database.InitTestMemoryDBRaw(t, "") + // Remove migration versions from schema.sql so tests can set their own + database.MustExec(t, "clearing schema versions", db, "DELETE FROM system WHERE key IN (?, ?)", consts.SystemSchema, consts.SystemRemoteSchema) + return db +} + +func TestExecute_bump_schema(t *testing.T) { + testCases := []struct { + schemaKey string + }{ + { + schemaKey: consts.SystemSchema, + }, + { + schemaKey: consts.SystemRemoteSchema, + }, + } + + for _, tc := range testCases { + func() { + // set up + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) + + database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 8) + + m1 := migration{ + name: "noop", + run: func(ctx context.DnoteCtx, db *database.DB) error { + return nil + }, + } + m2 := migration{ + name: "noop", + run: func(ctx context.DnoteCtx, db *database.DB) error { + return nil + }, + } + + // execute + err := execute(ctx, m1, tc.schemaKey) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to execute")) + } + err = execute(ctx, m2, tc.schemaKey) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to execute")) + } + + // test + var schema int + database.MustScan(t, "getting schema", db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) + assert.Equal(t, schema, 10, "schema was not incremented properly") + }() + } +} + +func TestRun_nonfresh(t *testing.T) { + testCases := []struct { + mode int + schemaKey string + }{ + { + mode: LocalMode, + schemaKey: consts.SystemSchema, + }, + { + mode: RemoteMode, + schemaKey: consts.SystemRemoteSchema, + }, + } + + for _, tc := range testCases { + func() { + // set up + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) + database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 2) + database.MustExec(t, "creating a temporary table for testing", db, + "CREATE TABLE migrate_run_test ( name string )") + + sequence := []migration{ + { + name: "v1", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v1 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v1") + return nil + }, + }, + { + name: "v2", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v2 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v2") + return nil + }, + }, + { + name: "v3", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v3 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v3") + return nil + }, + }, + { + name: "v4", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v4 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v4") + return nil + }, + }, + } + + // execute + err := Run(ctx, sequence, tc.mode) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + var schema int + database.MustScan(t, fmt.Sprintf("getting schema for %s", tc.schemaKey), db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) + assert.Equal(t, schema, 4, fmt.Sprintf("schema was not updated for %s", tc.schemaKey)) + + var testRunCount int + database.MustScan(t, "counting test runs", db.QueryRow("SELECT count(*) FROM migrate_run_test"), &testRunCount) + assert.Equal(t, testRunCount, 2, "test run count mismatch") + + var testRun1, testRun2 string + database.MustScan(t, "finding test run 1", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v3"), &testRun1) + database.MustScan(t, "finding test run 2", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v4"), &testRun2) + }() + } +} + +func TestRun_fresh(t *testing.T) { + testCases := []struct { + mode int + schemaKey string + }{ + { + mode: LocalMode, + schemaKey: consts.SystemSchema, + }, + { + mode: RemoteMode, + schemaKey: consts.SystemRemoteSchema, + }, + } + + for _, tc := range testCases { + func() { + // set up + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) + + database.MustExec(t, "creating a temporary table for testing", db, + "CREATE TABLE migrate_run_test ( name string )") + + sequence := []migration{ + { + name: "v1", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v1 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v1") + return nil + }, + }, + { + name: "v2", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v2 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v2") + return nil + }, + }, + { + name: "v3", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v3 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v3") + return nil + }, + }, + } + + // execute + err := Run(ctx, sequence, tc.mode) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + var schema int + database.MustScan(t, "getting schema", db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) + assert.Equal(t, schema, 3, "schema was not updated") + + var testRunCount int + database.MustScan(t, "counting test runs", db.QueryRow("SELECT count(*) FROM migrate_run_test"), &testRunCount) + assert.Equal(t, testRunCount, 3, "test run count mismatch") + + var testRun1, testRun2, testRun3 string + database.MustScan(t, "finding test run 1", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v1"), &testRun1) + database.MustScan(t, "finding test run 2", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v2"), &testRun2) + database.MustScan(t, "finding test run 2", db.QueryRow("SELECT name FROM migrate_run_test WHERE name = ?", "v3"), &testRun3) + }() + } +} + +func TestRun_up_to_date(t *testing.T) { + testCases := []struct { + mode int + schemaKey string + }{ + { + mode: LocalMode, + schemaKey: consts.SystemSchema, + }, + { + mode: RemoteMode, + schemaKey: consts.SystemRemoteSchema, + }, + } + + for _, tc := range testCases { + func() { + // set up + db := initTestDBNoMigration(t) + ctx := context.InitTestCtxWithDB(t, db) + + database.MustExec(t, "creating a temporary table for testing", db, + "CREATE TABLE migrate_run_test ( name string )") + + database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 3) + + sequence := []migration{ + { + name: "v1", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v1 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v1") + return nil + }, + }, + { + name: "v2", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v2 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v2") + return nil + }, + }, + { + name: "v3", + run: func(ctx context.DnoteCtx, db *database.DB) error { + database.MustExec(t, "marking v3 completed", db, "INSERT INTO migrate_run_test (name) VALUES (?)", "v3") + return nil + }, + }, + } + + // execute + err := Run(ctx, sequence, tc.mode) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + var schema int + database.MustScan(t, "getting schema", db.QueryRow("SELECT value FROM system WHERE key = ?", tc.schemaKey), &schema) + assert.Equal(t, schema, 3, "schema was not updated") + + var testRunCount int + database.MustScan(t, "counting test runs", db.QueryRow("SELECT count(*) FROM migrate_run_test"), &testRunCount) + assert.Equal(t, testRunCount, 0, "test run count mismatch") + }() + } +} + +func TestLocalMigration1(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) + a1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 1, "add_book", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.EditNoteDataV1{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: "", Content: "note 1"}) + a2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 1, "edit_note", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.EditNoteDataV1{NoteUUID: "note-2-uuid", FromBook: "js", ToBook: "", Content: "note 2"}) + a3UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 1, "edit_note", string(data), 1537829463) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm1.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var actionCount int + database.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount) + assert.Equal(t, actionCount, 3, "action count mismatch") + + var a1, a2, a3 actions.Action + var a1DataRaw, a2DataRaw, a3DataRaw string + database.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID), + &a1.Schema, &a1.Type, &a1DataRaw, &a1.Timestamp) + database.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID), + &a2.Schema, &a2.Type, &a2DataRaw, &a2.Timestamp) + database.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID), + &a3.Schema, &a3.Type, &a3DataRaw, &a3.Timestamp) + + var a1Data actions.AddBookDataV1 + var a2Data, a3Data actions.EditNoteDataV3 + testutils.MustUnmarshalJSON(t, []byte(a1DataRaw), &a1Data) + testutils.MustUnmarshalJSON(t, []byte(a2DataRaw), &a2Data) + testutils.MustUnmarshalJSON(t, []byte(a3DataRaw), &a3Data) + + assert.Equal(t, a1.Schema, 1, "a1 schema mismatch") + assert.Equal(t, a1.Type, "add_book", "a1 type mismatch") + assert.Equal(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch") + assert.Equal(t, a1Data.BookName, "js", "a1 data book_name mismatch") + + assert.Equal(t, a2.Schema, 3, "a2 schema mismatch") + assert.Equal(t, a2.Type, "edit_note", "a2 type mismatch") + assert.Equal(t, a2.Timestamp, int64(1537829463), "a2 timestamp mismatch") + assert.Equal(t, a2Data.NoteUUID, "note-1-uuid", "a2 data note_uuid mismatch") + assert.Equal(t, a2Data.BookName, (*string)(nil), "a2 data book_name mismatch") + assert.Equal(t, *a2Data.Content, "note 1", "a2 data content mismatch") + assert.Equal(t, *a2Data.Public, false, "a2 data public mismatch") + + assert.Equal(t, a3.Schema, 3, "a3 schema mismatch") + assert.Equal(t, a3.Type, "edit_note", "a3 type mismatch") + assert.Equal(t, a3.Timestamp, int64(1537829463), "a3 timestamp mismatch") + assert.Equal(t, a3Data.NoteUUID, "note-2-uuid", "a3 data note_uuid mismatch") + assert.Equal(t, a3Data.BookName, (*string)(nil), "a3 data book_name mismatch") + assert.Equal(t, *a3Data.Content, "note 2", "a3 data content mismatch") + assert.Equal(t, *a3Data.Public, false, "a3 data public mismatch") +} + +func TestLocalMigration2(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + c1 := "note 1 - v1" + c2 := "note 1 - v2" + css := "css" + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") + + data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false}) + a1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 2, "add_note", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.EditNoteDataV2{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: nil, Content: &c1, Public: nil}) + a2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 2, "edit_note", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.EditNoteDataV2{NoteUUID: "note-1-uuid", FromBook: "js", ToBook: &css, Content: &c2, Public: nil}) + a3UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 2, "edit_note", string(data), 1537829463) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm2.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var actionCount int + database.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount) + assert.Equal(t, actionCount, 3, "action count mismatch") + + var a1, a2, a3 actions.Action + var a1DataRaw, a2DataRaw, a3DataRaw string + database.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID), + &a1.Schema, &a1.Type, &a1DataRaw, &a1.Timestamp) + database.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID), + &a2.Schema, &a2.Type, &a2DataRaw, &a2.Timestamp) + database.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID), + &a3.Schema, &a3.Type, &a3DataRaw, &a3.Timestamp) + + var a1Data actions.AddNoteDataV2 + var a2Data, a3Data actions.EditNoteDataV3 + testutils.MustUnmarshalJSON(t, []byte(a1DataRaw), &a1Data) + testutils.MustUnmarshalJSON(t, []byte(a2DataRaw), &a2Data) + testutils.MustUnmarshalJSON(t, []byte(a3DataRaw), &a3Data) + + assert.Equal(t, a1.Schema, 2, "a1 schema mismatch") + assert.Equal(t, a1.Type, "add_note", "a1 type mismatch") + assert.Equal(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch") + assert.Equal(t, a1Data.NoteUUID, "note-1-uuid", "a1 data note_uuid mismatch") + assert.Equal(t, a1Data.BookName, "js", "a1 data book_name mismatch") + assert.Equal(t, a1Data.Public, false, "a1 data public mismatch") + + assert.Equal(t, a2.Schema, 3, "a2 schema mismatch") + assert.Equal(t, a2.Type, "edit_note", "a2 type mismatch") + assert.Equal(t, a2.Timestamp, int64(1537829463), "a2 timestamp mismatch") + assert.Equal(t, a2Data.NoteUUID, "note-1-uuid", "a2 data note_uuid mismatch") + assert.Equal(t, a2Data.BookName, (*string)(nil), "a2 data book_name mismatch") + assert.Equal(t, *a2Data.Content, c1, "a2 data content mismatch") + assert.Equal(t, a2Data.Public, (*bool)(nil), "a2 data public mismatch") + + assert.Equal(t, a3.Schema, 3, "a3 schema mismatch") + assert.Equal(t, a3.Type, "edit_note", "a3 type mismatch") + assert.Equal(t, a3.Timestamp, int64(1537829463), "a3 timestamp mismatch") + assert.Equal(t, a3Data.NoteUUID, "note-1-uuid", "a3 data note_uuid mismatch") + assert.Equal(t, *a3Data.BookName, "css", "a3 data book_name mismatch") + assert.Equal(t, *a3Data.Content, c2, "a3 data content mismatch") + assert.Equal(t, a3Data.Public, (*bool)(nil), "a3 data public mismatch") +} + +func TestLocalMigration3(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + data := testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: "note-1-uuid", BookName: "js", Content: "note 1", Public: false}) + a1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 2, "add_note", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.RemoveNoteDataV1{NoteUUID: "note-1-uuid", BookName: "js"}) + a2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a2UUID, 1, "remove_note", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.RemoveNoteDataV1{NoteUUID: "note-2-uuid", BookName: "js"}) + a3UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a3UUID, 1, "remove_note", string(data), 1537829463) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm3.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var actionCount int + database.MustScan(t, "counting actions", db.QueryRow("SELECT count(*) FROM actions"), &actionCount) + assert.Equal(t, actionCount, 3, "action count mismatch") + + var a1, a2, a3 actions.Action + var a1DataRaw, a2DataRaw, a3DataRaw string + database.MustScan(t, "getting action 1", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a1UUID), + &a1.Schema, &a1.Type, &a1DataRaw, &a1.Timestamp) + database.MustScan(t, "getting action 2", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a2UUID), + &a2.Schema, &a2.Type, &a2DataRaw, &a2.Timestamp) + database.MustScan(t, "getting action 3", db.QueryRow("SELECT schema, type, data, timestamp FROM actions WHERE uuid = ?", a3UUID), + &a3.Schema, &a3.Type, &a3DataRaw, &a3.Timestamp) + + var a1Data actions.AddNoteDataV2 + var a2Data, a3Data actions.RemoveNoteDataV2 + testutils.MustUnmarshalJSON(t, []byte(a1DataRaw), &a1Data) + testutils.MustUnmarshalJSON(t, []byte(a2DataRaw), &a2Data) + testutils.MustUnmarshalJSON(t, []byte(a3DataRaw), &a3Data) + + assert.Equal(t, a1.Schema, 2, "a1 schema mismatch") + assert.Equal(t, a1.Type, "add_note", "a1 type mismatch") + assert.Equal(t, a1.Timestamp, int64(1537829463), "a1 timestamp mismatch") + assert.Equal(t, a1Data.NoteUUID, "note-1-uuid", "a1 data note_uuid mismatch") + assert.Equal(t, a1Data.BookName, "js", "a1 data book_name mismatch") + assert.Equal(t, a1Data.Content, "note 1", "a1 data content mismatch") + assert.Equal(t, a1Data.Public, false, "a1 data public mismatch") + + assert.Equal(t, a2.Schema, 2, "a2 schema mismatch") + assert.Equal(t, a2.Type, "remove_note", "a2 type mismatch") + assert.Equal(t, a2.Timestamp, int64(1537829463), "a2 timestamp mismatch") + assert.Equal(t, a2Data.NoteUUID, "note-1-uuid", "a2 data note_uuid mismatch") + + assert.Equal(t, a3.Schema, 2, "a3 schema mismatch") + assert.Equal(t, a3.Type, "remove_note", "a3 type mismatch") + assert.Equal(t, a3.Timestamp, int64(1537829463), "a3 timestamp mismatch") + assert.Equal(t, a3Data.NoteUUID, "note-2-uuid", "a3 data note_uuid mismatch") +} + +func TestLocalMigration4(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n1UUID, b1UUID, "n1 content", time.Now().UnixNano()) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm4.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var n1Dirty, b1Dirty bool + var n1Deleted, b1Deleted bool + var n1USN, b1USN int + database.MustScan(t, "scanning the newly added dirty flag of n1", db.QueryRow("SELECT dirty, deleted, usn FROM notes WHERE uuid = ?", n1UUID), &n1Dirty, &n1Deleted, &n1USN) + database.MustScan(t, "scanning the newly added dirty flag of b1", db.QueryRow("SELECT dirty, deleted, usn FROM books WHERE uuid = ?", b1UUID), &b1Dirty, &b1Deleted, &b1USN) + + assert.Equal(t, n1Dirty, false, "n1 dirty flag should be false by default") + assert.Equal(t, b1Dirty, false, "b1 dirty flag should be false by default") + + assert.Equal(t, n1Deleted, false, "n1 deleted flag should be false by default") + assert.Equal(t, b1Deleted, false, "b1 deleted flag should be false by default") + + assert.Equal(t, n1USN, 0, "n1 usn flag should be 0 by default") + assert.Equal(t, b1USN, 0, "b1 usn flag should be 0 by default") +} + +func TestLocalMigration5(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "css") + b2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting js book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "js") + + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n1UUID, b1UUID, "n1 content", time.Now().UnixNano()) + n2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n2UUID, b1UUID, "n2 content", time.Now().UnixNano()) + n3UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting css note", db, "INSERT INTO notes (uuid, book_uuid, content, added_on) VALUES (?, ?, ?, ?)", n3UUID, b1UUID, "n3 content", time.Now().UnixNano()) + + data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) + database.MustExec(t, "inserting a1", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a1-uuid", 1, "add_book", string(data), 1537829463) + + data = testutils.MustMarshalJSON(t, actions.AddNoteDataV2{NoteUUID: n1UUID, BookName: "css", Content: "n1 content", Public: false}) + database.MustExec(t, "inserting a2", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a2-uuid", 1, "add_note", string(data), 1537829463) + + updatedContent := "updated content" + data = testutils.MustMarshalJSON(t, actions.EditNoteDataV3{NoteUUID: n2UUID, BookName: (*string)(nil), Content: &updatedContent, Public: (*bool)(nil)}) + database.MustExec(t, "inserting a3", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", "a3-uuid", 1, "edit_note", string(data), 1537829463) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm5.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var b1Dirty, b2Dirty, n1Dirty, n2Dirty, n3Dirty bool + database.MustScan(t, "scanning the newly added dirty flag of b1", db.QueryRow("SELECT dirty FROM books WHERE uuid = ?", b1UUID), &b1Dirty) + database.MustScan(t, "scanning the newly added dirty flag of b2", db.QueryRow("SELECT dirty FROM books WHERE uuid = ?", b2UUID), &b2Dirty) + database.MustScan(t, "scanning the newly added dirty flag of n1", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n1UUID), &n1Dirty) + database.MustScan(t, "scanning the newly added dirty flag of n2", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n2UUID), &n2Dirty) + database.MustScan(t, "scanning the newly added dirty flag of n3", db.QueryRow("SELECT dirty FROM notes WHERE uuid = ?", n3UUID), &n3Dirty) + + assert.Equal(t, b1Dirty, false, "b1 dirty flag should be false by default") + assert.Equal(t, b2Dirty, true, "b2 dirty flag should be false by default") + assert.Equal(t, n1Dirty, true, "n1 dirty flag should be false by default") + assert.Equal(t, n2Dirty, true, "n2 dirty flag should be false by default") + assert.Equal(t, n3Dirty, false, "n3 dirty flag should be false by default") +} + +func TestLocalMigration6(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-5-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + data := testutils.MustMarshalJSON(t, actions.AddBookDataV1{BookName: "js"}) + a1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting action", db, + "INSERT INTO actions (uuid, schema, type, data, timestamp) VALUES (?, ?, ?, ?, ?)", a1UUID, 1, "add_book", string(data), 1537829463) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm5.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var count int + err = db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name = ?;", "actions").Scan(&count) + assert.Equal(t, count, 0, "actions table should have been deleted") +} + +func TestLocalMigration7_trash(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting trash book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "trash") + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm7.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var b1Label string + var b1Dirty bool + database.MustScan(t, "scanning b1 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) + assert.Equal(t, b1Label, "trash (2)", "b1 label was not migrated") + assert.Equal(t, b1Dirty, true, "b1 was not marked dirty") +} + +func TestLocalMigration7_conflicts(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts") + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm7.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var b1Label string + var b1Dirty bool + database.MustScan(t, "scanning b1 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) + assert.Equal(t, b1Label, "conflicts (2)", "b1 label was not migrated") + assert.Equal(t, b1Dirty, true, "b1 was not marked dirty") +} + +func TestLocalMigration7_conflicts_dup(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-7-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "conflicts") + b2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "conflicts (2)") + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm7.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var b1Label, b2Label string + var b1Dirty, b2Dirty bool + database.MustScan(t, "scanning b1 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) + database.MustScan(t, "scanning b2 label", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b2UUID), &b2Label, &b2Dirty) + assert.Equal(t, b1Label, "conflicts (3)", "b1 label was not migrated") + assert.Equal(t, b2Label, "conflicts (2)", "b1 label was not migrated") + assert.Equal(t, b1Dirty, true, "b1 was not marked dirty") + assert.Equal(t, b2Dirty, false, "b2 should not have been marked dirty") +} + +func TestLocalMigration8(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-8-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") + + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting n1", db, `INSERT INTO notes + (id, uuid, book_uuid, content, added_on, edited_on, public, dirty, usn, deleted) VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 1, n1UUID, b1UUID, "n1 Body", 1, 2, true, true, 20, false) + n2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting n2", db, `INSERT INTO notes + (id, uuid, book_uuid, content, added_on, edited_on, public, dirty, usn, deleted) VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 2, n2UUID, b1UUID, "", 3, 4, false, true, 21, true) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm8.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var n1BookUUID, n1Body string + var n1AddedOn, n1EditedOn int64 + var n1USN int + var n1Public, n1Dirty, n1Deleted bool + database.MustScan(t, "scanning n1", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n1UUID), &n1BookUUID, &n1Body, &n1AddedOn, &n1EditedOn, &n1USN, &n1Public, &n1Dirty, &n1Deleted) + + var n2BookUUID, n2Body string + var n2AddedOn, n2EditedOn int64 + var n2USN int + var n2Public, n2Dirty, n2Deleted bool + database.MustScan(t, "scanning n2", db.QueryRow("SELECT book_uuid, body, added_on, edited_on, usn, public, dirty, deleted FROM notes WHERE uuid = ?", n2UUID), &n2BookUUID, &n2Body, &n2AddedOn, &n2EditedOn, &n2USN, &n2Public, &n2Dirty, &n2Deleted) + + assert.Equal(t, n1BookUUID, b1UUID, "n1 BookUUID mismatch") + assert.Equal(t, n1Body, "n1 Body", "n1 Body mismatch") + assert.Equal(t, n1AddedOn, int64(1), "n1 AddedOn mismatch") + assert.Equal(t, n1EditedOn, int64(2), "n1 EditedOn mismatch") + assert.Equal(t, n1USN, 20, "n1 USN mismatch") + assert.Equal(t, n1Public, true, "n1 Public mismatch") + assert.Equal(t, n1Dirty, true, "n1 Dirty mismatch") + assert.Equal(t, n1Deleted, false, "n1 Deleted mismatch") + + assert.Equal(t, n2BookUUID, b1UUID, "n2 BookUUID mismatch") + assert.Equal(t, n2Body, "", "n2 Body mismatch") + assert.Equal(t, n2AddedOn, int64(3), "n2 AddedOn mismatch") + assert.Equal(t, n2EditedOn, int64(4), "n2 EditedOn mismatch") + assert.Equal(t, n2USN, 21, "n2 USN mismatch") + assert.Equal(t, n2Public, false, "n2 Public mismatch") + assert.Equal(t, n2Dirty, true, "n2 Dirty mismatch") + assert.Equal(t, n2Deleted, true, "n2 Deleted mismatch") +} + +func TestLocalMigration9(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-9-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") + + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting n1", db, `INSERT INTO notes + (uuid, book_uuid, body, added_on, edited_on, public, dirty, usn, deleted) VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?)`, n1UUID, b1UUID, "n1 Body", 1, 2, true, true, 20, false) + n2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting n2", db, `INSERT INTO notes + (uuid, book_uuid, body, added_on, edited_on, public, dirty, usn, deleted) VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?)`, n2UUID, b1UUID, "n2 Body", 3, 4, false, true, 21, false) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm9.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + + // assert that note_fts was populated with correct values + var noteFtsCount int + database.MustScan(t, "counting note_fts", db.QueryRow("SELECT count(*) FROM note_fts;"), ¬eFtsCount) + assert.Equal(t, noteFtsCount, 2, "noteFtsCount mismatch") + + var resCount int + database.MustScan(t, "counting result", db.QueryRow("SELECT count(*) FROM note_fts WHERE note_fts MATCH ?", "n1"), &resCount) + assert.Equal(t, resCount, 1, "noteFtsCount mismatch") +} + +func TestLocalMigration10(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-10-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book ", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "123") + b2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 2", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "123 javascript") + b3UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 3", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b3UUID, "foo") + b4UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 4", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b4UUID, "+123") + b5UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 5", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b5UUID, "0123") + b6UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 6", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b6UUID, "javascript 123") + b7UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 7", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b7UUID, "123 (1)") + b8UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 8", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b8UUID, "5") + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm10.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + + // assert that note_fts was populated with correct values + var b1Label, b2Label, b3Label, b4Label, b5Label, b6Label, b7Label, b8Label string + var b1Dirty, b2Dirty, b3Dirty, b4Dirty, b5Dirty, b6Dirty, b7Dirty, b8Dirty bool + + database.MustScan(t, "getting b1", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) + database.MustScan(t, "getting b2", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b2UUID), &b2Label, &b2Dirty) + database.MustScan(t, "getting b3", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b3UUID), &b3Label, &b3Dirty) + database.MustScan(t, "getting b4", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b4UUID), &b4Label, &b4Dirty) + database.MustScan(t, "getting b5", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b5UUID), &b5Label, &b5Dirty) + database.MustScan(t, "getting b6", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b6UUID), &b6Label, &b6Dirty) + database.MustScan(t, "getting b7", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b7UUID), &b7Label, &b7Dirty) + database.MustScan(t, "getting b8", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b8UUID), &b8Label, &b8Dirty) + + assert.Equal(t, b1Label, "123 (2)", "b1Label mismatch") + assert.Equal(t, b1Dirty, true, "b1Dirty mismatch") + assert.Equal(t, b2Label, "123 javascript", "b2Label mismatch") + assert.Equal(t, b2Dirty, false, "b2Dirty mismatch") + assert.Equal(t, b3Label, "foo", "b3Label mismatch") + assert.Equal(t, b3Dirty, false, "b3Dirty mismatch") + assert.Equal(t, b4Label, "+123", "b4Label mismatch") + assert.Equal(t, b4Dirty, false, "b4Dirty mismatch") + assert.Equal(t, b5Label, "0123 (1)", "b5Label mismatch") + assert.Equal(t, b5Dirty, true, "b5Dirty mismatch") + assert.Equal(t, b6Label, "javascript 123", "b6Label mismatch") + assert.Equal(t, b6Dirty, false, "b6Dirty mismatch") + assert.Equal(t, b7Label, "123 (1)", "b7Label mismatch") + assert.Equal(t, b7Dirty, false, "b7Dirty mismatch") + assert.Equal(t, b8Label, "5 (1)", "b8Label mismatch") + assert.Equal(t, b8Dirty, true, "b8Dirty mismatch") +} + +func TestLocalMigration11(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-11-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "foo") + b2UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 2", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "bar baz") + b3UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 3", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b3UUID, "quz qux") + b4UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 4", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b4UUID, "quz_qux") + b5UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 5", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b5UUID, "foo bar baz quz 123") + b6UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 6", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b6UUID, "foo_bar baz") + b7UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 7", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b7UUID, "cool ideas") + b8UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 8", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b8UUID, "cool_ideas") + b9UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book 9", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b9UUID, "cool_ideas_2") + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm11.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test + var bookCount int + database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount) + assert.Equal(t, bookCount, 9, "bookCount mismatch") + + // assert that note_fts was populated with correct values + var b1Label, b2Label, b3Label, b4Label, b5Label, b6Label, b7Label, b8Label, b9Label string + var b1Dirty, b2Dirty, b3Dirty, b4Dirty, b5Dirty, b6Dirty, b7Dirty, b8Dirty, b9Dirty bool + + database.MustScan(t, "getting b1", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b1UUID), &b1Label, &b1Dirty) + database.MustScan(t, "getting b2", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b2UUID), &b2Label, &b2Dirty) + database.MustScan(t, "getting b3", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b3UUID), &b3Label, &b3Dirty) + database.MustScan(t, "getting b4", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b4UUID), &b4Label, &b4Dirty) + database.MustScan(t, "getting b5", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b5UUID), &b5Label, &b5Dirty) + database.MustScan(t, "getting b6", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b6UUID), &b6Label, &b6Dirty) + database.MustScan(t, "getting b7", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b7UUID), &b7Label, &b7Dirty) + database.MustScan(t, "getting b8", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b8UUID), &b8Label, &b8Dirty) + database.MustScan(t, "getting b9", db.QueryRow("SELECT label, dirty FROM books WHERE uuid = ?", b9UUID), &b9Label, &b9Dirty) + + assert.Equal(t, b1Label, "foo", "b1Label mismatch") + assert.Equal(t, b1Dirty, false, "b1Dirty mismatch") + assert.Equal(t, b2Label, "bar_baz", "b2Label mismatch") + assert.Equal(t, b2Dirty, true, "b2Dirty mismatch") + assert.Equal(t, b3Label, "quz_qux_2", "b3Label mismatch") + assert.Equal(t, b3Dirty, true, "b3Dirty mismatch") + assert.Equal(t, b4Label, "quz_qux", "b4Label mismatch") + assert.Equal(t, b4Dirty, false, "b4Dirty mismatch") + assert.Equal(t, b5Label, "foo_bar_baz_quz_123", "b5Label mismatch") + assert.Equal(t, b5Dirty, true, "b5Dirty mismatch") + assert.Equal(t, b6Label, "foo_bar_baz", "b6Label mismatch") + assert.Equal(t, b6Dirty, true, "b6Dirty mismatch") + assert.Equal(t, b7Label, "cool_ideas_3", "b7Label mismatch") + assert.Equal(t, b7Dirty, true, "b7Dirty mismatch") + assert.Equal(t, b8Label, "cool_ideas", "b8Label mismatch") + assert.Equal(t, b8Dirty, false, "b8Dirty mismatch") + assert.Equal(t, b9Label, "cool_ideas_2", "b9Label mismatch") + assert.Equal(t, b9Dirty, false, "b9Dirty mismatch") +} + +func TestLocalMigration12(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + data := []byte("editor: vim") + path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName) + if err := os.WriteFile(path, data, 0644); err != nil { + t.Fatal(errors.Wrap(err, "Failed to write schema file")) + } + + // execute + err := lm12.run(ctx, nil) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(errors.Wrap(err, "reading config")) + } + + type config struct { + APIEndpoint string `yaml:"apiEndpoint"` + } + + var cf config + err = yaml.Unmarshal(b, &cf) + if err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling config")) + } + + assert.NotEqual(t, cf.APIEndpoint, "", "apiEndpoint was not populated") +} + +func TestLocalMigration13(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + data := []byte("editor: vim\napiEndpoint: https://test.com/api") + + path := fmt.Sprintf("%s/%s/dnoterc", ctx.Paths.Config, consts.DnoteDirName) + if err := os.WriteFile(path, data, 0644); err != nil { + t.Fatal(errors.Wrap(err, "Failed to write schema file")) + } + + // execute + err := lm13.run(ctx, nil) + if err != nil { + t.Fatal(errors.Wrap(err, "failed to run")) + } + + // test + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(errors.Wrap(err, "reading config")) + } + + type config struct { + Editor string `yaml:"editor"` + ApiEndpoint string `yaml:"apiEndpoint"` + EnableUpgradeCheck bool `yaml:"enableUpgradeCheck"` + } + + var cf config + err = yaml.Unmarshal(b, &cf) + if err != nil { + t.Fatal(errors.Wrap(err, "unmarshalling config")) + } + + assert.Equal(t, cf.Editor, "vim", "editor mismatch") + assert.Equal(t, cf.ApiEndpoint, "https://test.com/api", "apiEndpoint mismatch") + assert.Equal(t, cf.EnableUpgradeCheck, true, "enableUpgradeCheck mismatch") +} + +func TestLocalMigration14(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/local-14-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + + b1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1") + + n1UUID := testutils.MustGenerateUUID(t) + database.MustExec(t, "inserting note", db, `INSERT INTO notes + (uuid, book_uuid, body, added_on, edited_on, public, dirty, usn, deleted) VALUES + (?, ?, ?, ?, ?, ?, ?, ?, ?)`, n1UUID, b1UUID, "test note", 1, 2, true, false, 0, false) + + // Execute + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = lm14.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // Test - verify public column was dropped by checking column names + rows, err := db.Query("SELECT name FROM pragma_table_info('notes')") + if err != nil { + t.Fatal(errors.Wrap(err, "getting table info")) + } + defer rows.Close() + + for rows.Next() { + var name string + err := rows.Scan(&name) + if err != nil { + t.Fatal(errors.Wrap(err, "scanning column name")) + } + + if name == "public" { + t.Fatal("public column still exists after migration") + } + } +} + +func TestRemoteMigration1(t *testing.T) { + // set up + db := database.InitTestMemoryDBRaw(t, "./fixtures/remote-1-pre-schema.sql") + ctx := context.InitTestCtxWithDB(t, db) + testutils.Login(t, &ctx) + + JSBookUUID := "existing-js-book-uuid" + CSSBookUUID := "existing-css-book-uuid" + linuxBookUUID := "existing-linux-book-uuid" + newJSBookUUID := "new-js-book-uuid" + newCSSBookUUID := "new-css-book-uuid" + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.String() == "/v3/books" { + res := []struct { + UUID string `json:"uuid"` + Label string `json:"label"` + }{ + { + UUID: newJSBookUUID, + Label: "js", + }, + { + UUID: newCSSBookUUID, + Label: "css", + }, + // book that only exists on the server. client must ignore. + { + UUID: "golang-book-uuid", + Label: "golang", + }, + } + + w.Header().Set("Content-Type", "application/json") + if err := json.NewEncoder(w).Encode(res); err != nil { + t.Fatal(errors.Wrap(err, "encoding response")) + } + } + })) + defer server.Close() + + ctx.APIEndpoint = server.URL + + database.MustExec(t, "inserting js book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", JSBookUUID, "js") + database.MustExec(t, "inserting css book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", CSSBookUUID, "css") + database.MustExec(t, "inserting linux book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", linuxBookUUID, "linux") + database.MustExec(t, "inserting sessionKey", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, "someSessionKey") + database.MustExec(t, "inserting sessionKeyExpiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, time.Now().Add(24*time.Hour).Unix()) + + tx, err := db.Begin() + if err != nil { + t.Fatal(errors.Wrap(err, "beginning a transaction")) + } + + err = rm1.run(ctx, tx) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "failed to run")) + } + + tx.Commit() + + // test + var postJSBookUUID, postCSSBookUUID, postLinuxBookUUID string + database.MustScan(t, "getting js book uuid", db.QueryRow("SELECT uuid FROM books WHERE label = ?", "js"), &postJSBookUUID) + database.MustScan(t, "getting css book uuid", db.QueryRow("SELECT uuid FROM books WHERE label = ?", "css"), &postCSSBookUUID) + database.MustScan(t, "getting linux book uuid", db.QueryRow("SELECT uuid FROM books WHERE label = ?", "linux"), &postLinuxBookUUID) + + assert.Equal(t, postJSBookUUID, newJSBookUUID, "js book uuid was not updated correctly") + assert.Equal(t, postCSSBookUUID, newCSSBookUUID, "css book uuid was not updated correctly") + assert.Equal(t, postLinuxBookUUID, linuxBookUUID, "linux book uuid changed") +} diff --git a/cli/migrate/migrations.go b/pkg/cli/migrate/migrations.go similarity index 67% rename from cli/migrate/migrations.go rename to pkg/cli/migrate/migrations.go index e624e2a4..197a2b03 100644 --- a/cli/migrate/migrations.go +++ b/pkg/cli/migrate/migrations.go @@ -1,19 +1,16 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote CLI. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote CLI is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote CLI is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Dnote CLI. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package migrate @@ -22,22 +19,26 @@ import ( "database/sql" "encoding/json" "fmt" + "regexp" + "strings" "github.com/dnote/actions" - "github.com/dnote/dnote/cli/client" - "github.com/dnote/dnote/cli/infra" - "github.com/dnote/dnote/cli/log" + "github.com/dnote/dnote/pkg/cli/client" + "github.com/dnote/dnote/pkg/cli/config" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/log" "github.com/pkg/errors" ) type migration struct { name string - run func(ctx infra.DnoteCtx, tx *infra.DB) error + run func(ctx context.DnoteCtx, tx *database.DB) error } var lm1 = migration{ name: "upgrade-edit-note-from-v1-to-v3", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { rows, err := tx.Query("SELECT uuid, data FROM actions WHERE type = ? AND schema = ?", "edit_note", 1) if err != nil { return errors.Wrap(err, "querying rows") @@ -85,7 +86,7 @@ var lm1 = migration{ var lm2 = migration{ name: "upgrade-edit-note-from-v2-to-v3", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { rows, err := tx.Query("SELECT uuid, data FROM actions WHERE type = ? AND schema = ?", "edit_note", 2) if err != nil { return errors.Wrap(err, "querying rows") @@ -130,7 +131,7 @@ var lm2 = migration{ var lm3 = migration{ name: "upgrade-remove-note-from-v1-to-v2", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { rows, err := tx.Query("SELECT uuid, data FROM actions WHERE type = ? AND schema = ?", "remove_note", 1) if err != nil { return errors.Wrap(err, "querying rows") @@ -172,7 +173,7 @@ var lm3 = migration{ var lm4 = migration{ name: "add-dirty-usn-deleted-to-notes-and-books", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { _, err := tx.Exec("ALTER TABLE books ADD COLUMN dirty bool DEFAULT false") if err != nil { return errors.Wrap(err, "adding dirty column to books") @@ -209,7 +210,7 @@ var lm4 = migration{ var lm5 = migration{ name: "mark-action-targets-dirty", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { rows, err := tx.Query("SELECT uuid, data, type FROM actions") if err != nil { return errors.Wrap(err, "querying rows") @@ -271,7 +272,7 @@ var lm5 = migration{ var lm6 = migration{ name: "drop-actions", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { _, err := tx.Exec("DROP TABLE actions;") if err != nil { return errors.Wrap(err, "dropping the actions table") @@ -283,7 +284,7 @@ var lm6 = migration{ var lm7 = migration{ name: "resolve-conflicts-with-reserved-book-names", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { migrateBook := func(name string) error { var uuid string @@ -330,7 +331,7 @@ var lm7 = migration{ var lm8 = migration{ name: "drop-note-id-and-rename-content-to-body", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { _, err := tx.Exec(`CREATE TABLE notes_tmp ( uuid text NOT NULL, @@ -369,7 +370,7 @@ var lm8 = migration{ var lm9 = migration{ name: "create-fts-index", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { _, err := tx.Exec(`CREATE VIRTUAL TABLE IF NOT EXISTS note_fts USING fts5(content=notes, body, tokenize="porter unicode61 categories 'L* N* Co Ps Pe'");`) if err != nil { return errors.Wrap(err, "creating note_fts") @@ -403,9 +404,186 @@ var lm9 = migration{ }, } +var lm10 = migration{ + name: "rename-number-only-book", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + migrateBook := func(label string) error { + var uuid string + + err := tx.QueryRow("SELECT uuid FROM books WHERE label = ?", label).Scan(&uuid) + if err != nil { + return errors.Wrap(err, "finding uuid") + } + + for i := 1; ; i++ { + candidate := fmt.Sprintf("%s (%d)", label, i) + + var count int + err := tx.QueryRow("SELECT count(*) FROM books WHERE label = ?", candidate).Scan(&count) + if err != nil { + return errors.Wrap(err, "counting candidate") + } + + if count == 0 { + _, err := tx.Exec("UPDATE books SET label = ?, dirty = ? WHERE uuid = ?", candidate, true, uuid) + if err != nil { + return errors.Wrapf(err, "updating book '%s'", label) + } + + break + } + } + + return nil + } + + rows, err := tx.Query("SELECT label FROM books") + defer rows.Close() + if err != nil { + return errors.Wrap(err, "getting labels") + } + + var regexNumber = regexp.MustCompile(`^\d+$`) + + for rows.Next() { + var label string + err := rows.Scan(&label) + if err != nil { + return errors.Wrap(err, "scannign row") + } + + if regexNumber.MatchString(label) { + err = migrateBook(label) + if err != nil { + return errors.Wrapf(err, "migrating book %s", label) + } + } + } + + return nil + }, +} + +var lm11 = migration{ + name: "rename-book-labels-with-space", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + processLabel := func(label string) (string, error) { + sanitized := strings.Replace(label, " ", "_", -1) + + var cnt int + err := tx.QueryRow("SELECT count(*) FROM books WHERE label = ?", sanitized).Scan(&cnt) + if err != nil { + return "", errors.Wrap(err, "counting ret") + } + if cnt == 0 { + return sanitized, nil + } + + // if there is a collision, resolve by appending number + var ret string + for i := 2; ; i++ { + ret = fmt.Sprintf("%s_%d", sanitized, i) + + var count int + err := tx.QueryRow("SELECT count(*) FROM books WHERE label = ?", ret).Scan(&count) + if err != nil { + return "", errors.Wrap(err, "counting ret") + } + + if count == 0 { + break + } + } + + return ret, nil + } + + rows, err := tx.Query("SELECT uuid, label FROM books") + defer rows.Close() + if err != nil { + return errors.Wrap(err, "getting labels") + } + + for rows.Next() { + var uuid, label string + err := rows.Scan(&uuid, &label) + if err != nil { + return errors.Wrap(err, "scanning row") + } + + if strings.Contains(label, " ") { + processed, err := processLabel(label) + if err != nil { + return errors.Wrapf(err, "resolving book name for %s", label) + } + + _, err = tx.Exec("UPDATE books SET label = ?, dirty = ? WHERE uuid = ?", processed, true, uuid) + if err != nil { + return errors.Wrapf(err, "updating book '%s'", label) + } + } + } + + return nil + }, +} + +var lm12 = migration{ + name: "add apiEndpoint to the configuration file", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + cf, err := config.Read(ctx) + if err != nil { + return errors.Wrap(err, "reading config") + } + + // Only set if not already configured + if cf.APIEndpoint == "" { + cf.APIEndpoint = "https://api.getdnote.com" + } + + err = config.Write(ctx, cf) + if err != nil { + return errors.Wrap(err, "writing config") + } + + return nil + }, +} + +var lm13 = migration{ + name: "add enableUpgradeCheck to the configuration file", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + cf, err := config.Read(ctx) + if err != nil { + return errors.Wrap(err, "reading config") + } + + cf.EnableUpgradeCheck = true + + err = config.Write(ctx, cf) + if err != nil { + return errors.Wrap(err, "writing config") + } + + return nil + }, +} + +var lm14 = migration{ + name: "drop-public-from-notes", + run: func(ctx context.DnoteCtx, tx *database.DB) error { + _, err := tx.Exec(`ALTER TABLE notes DROP COLUMN public;`) + if err != nil { + return errors.Wrap(err, "dropping public column from notes") + } + + return nil + }, +} + var rm1 = migration{ name: "sync-book-uuids-from-server", - run: func(ctx infra.DnoteCtx, tx *infra.DB) error { + run: func(ctx context.DnoteCtx, tx *database.DB) error { sessionKey := ctx.SessionKey if sessionKey == "" { return errors.New("not logged in") diff --git a/pkg/cli/output/output.go b/pkg/cli/output/output.go new file mode 100644 index 00000000..d272ba88 --- /dev/null +++ b/pkg/cli/output/output.go @@ -0,0 +1,53 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package output provides functions to print informations on the terminal +// in a consistent manner +package output + +import ( + "fmt" + "io" + "time" + + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/log" +) + +// NoteInfo prints a note information +func NoteInfo(w io.Writer, info database.NoteInfo) { + log.Infof("book name: %s\n", info.BookLabel) + log.Infof("created at: %s\n", time.Unix(0, info.AddedOn).Format("Jan 2, 2006 3:04pm (MST)")) + if info.EditedOn != 0 { + log.Infof("updated at: %s\n", time.Unix(0, info.EditedOn).Format("Jan 2, 2006 3:04pm (MST)")) + } + log.Infof("note id: %d\n", info.RowID) + log.Infof("note uuid: %s\n", info.UUID) + + fmt.Fprintf(w, "\n------------------------content------------------------\n") + fmt.Fprintf(w, "%s", info.Content) + fmt.Fprintf(w, "\n-------------------------------------------------------\n") +} + +func NoteContent(w io.Writer, info database.NoteInfo) { + fmt.Fprintf(w, "%s", info.Content) +} + +// BookInfo prints a note information +func BookInfo(info database.BookInfo) { + log.Infof("book name: %s\n", info.Name) + log.Infof("book id: %d\n", info.RowID) + log.Infof("book uuid: %s\n", info.UUID) +} diff --git a/pkg/cli/testutils/main.go b/pkg/cli/testutils/main.go new file mode 100644 index 00000000..db3282d7 --- /dev/null +++ b/pkg/cli/testutils/main.go @@ -0,0 +1,310 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package testutils provides utilities used in tests +package testutils + +import ( + "bytes" + "encoding/json" + "io" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +// Prompts for user input +const ( + PromptRemoveNote = "remove this note?" + PromptDeleteBook = "delete book" + PromptEmptyServer = "The server is empty but you have local data" +) + +// Timeout for waiting for prompts in tests +const promptTimeout = 10 * time.Second + +// LoginDB sets up login credentials in the database for tests +func LoginDB(t *testing.T, db *database.DB) { + database.MustExec(t, "inserting sessionKey", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, "someSessionKey") + database.MustExec(t, "inserting sessionKeyExpiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, time.Now().Add(24*time.Hour).Unix()) +} + +// Login simulates a logged in user by inserting credentials in the local database +func Login(t *testing.T, ctx *context.DnoteCtx) { + LoginDB(t, ctx.DB) + + ctx.SessionKey = "someSessionKey" + ctx.SessionKeyExpiry = time.Now().Add(24 * time.Hour).Unix() +} + +// RemoveDir cleans up the test env represented by the given context +func RemoveDir(t *testing.T, dir string) { + if err := os.RemoveAll(dir); err != nil { + t.Fatal(errors.Wrap(err, "removing the directory")) + } +} + +// CopyFixture writes the content of the given fixture to the filename inside the dnote dir +func CopyFixture(t *testing.T, ctx context.DnoteCtx, fixturePath string, filename string) { + fp, err := filepath.Abs(fixturePath) + if err != nil { + t.Fatal(errors.Wrap(err, "getting the absolute path for fixture")) + } + + dp, err := filepath.Abs(filepath.Join(ctx.Paths.LegacyDnote, filename)) + if err != nil { + t.Fatal(errors.Wrap(err, "getting the absolute path dnote dir")) + } + + err = utils.CopyFile(fp, dp) + if err != nil { + t.Fatal(errors.Wrap(err, "copying the file")) + } +} + +// WriteFile writes a file with the given content and filename inside the dnote dir +func WriteFile(ctx context.DnoteCtx, content []byte, filename string) { + dp, err := filepath.Abs(filepath.Join(ctx.Paths.LegacyDnote, filename)) + if err != nil { + panic(err) + } + + if err := os.WriteFile(dp, content, 0644); err != nil { + panic(err) + } +} + +// ReadFile reads the content of the file with the given name in dnote dir +func ReadFile(ctx context.DnoteCtx, filename string) []byte { + path := filepath.Join(ctx.Paths.LegacyDnote, filename) + + b, err := os.ReadFile(path) + if err != nil { + panic(err) + } + + return b +} + +// ReadJSON reads JSON fixture to the struct at the destination address +func ReadJSON(path string, destination interface{}) { + var dat []byte + dat, err := os.ReadFile(path) + if err != nil { + panic(errors.Wrap(err, "Failed to load fixture payload")) + } + if err := json.Unmarshal(dat, destination); err != nil { + panic(errors.Wrap(err, "Failed to get event")) + } +} + +// NewDnoteCmd returns a new Dnote command and a pointer to stderr +func NewDnoteCmd(opts RunDnoteCmdOptions, binaryName string, arg ...string) (*exec.Cmd, *bytes.Buffer, *bytes.Buffer, error) { + var stderr, stdout bytes.Buffer + + binaryPath, err := filepath.Abs(binaryName) + if err != nil { + return &exec.Cmd{}, &stderr, &stdout, errors.Wrap(err, "getting the absolute path to the test binary") + } + + cmd := exec.Command(binaryPath, arg...) + cmd.Stderr = &stderr + cmd.Stdout = &stdout + + cmd.Env = opts.Env + + return cmd, &stderr, &stdout, nil +} + +// RunDnoteCmdOptions is an option for RunDnoteCmd +type RunDnoteCmdOptions struct { + Env []string +} + +// RunDnoteCmd runs a dnote command +func RunDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, binaryName string, arg ...string) string { + t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) + + cmd, stderr, stdout, err := NewDnoteCmd(opts, binaryName, arg...) + if err != nil { + t.Logf("\n%s", stdout) + t.Fatal(errors.Wrap(err, "getting command").Error()) + } + + cmd.Env = append(cmd.Env, "DNOTE_DEBUG=1") + + if err := cmd.Run(); err != nil { + t.Logf("\n%s", stdout) + t.Fatal(errors.Wrapf(err, "running command %s", stderr.String())) + } + + // Print stdout if and only if test fails later + t.Logf("\n%s", stdout) + + return stdout.String() +} + +// WaitDnoteCmd runs a dnote command and passes stdout to the callback. +func WaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string) (string, error) { + t.Logf("running: %s %s", binaryName, strings.Join(arg, " ")) + + binaryPath, err := filepath.Abs(binaryName) + if err != nil { + return "", errors.Wrap(err, "getting absolute path to test binary") + } + + cmd := exec.Command(binaryPath, arg...) + cmd.Env = opts.Env + + var stderr bytes.Buffer + cmd.Stderr = &stderr + + stdout, err := cmd.StdoutPipe() + if err != nil { + return "", errors.Wrap(err, "getting stdout pipe") + } + + stdin, err := cmd.StdinPipe() + if err != nil { + return "", errors.Wrap(err, "getting stdin") + } + defer stdin.Close() + + if err = cmd.Start(); err != nil { + return "", errors.Wrap(err, "starting command") + } + + var output bytes.Buffer + tee := io.TeeReader(stdout, &output) + + err = runFunc(tee, stdin) + if err != nil { + t.Logf("\n%s", output.String()) + return output.String(), errors.Wrap(err, "running callback") + } + + io.Copy(&output, stdout) + + if err := cmd.Wait(); err != nil { + t.Logf("\n%s", output.String()) + return output.String(), errors.Wrapf(err, "command failed: %s", stderr.String()) + } + + t.Logf("\n%s", output.String()) + return output.String(), nil +} + +func MustWaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Reader, io.WriteCloser) error, binaryName string, arg ...string) string { + output, err := WaitDnoteCmd(t, opts, runFunc, binaryName, arg...) + if err != nil { + t.Fatal(err) + } + + return output +} + +// MustWaitForPrompt waits for an expected prompt with a default timeout. +// Fails the test if the prompt is not found or an error occurs. +func MustWaitForPrompt(t *testing.T, stdout io.Reader, expectedPrompt string) { + if err := assert.WaitForPrompt(stdout, expectedPrompt, promptTimeout); err != nil { + t.Fatal(err) + } +} + +// ConfirmRemoveNote waits for prompt for removing a note and confirms. +func ConfirmRemoveNote(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptRemoveNote, "y\n", promptTimeout) +} + +// ConfirmRemoveBook waits for prompt for deleting a book confirms. +func ConfirmRemoveBook(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptDeleteBook, "y\n", promptTimeout) +} + +// UserConfirmEmptyServerSync waits for an empty server prompt and confirms. +func UserConfirmEmptyServerSync(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptEmptyServer, "y\n", promptTimeout) +} + +// UserCancelEmptyServerSync waits for an empty server prompt and cancels. +func UserCancelEmptyServerSync(stdout io.Reader, stdin io.WriteCloser) error { + return assert.RespondToPrompt(stdout, stdin, PromptEmptyServer, "n\n", promptTimeout) +} + +// UserContent simulates content from the user by writing to stdin. +// This is used for piped input where no prompt is shown. +func UserContent(stdout io.Reader, stdin io.WriteCloser) error { + longText := `Lorem ipsum dolor sit amet, consectetur adipiscing elit, + sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.` + + if _, err := io.WriteString(stdin, longText); err != nil { + return errors.Wrap(err, "creating note from stdin") + } + + // stdin needs to close so stdin reader knows to stop reading + // otherwise test case would wait until test timeout + stdin.Close() + + return nil +} + +// MustMarshalJSON marshalls the given interface into JSON. +// If there is any error, it fails the test. +func MustMarshalJSON(t *testing.T, v interface{}) []byte { + b, err := json.Marshal(v) + if err != nil { + t.Fatalf("%s: marshalling data: %s", t.Name(), err.Error()) + } + + return b +} + +// MustUnmarshalJSON marshalls the given interface into JSON. +// If there is any error, it fails the test. +func MustUnmarshalJSON(t *testing.T, data []byte, v interface{}) { + err := json.Unmarshal(data, v) + if err != nil { + t.Fatalf("%s: unmarshalling data: %s", t.Name(), err.Error()) + } +} + +// MustGenerateUUID generates the uuid. If error occurs, it fails the test. +func MustGenerateUUID(t *testing.T) string { + ret, err := utils.GenerateUUID() + if err != nil { + t.Fatal(errors.Wrap(err, "generating uuid").Error()) + } + + return ret +} + +func MustOpenDatabase(t *testing.T, dbPath string) *database.DB { + db, err := database.Open(dbPath) + if err != nil { + t.Fatal(errors.Wrap(err, "opening database")) + } + + return db +} diff --git a/pkg/cli/testutils/setup.go b/pkg/cli/testutils/setup.go new file mode 100644 index 00000000..d88afd2d --- /dev/null +++ b/pkg/cli/testutils/setup.go @@ -0,0 +1,77 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package testutils + +import ( + "testing" + + "github.com/dnote/dnote/pkg/cli/database" +) + +// Setup1 sets up a dnote env #1 +func Setup1(t *testing.T, db *database.DB) { + b1UUID := "js-book-uuid" + b2UUID := "linux-book-uuid" + + database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "js") + database.MustExec(t, "setting up book 2", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b2UUID, "linux") + + database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943) +} + +// Setup2 sets up a dnote env #2 +func Setup2(t *testing.T, db *database.DB) { + b1UUID := "js-book-uuid" + b2UUID := "linux-book-uuid" + + database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b1UUID, "js", 111) + database.MustExec(t, "setting up book 2", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b2UUID, "linux", 122) + + database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "n1 body", 1515199951, 11) + database.MustExec(t, "setting up note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "n2 body", 1515199943, 12) + database.MustExec(t, "setting up note 3", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "3e065d55-6d47-42f2-a6bf-f5844130b2d2", b2UUID, "n3 body", 1515199961, 13) +} + +// Setup3 sets up a dnote env #3 +func Setup3(t *testing.T, db *database.DB) { + b1UUID := "js-book-uuid" + + database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "js") + + database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on) VALUES (?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943) +} + +// Setup4 sets up a dnote env #4 +func Setup4(t *testing.T, db *database.DB) { + b1UUID := "js-book-uuid" + + database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b1UUID, "js", 111) + + database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?, ?)", 1, "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "Booleans have toString()", 1515199943, 11) + database.MustExec(t, "setting up note 2", db, "INSERT INTO notes (rowid, uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?, ?)", 2, "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "Date object implements mathematical comparisons", 1515199951, 12) +} + +// Setup5 sets up a dnote env #2 +func Setup5(t *testing.T, db *database.DB) { + b1UUID := "js-book-uuid" + b2UUID := "linux-book-uuid" + + database.MustExec(t, "setting up book 1", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b1UUID, "js", 111) + database.MustExec(t, "setting up book 2", db, "INSERT INTO books (uuid, label, usn) VALUES (?, ?, ?)", b2UUID, "linux", 122) + + database.MustExec(t, "setting up note 1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "f0d0fbb7-31ff-45ae-9f0f-4e429c0c797f", b1UUID, "n1 body", 1515199951, 11) + database.MustExec(t, "setting up note 2", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, usn) VALUES (?, ?, ?, ?, ?)", "43827b9a-c2b0-4c06-a290-97991c896653", b1UUID, "n2 body", 1515199943, 12) +} diff --git a/pkg/cli/ui/editor.go b/pkg/cli/ui/editor.go new file mode 100644 index 00000000..a6c165f4 --- /dev/null +++ b/pkg/cli/ui/editor.go @@ -0,0 +1,134 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package ui provides the user interface for the program +package ui + +import ( + "fmt" + "os" + "os/exec" + "strings" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +// GetTmpContentPath returns the path to the temporary file containing +// content being added or edited +func GetTmpContentPath(ctx context.DnoteCtx) (string, error) { + for i := 0; ; i++ { + filename := fmt.Sprintf("%s_%d.%s", consts.TmpContentFileBase, i, consts.TmpContentFileExt) + candidate := fmt.Sprintf("%s/%s", ctx.Paths.Cache, filename) + + ok, err := utils.FileExists(candidate) + if err != nil { + return "", errors.Wrapf(err, "checking if file exists at %s", candidate) + } + if !ok { + return candidate, nil + } + } +} + +// getEditorCommand returns the system's editor command with appropriate flags, +// if necessary, to make the command wait until editor is close to exit. +func getEditorCommand() string { + editor := os.Getenv("EDITOR") + + var ret string + + switch editor { + case "atom": + ret = "atom -w" + case "subl": + ret = "subl -n -w" + case "mate": + ret = "mate -w" + case "vim": + ret = "vim" + case "nano": + ret = "nano" + case "emacs": + ret = "emacs" + case "nvim": + ret = "nvim" + default: + ret = "vi" + } + + return ret +} + +func newEditorCmd(ctx context.DnoteCtx, fpath string) (*exec.Cmd, error) { + args := strings.Fields(ctx.Editor) + args = append(args, fpath) + + return exec.Command(args[0], args[1:]...), nil +} + +// GetEditorInput gets the user input by launching a text editor and waiting for +// it to exit +func GetEditorInput(ctx context.DnoteCtx, fpath string) (string, error) { + ok, err := utils.FileExists(fpath) + if err != nil { + return "", errors.Wrapf(err, "checking if the file exists at %s", fpath) + } + if !ok { + f, err := os.Create(fpath) + if err != nil { + return "", errors.Wrap(err, "creating a temporary content file") + } + err = f.Close() + if err != nil { + return "", errors.Wrap(err, "closing the temporary content file") + } + } + + cmd, err := newEditorCmd(ctx, fpath) + if err != nil { + return "", errors.Wrap(err, "creating an editor command") + } + + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + err = cmd.Start() + if err != nil { + return "", errors.Wrapf(err, "launching an editor") + } + + err = cmd.Wait() + if err != nil { + return "", errors.Wrap(err, "waiting for the editor") + } + + b, err := os.ReadFile(fpath) + if err != nil { + return "", errors.Wrap(err, "reading the temporary content file") + } + + err = os.Remove(fpath) + if err != nil { + return "", errors.Wrap(err, "removing the temporary content file") + } + + raw := string(b) + + return raw, nil +} diff --git a/pkg/cli/ui/editor_test.go b/pkg/cli/ui/editor_test.go new file mode 100644 index 00000000..54538c49 --- /dev/null +++ b/pkg/cli/ui/editor_test.go @@ -0,0 +1,84 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ui + +import ( + "fmt" + "os" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/pkg/errors" +) + +func TestGetTmpContentPath(t *testing.T) { + t.Run("no collision", func(t *testing.T) { + ctx := context.InitTestCtx(t) + + res, err := GetTmpContentPath(ctx) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + expected := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md") + assert.Equal(t, res, expected, "filename did not match") + }) + + t.Run("one existing session", func(t *testing.T) { + // set up + ctx := context.InitTestCtx(t) + + p := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md") + if _, err := os.Create(p); err != nil { + t.Fatal(errors.Wrap(err, "preparing the conflicting file")) + } + + // execute + res, err := GetTmpContentPath(ctx) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + // test + expected := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_1.md") + assert.Equal(t, res, expected, "filename did not match") + }) + + t.Run("two existing sessions", func(t *testing.T) { + // set up + ctx := context.InitTestCtx(t) + + p1 := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md") + if _, err := os.Create(p1); err != nil { + t.Fatal(errors.Wrap(err, "preparing the conflicting file")) + } + p2 := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_1.md") + if _, err := os.Create(p2); err != nil { + t.Fatal(errors.Wrap(err, "preparing the conflicting file")) + } + + // execute + res, err := GetTmpContentPath(ctx) + if err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + // test + expected := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_2.md") + assert.Equal(t, res, expected, "filename did not match") + }) +} diff --git a/pkg/cli/ui/terminal.go b/pkg/cli/ui/terminal.go new file mode 100644 index 00000000..6a255766 --- /dev/null +++ b/pkg/cli/ui/terminal.go @@ -0,0 +1,101 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ui + +import ( + "bufio" + "fmt" + "os" + "strings" + "syscall" + + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/prompt" + "github.com/pkg/errors" + "golang.org/x/crypto/ssh/terminal" +) + +func readInput() (string, error) { + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + return "", errors.Wrap(err, "reading stdin") + } + + return strings.Trim(input, "\r\n"), nil +} + +// PromptInput prompts the user input and saves the result to the destination +func PromptInput(message string, dest *string) error { + log.Askf(message, false) + + input, err := readInput() + if err != nil { + return errors.Wrap(err, "getting user input") + } + + *dest = input + + return nil +} + +// PromptPassword prompts the user input a password and saves the result to the destination. +// The input is masked, meaning it is not echoed on the terminal. +func PromptPassword(message string, dest *string) error { + log.Askf(message, true) + + password, err := terminal.ReadPassword(int(syscall.Stdin)) + if err != nil { + return errors.Wrap(err, "getting user input") + } + + fmt.Println("") + + *dest = string(password) + + return nil +} + +// Confirm prompts for user input to confirm a choice +func Confirm(question string, optimistic bool) (bool, error) { + message := prompt.FormatQuestion(question, optimistic) + + // Use log.Askf for colored prompt in CLI + log.Askf(message, false) + + confirmed, err := prompt.ReadYesNo(os.Stdin, optimistic) + if err != nil { + return false, errors.Wrap(err, "Failed to get user input") + } + + return confirmed, nil +} + +// Grab text from stdin content +func ReadStdInput() (string, error) { + var lines []string + + s := bufio.NewScanner(os.Stdin) + for s.Scan() { + lines = append(lines, s.Text()) + } + err := s.Err() + if err != nil { + return "", errors.Wrap(err, "reading pipe") + } + + return strings.Join(lines, "\n"), nil +} diff --git a/pkg/cli/upgrade/upgrade.go b/pkg/cli/upgrade/upgrade.go new file mode 100644 index 00000000..7c24a86d --- /dev/null +++ b/pkg/cli/upgrade/upgrade.go @@ -0,0 +1,145 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package upgrade + +import ( + stdCtx "context" + "fmt" + "strings" + "time" + + "github.com/dnote/dnote/pkg/cli/consts" + "github.com/dnote/dnote/pkg/cli/context" + "github.com/dnote/dnote/pkg/cli/log" + "github.com/dnote/dnote/pkg/cli/ui" + "github.com/google/go-github/github" + "github.com/pkg/errors" +) + +// upgradeInterval is 8 weeks +var upgradeInterval int64 = 86400 * 7 * 8 + +// shouldCheckUpdate checks if update should be checked +func shouldCheckUpdate(ctx context.DnoteCtx) (bool, error) { + db := ctx.DB + + var lastUpgrade int64 + err := db.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastUpgrade).Scan(&lastUpgrade) + if err != nil { + return false, errors.Wrap(err, "getting last_udpate") + } + + now := time.Now().Unix() + + return now-lastUpgrade > upgradeInterval, nil +} + +func touchLastUpgrade(ctx context.DnoteCtx) error { + db := ctx.DB + + now := time.Now().Unix() + _, err := db.Exec("UPDATE system SET value = ? WHERE key = ?", now, consts.SystemLastUpgrade) + if err != nil { + return errors.Wrap(err, "updating last_upgrade") + } + + return nil +} + +func fetchLatestStableTag(gh *github.Client, page int) (string, error) { + params := github.ListOptions{ + Page: page, + } + releases, resp, err := gh.Repositories.ListReleases(stdCtx.Background(), "dnote", "dnote", ¶ms) + if err != nil { + return "", errors.Wrapf(err, "fetching releases page %d", page) + } + + for _, release := range releases { + tag := release.GetTagName() + isStable := !release.GetPrerelease() + + if strings.HasPrefix(tag, "cli-") && isStable { + return tag, nil + } + } + + if page == resp.LastPage { + return "", errors.New("No CLI release was found") + } + + return fetchLatestStableTag(gh, page+1) +} + +func checkVersion(ctx context.DnoteCtx) error { + log.Infof("current version is %s\n", ctx.Version) + + // Fetch the latest version + gh := github.NewClient(nil) + latestTag, err := fetchLatestStableTag(gh, 1) + if err != nil { + return errors.Wrap(err, "fetching the latest stable release") + } + + // releases are tagged in a form of cli-v1.0.0 + latestVersion := latestTag[5:] + log.Infof("latest version is %s\n", latestVersion) + + if latestVersion == ctx.Version { + log.Success("you are up-to-date\n\n") + } else { + log.Infof("to upgrade, see https://github.com/dnote/dnote\n") + } + + return nil +} + +// Check triggers update if needed +func Check(ctx context.DnoteCtx) error { + // If upgrade check is not enabled, do not proceed further + if !ctx.EnableUpgradeCheck { + return nil + } + + shouldCheck, err := shouldCheckUpdate(ctx) + if err != nil { + return errors.Wrap(err, "checking if dnote should check update") + } + if !shouldCheck { + return nil + } + + err = touchLastUpgrade(ctx) + if err != nil { + return errors.Wrap(err, "updating the last upgrade timestamp") + } + + fmt.Printf("\n") + willCheck, err := ui.Confirm("check for upgrade?", true) + if err != nil { + return errors.Wrap(err, "getting user confirmation") + } + if !willCheck { + return nil + } + + err = checkVersion(ctx) + if err != nil { + return errors.Wrap(err, "checking version") + } + + return nil +} diff --git a/pkg/cli/upgrade/upgrade_test.go b/pkg/cli/upgrade/upgrade_test.go new file mode 100644 index 00000000..917e6754 --- /dev/null +++ b/pkg/cli/upgrade/upgrade_test.go @@ -0,0 +1,172 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package upgrade + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/google/go-github/github" + "github.com/pkg/errors" +) + +func setupGithubClient(t *testing.T) (*github.Client, *http.ServeMux) { + mux := http.NewServeMux() + server := httptest.NewServer(mux) + + url, err := url.Parse(server.URL + "/") + if err != nil { + t.Fatal(errors.Wrap(err, "parsing mock server url")) + } + + client := github.NewClient(nil) + client.BaseURL = url + client.UploadURL = url + + return client, mux +} + +func TestFetchLatestStableTag(t *testing.T) { + tagCLI0_1_0 := "cli-v0.1.0" + tagCLI0_1_1 := "cli-v0.1.1" + tagCLI0_1_2Beta := "cli-v0.1.2-beta" + tagCLI0_1_3 := "cli-v0.1.3" + tagServer0_1_0 := "server-v0.1.0" + + prereleaseTrue := true + + testCases := []struct { + releases []*github.RepositoryRelease + expected string + }{ + { + releases: []*github.RepositoryRelease{{TagName: &tagCLI0_1_0}}, + expected: tagCLI0_1_0, + }, + { + releases: []*github.RepositoryRelease{ + {TagName: &tagCLI0_1_1}, + {TagName: &tagServer0_1_0}, + {TagName: &tagCLI0_1_0}, + }, + expected: tagCLI0_1_1, + }, + { + releases: []*github.RepositoryRelease{ + {TagName: &tagServer0_1_0}, + {TagName: &tagCLI0_1_1}, + {TagName: &tagCLI0_1_0}, + }, + expected: tagCLI0_1_1, + }, + { + releases: []*github.RepositoryRelease{ + {TagName: &tagCLI0_1_2Beta, Prerelease: &prereleaseTrue}, + {TagName: &tagServer0_1_0}, + {TagName: &tagCLI0_1_1}, + {TagName: &tagCLI0_1_0}, + }, + expected: tagCLI0_1_1, + }, + { + releases: []*github.RepositoryRelease{ + {TagName: &tagCLI0_1_3}, + {TagName: &tagCLI0_1_2Beta, Prerelease: &prereleaseTrue}, + {TagName: &tagCLI0_1_1}, + {TagName: &tagCLI0_1_0}, + }, + expected: tagCLI0_1_3, + }, + } + + for idx, tc := range testCases { + t.Run(fmt.Sprintf("case %d", idx), func(t *testing.T) { + // setup + gh, mux := setupGithubClient(t) + mux.HandleFunc("/repos/dnote/dnote/releases", func(w http.ResponseWriter, r *http.Request) { + if err := json.NewEncoder(w).Encode(tc.releases); err != nil { + t.Fatal(errors.Wrap(err, "responding with mock releases")) + } + }) + + // execute + got, err := fetchLatestStableTag(gh, 0) + if err != nil { + t.Fatal(errors.Wrap(err, "performing")) + } + + // test + assert.Equal(t, got, tc.expected, "result mismatch") + }) + } +} + +func TestFetchLatestStableTag_paginated(t *testing.T) { + tagServer0_1_0 := "server-v0.1.0" + tagCLI0_1_2Beta := "cli-v0.1.2-beta" + tagCLI0_1_1 := "cli-v0.1.1" + prereleaseTrue := true + + // set up + gh, mux := setupGithubClient(t) + path := "/repos/dnote/dnote/releases" + mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { + page := r.FormValue("page") + + releasesPage1 := []*github.RepositoryRelease{ + {TagName: &tagServer0_1_0}, + } + releasesPage2 := []*github.RepositoryRelease{ + {TagName: &tagCLI0_1_2Beta, Prerelease: &prereleaseTrue}, + {TagName: &tagCLI0_1_1}, + } + + baseURL := gh.BaseURL.String() + + switch page { + case "", "1": + linkHeader := fmt.Sprintf("<%s%s?page=2>; rel=\"next\" <%s%s?page=2>; rel=\"last\"", baseURL, path, baseURL, path) + w.Header().Set("Link", linkHeader) + + if err := json.NewEncoder(w).Encode(releasesPage1); err != nil { + t.Fatal(errors.Wrap(err, "responding with mock releases")) + } + case "2": + linkHeader := fmt.Sprintf("<%s%s?page=1>; rel=\"prev\" <%s%s?page=1>; rel=\"first\"", baseURL, path, baseURL, path) + w.Header().Set("Link", linkHeader) + + if err := json.NewEncoder(w).Encode(releasesPage2); err != nil { + t.Fatal(errors.Wrap(err, "responding with mock releases")) + } + default: + t.Fatal("Should have stopped walking") + } + }) + + // execute + got, err := fetchLatestStableTag(gh, 0) + if err != nil { + t.Fatal(errors.Wrap(err, "performing")) + } + + // test + assert.Equal(t, got, tagCLI0_1_1, "result mismatch") +} diff --git a/pkg/cli/utils/diff/diff.go b/pkg/cli/utils/diff/diff.go new file mode 100644 index 00000000..ff7e3384 --- /dev/null +++ b/pkg/cli/utils/diff/diff.go @@ -0,0 +1,45 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package diff provides line-by-line diff feature by wrapping +// a package github.com/sergi/go-diff/diffmatchpatch +package diff + +import ( + "time" + + "github.com/sergi/go-diff/diffmatchpatch" +) + +const ( + // DiffEqual represents an equal diff + DiffEqual = diffmatchpatch.DiffEqual + // DiffInsert represents an insert diff + DiffInsert = diffmatchpatch.DiffInsert + // DiffDelete represents a delete diff + DiffDelete = diffmatchpatch.DiffDelete +) + +// Do computes line-by-line diff between two strings +func Do(s1, s2 string) (diffs []diffmatchpatch.Diff) { + dmp := diffmatchpatch.New() + dmp.DiffTimeout = time.Hour + + s1Chars, s2Chars, arr := dmp.DiffLinesToRunes(s1, s2) + diffs = dmp.DiffMainRunes(s1Chars, s2Chars, false) + diffs = dmp.DiffCharsToLines(diffs, arr) + + return diffs +} diff --git a/pkg/cli/utils/diff/diff_test.go b/pkg/cli/utils/diff/diff_test.go new file mode 100644 index 00000000..f777f26c --- /dev/null +++ b/pkg/cli/utils/diff/diff_test.go @@ -0,0 +1,146 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package diff + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/sergi/go-diff/diffmatchpatch" +) + +func TestDo(t *testing.T) { + testCases := []struct { + s1 string + s2 string + expected []diffmatchpatch.Diff + }{ + { + s1: "", + s2: "", + expected: []diffmatchpatch.Diff{}, + }, + { + s1: "", + s2: "foo", + expected: []diffmatchpatch.Diff{ + { + Type: diffmatchpatch.DiffInsert, + Text: "foo", + }, + }, + }, + { + s1: "foo", + s2: "", + expected: []diffmatchpatch.Diff{ + { + Type: DiffDelete, + Text: "foo", + }, + }, + }, + { + s1: "foo", + s2: "bar", + expected: []diffmatchpatch.Diff{ + { + Type: DiffDelete, + Text: "foo", + }, + { + Type: DiffInsert, + Text: "bar", + }, + }, + }, + { + s1: "foo\nbar\nbaz", + s2: "foo\nbar\nquz", + expected: []diffmatchpatch.Diff{ + { + Type: DiffEqual, + Text: "foo\nbar\n", + }, + { + Type: DiffDelete, + Text: "baz", + }, + { + Type: DiffInsert, + Text: "quz", + }, + }, + }, + { + s1: "fuz\nbar\nbaz\nquz", + s2: "foo\nbar\nbaz\nqux", + expected: []diffmatchpatch.Diff{ + { + Type: DiffDelete, + Text: "fuz\n", + }, + { + Type: DiffInsert, + Text: "foo\n", + }, + { + Type: DiffEqual, + Text: "bar\nbaz\n", + }, + { + Type: DiffDelete, + Text: "quz", + }, + { + Type: DiffInsert, + Text: "qux", + }, + }, + }, + { + s1: "foo bar\nhello dnote\nbaz quz", + s2: "foo bar\nhello foo\nbaz quz", + expected: []diffmatchpatch.Diff{ + { + Type: DiffEqual, + Text: "foo bar\n", + }, + { + Type: DiffDelete, + Text: "hello dnote\n", + }, + { + Type: DiffInsert, + Text: "hello foo\n", + }, + { + Type: DiffEqual, + Text: "baz quz", + }, + }, + }, + } + + for idx, tc := range testCases { + result := Do(tc.s1, tc.s2) + + t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { + assert.DeepEqual(t, result, tc.expected, "result mismatch") + }) + } +} diff --git a/pkg/cli/utils/files.go b/pkg/cli/utils/files.go new file mode 100644 index 00000000..60f57de6 --- /dev/null +++ b/pkg/cli/utils/files.go @@ -0,0 +1,156 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package utils + +import ( + "io" + "os" + "path/filepath" + + "github.com/pkg/errors" +) + +// ReadFileAbs reads the content of the file with the given file path by resolving +// it as an absolute path +func ReadFileAbs(relpath string) []byte { + fp, err := filepath.Abs(relpath) + if err != nil { + panic(err) + } + + b, err := os.ReadFile(fp) + if err != nil { + panic(err) + } + + return b +} + +// FileExists checks if the file exists at the given path +func FileExists(filepath string) (bool, error) { + _, err := os.Stat(filepath) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + + return false, errors.Wrap(err, "getting file info") +} + +// EnsureDir creates a directory if it doesn't exist. +// Returns nil if the directory already exists or was successfully created. +func EnsureDir(path string) error { + ok, err := FileExists(path) + if err != nil { + return errors.Wrapf(err, "checking if dir exists at %s", path) + } + if ok { + return nil + } + + if err := os.MkdirAll(path, 0755); err != nil { + return errors.Wrapf(err, "creating directory at %s", path) + } + + return nil +} + +// CopyDir copies a directory from src to dest, recursively copying nested +// directories +func CopyDir(src, dest string) error { + srcPath := filepath.Clean(src) + destPath := filepath.Clean(dest) + + fi, err := os.Stat(srcPath) + if err != nil { + return errors.Wrap(err, "getting the file info for the input") + } + if !fi.IsDir() { + return errors.New("source is not a directory") + } + + _, err = os.Stat(dest) + if err != nil && !os.IsNotExist(err) { + return errors.Wrap(err, "looking up the destination") + } + + err = os.MkdirAll(dest, fi.Mode()) + if err != nil { + return errors.Wrap(err, "creating destination") + } + + entries, err := os.ReadDir(src) + if err != nil { + return errors.Wrap(err, "reading the directory listing for the input") + } + + for _, entry := range entries { + srcEntryPath := filepath.Join(srcPath, entry.Name()) + destEntryPath := filepath.Join(destPath, entry.Name()) + + if entry.IsDir() { + if err = CopyDir(srcEntryPath, destEntryPath); err != nil { + return errors.Wrapf(err, "copying %s", entry.Name()) + } + } else { + if err = CopyFile(srcEntryPath, destEntryPath); err != nil { + return errors.Wrapf(err, "copying %s", entry.Name()) + } + } + } + + return nil +} + +// CopyFile copies a file from the src to dest +func CopyFile(src, dest string) error { + in, err := os.Open(src) + if err != nil { + return errors.Wrap(err, "opening the input file") + } + defer in.Close() + + out, err := os.Create(dest) + if err != nil { + return errors.Wrap(err, "creating the output file") + } + + if _, err = io.Copy(out, in); err != nil { + return errors.Wrap(err, "copying the file content") + } + + if err = out.Sync(); err != nil { + return errors.Wrap(err, "flushing the output file to disk") + } + + fi, err := os.Stat(src) + if err != nil { + return errors.Wrap(err, "getting the file info for the input file") + } + + if err = os.Chmod(dest, fi.Mode()); err != nil { + return errors.Wrap(err, "copying permission to the output file") + } + + // Close the output file + if err = out.Close(); err != nil { + return errors.Wrap(err, "closing the output file") + } + + return nil +} diff --git a/pkg/cli/utils/files_test.go b/pkg/cli/utils/files_test.go new file mode 100644 index 00000000..7f26e19c --- /dev/null +++ b/pkg/cli/utils/files_test.go @@ -0,0 +1,42 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package utils + +import ( + "os" + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestEnsureDir(t *testing.T) { + tmpDir := t.TempDir() + testPath := filepath.Join(tmpDir, "test", "nested", "dir") + + // Create directory + err := EnsureDir(testPath) + assert.Equal(t, err, nil, "EnsureDir should succeed") + + // Verify it exists + info, err := os.Stat(testPath) + assert.Equal(t, err, nil, "directory should exist") + assert.Equal(t, info.IsDir(), true, "should be a directory") + + // Call again on existing directory - should not error + err = EnsureDir(testPath) + assert.Equal(t, err, nil, "EnsureDir should succeed on existing directory") +} diff --git a/pkg/cli/utils/utils.go b/pkg/cli/utils/utils.go new file mode 100644 index 00000000..417963ce --- /dev/null +++ b/pkg/cli/utils/utils.go @@ -0,0 +1,45 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package utils + +import ( + "regexp" + + "github.com/google/uuid" + "github.com/pkg/errors" +) + +// GenerateUUID returns a uuid v4 in string +func GenerateUUID() (string, error) { + u, err := uuid.NewRandom() + if err != nil { + return "", errors.Wrap(err, "generating uuid") + } + + return u.String(), nil +} + +// regexNumber is a regex that matches a string that looks like an integer +var regexNumber = regexp.MustCompile(`^\d+$`) + +// IsNumber checks if the given string is in the form of a number +func IsNumber(s string) bool { + if s == "" { + return false + } + + return regexNumber.MatchString(s) +} diff --git a/pkg/cli/validate/book_test.go b/pkg/cli/validate/book_test.go new file mode 100644 index 00000000..97a384fc --- /dev/null +++ b/pkg/cli/validate/book_test.go @@ -0,0 +1,157 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package validate + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestValidateBookName(t *testing.T) { + testCases := []struct { + input string + expected error + }{ + { + input: "javascript", + expected: nil, + }, + { + input: "node.js", + expected: nil, + }, + { + input: "", + expected: ErrBookNameEmpty, + }, + { + input: "foo bar", + expected: ErrBookNameHasSpace, + }, + { + input: "123", + expected: ErrBookNameNumeric, + }, + { + input: "+123", + expected: nil, + }, + { + input: "-123", + expected: nil, + }, + { + input: "+javascript", + expected: nil, + }, + { + input: "0", + expected: ErrBookNameNumeric, + }, + { + input: "0333", + expected: ErrBookNameNumeric, + }, + { + input: " javascript", + expected: ErrBookNameHasSpace, + }, + { + input: "java script", + expected: ErrBookNameHasSpace, + }, + { + input: "javascript (1)", + expected: ErrBookNameHasSpace, + }, + { + input: "javascript ", + expected: ErrBookNameHasSpace, + }, + { + input: "javascript (1) (2) (3)", + expected: ErrBookNameHasSpace, + }, + + // multiline + { + input: "\n", + expected: ErrBookNameMultiline, + }, + { + input: "\n\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\nbar\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\nbar\nbaz", + expected: ErrBookNameMultiline, + }, + { + input: "\r\n", + expected: ErrBookNameMultiline, + }, + { + input: "\r\n\r\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\r\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\r\nbar\r\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\r\nbar\r\nbaz", + expected: ErrBookNameMultiline, + }, + { + input: "\n\r\n", + expected: ErrBookNameMultiline, + }, + { + input: "foo\nbar\r\n", + expected: ErrBookNameMultiline, + }, + + // reserved book names + { + input: "trash", + expected: ErrBookNameReserved, + }, + { + input: "conflicts", + expected: ErrBookNameReserved, + }, + } + + for _, tc := range testCases { + actual := BookName(tc.input) + + assert.Equal(t, actual, tc.expected, fmt.Sprintf("result does not match for the input '%s'", tc.input)) + } +} diff --git a/pkg/cli/validate/books.go b/pkg/cli/validate/books.go new file mode 100644 index 00000000..b0e4c0ed --- /dev/null +++ b/pkg/cli/validate/books.go @@ -0,0 +1,75 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package validate + +import ( + "strings" + + "github.com/dnote/dnote/pkg/cli/utils" + "github.com/pkg/errors" +) + +var reservedBookNames = []string{"trash", "conflicts"} + +// ErrBookNameReserved is an error incidating that the specified book name is reserved +var ErrBookNameReserved = errors.New("The book name is reserved") + +// ErrBookNameNumeric is an error for a book name that only contains numbers +var ErrBookNameNumeric = errors.New("The book name cannot contain only numbers") + +// ErrBookNameHasSpace is an error for a book name that has any space +var ErrBookNameHasSpace = errors.New("The book name cannot contain spaces") + +// ErrBookNameEmpty is an error for an empty book name +var ErrBookNameEmpty = errors.New("The book name is empty") + +// ErrBookNameMultiline is an error for a book name that has linebreaks +var ErrBookNameMultiline = errors.New("The book name contains multiple lines") + +func isReservedName(name string) bool { + for _, n := range reservedBookNames { + if name == n { + return true + } + } + + return false +} + +// BookName validates a book name +func BookName(name string) error { + if name == "" { + return ErrBookNameEmpty + } + + if isReservedName(name) { + return ErrBookNameReserved + } + + if utils.IsNumber(name) { + return ErrBookNameNumeric + } + + if strings.Contains(name, " ") { + return ErrBookNameHasSpace + } + + if strings.Contains(name, "\n") || strings.Contains(name, "\r\n") { + return ErrBookNameMultiline + } + + return nil +} diff --git a/server/api/clock/clock.go b/pkg/clock/clock.go similarity index 53% rename from server/api/clock/clock.go rename to pkg/clock/clock.go index a8dff8a6..061bb1a3 100644 --- a/server/api/clock/clock.go +++ b/pkg/clock/clock.go @@ -1,33 +1,28 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ // Package clock provides an abstract layer over the standard time package package clock import ( + "sync" "time" ) -//TODO: use mutex to avoid race - // Clock is an interface to the standard library time. -// It is used to implement a real or a mock clock. The latter is -// used in tests. +// It is used to implement a real or a mock clock. The latter is used in tests. type Clock interface { Now() time.Time } @@ -40,16 +35,21 @@ func (c *clock) Now() time.Time { // Mock is a mock instance of clock type Mock struct { + mu sync.RWMutex currentTime time.Time } // SetNow sets the current time for the mock clock func (c *Mock) SetNow(t time.Time) { + c.mu.Lock() + defer c.mu.Unlock() c.currentTime = t } // Now returns the current time func (c *Mock) Now() time.Time { + c.mu.RLock() + defer c.mu.RUnlock() return c.currentTime } diff --git a/pkg/dirs/dirs.go b/pkg/dirs/dirs.go new file mode 100644 index 00000000..3eb57a7f --- /dev/null +++ b/pkg/dirs/dirs.go @@ -0,0 +1,64 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package dirs provides base directory definitions for the system +package dirs + +import ( + "os" + "os/user" + + "github.com/pkg/errors" +) + +var ( + // Home is the home directory of the user + Home string + // ConfigHome is the full path to the directory in which user-specific + // configurations should be written. + ConfigHome string + // DataHome is the full path to the directory in which user-specific data + // files should be written. + DataHome string + // CacheHome is the full path to the directory in which user-specific + // non-essential cached data should be writte + CacheHome string +) + +func init() { + Reload() +} + +// Reload reloads the directory definitions +func Reload() { + initDirs() +} + +func getHomeDir() string { + usr, err := user.Current() + if err != nil { + panic(errors.Wrap(err, "getting home dir")) + } + + return usr.HomeDir +} + +func readPath(envName, defaultPath string) string { + if dir := os.Getenv(envName); dir != "" { + return dir + } + + return defaultPath +} diff --git a/pkg/dirs/dirs_test.go b/pkg/dirs/dirs_test.go new file mode 100644 index 00000000..0c1ecb1f --- /dev/null +++ b/pkg/dirs/dirs_test.go @@ -0,0 +1,39 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dirs + +import ( + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +type envTestCase struct { + envKey string + envVal string + got *string + expected string +} + +func testCustomDirs(t *testing.T, testCases []envTestCase) { + for _, tc := range testCases { + t.Setenv(tc.envKey, tc.envVal) + + Reload() + + assert.Equal(t, *tc.got, tc.expected, "result mismatch") + } +} diff --git a/pkg/dirs/dirs_unix.go b/pkg/dirs/dirs_unix.go new file mode 100644 index 00000000..6c7696c6 --- /dev/null +++ b/pkg/dirs/dirs_unix.go @@ -0,0 +1,49 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build linux || darwin || freebsd + + +package dirs + +import ( + "path/filepath" +) + +// The environment variable names for the XDG base directory specification +var ( + envConfigHome = "XDG_CONFIG_HOME" + envDataHome = "XDG_DATA_HOME" + envCacheHome = "XDG_CACHE_HOME" +) + +func initDirs() { + Home = getHomeDir() + ConfigHome = readPath(envConfigHome, getConfigHome(Home)) + DataHome = readPath(envDataHome, getDataHome(Home)) + CacheHome = readPath(envCacheHome, getCacheHome(Home)) +} + +func getConfigHome(homeDir string) string { + return filepath.Join(homeDir, ".config") +} + +func getDataHome(homeDir string) string { + return filepath.Join(homeDir, ".local/share") +} + +func getCacheHome(homeDir string) string { + return filepath.Join(homeDir, ".cache") +} diff --git a/pkg/dirs/dirs_unix_test.go b/pkg/dirs/dirs_unix_test.go new file mode 100644 index 00000000..9ea5805a --- /dev/null +++ b/pkg/dirs/dirs_unix_test.go @@ -0,0 +1,82 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build linux || darwin || freebsd + + +package dirs + +import ( + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestDirs(t *testing.T) { + home := Home + assert.NotEqual(t, home, "", "home is empty") + + configHome := filepath.Join(home, ".config") + dataHome := filepath.Join(home, ".local", "share") + cacheHome := filepath.Join(home, ".cache") + + testCases := []struct { + got string + expected string + }{ + { + got: ConfigHome, + expected: configHome, + }, + { + got: DataHome, + expected: dataHome, + }, + { + got: CacheHome, + expected: cacheHome, + }, + } + + for _, tc := range testCases { + assert.Equal(t, tc.got, tc.expected, "result mismatch") + } +} + +func TestCustomDirs(t *testing.T) { + testCases := []envTestCase{ + { + envKey: "XDG_CONFIG_HOME", + envVal: "~/custom/config", + got: &ConfigHome, + expected: "~/custom/config", + }, + { + envKey: "XDG_DATA_HOME", + envVal: "~/custom/data", + got: &DataHome, + expected: "~/custom/data", + }, + { + envKey: "XDG_CACHE_HOME", + envVal: "~/custom/cache", + got: &CacheHome, + expected: "~/custom/cache", + }, + } + + testCustomDirs(t, testCases) +} diff --git a/pkg/dirs/dirs_windows.go b/pkg/dirs/dirs_windows.go new file mode 100644 index 00000000..7159ae8d --- /dev/null +++ b/pkg/dirs/dirs_windows.go @@ -0,0 +1,30 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build windows + + +package dirs + +import ( + "path/filepath" +) + +func initDirs() { + Home = getHomeDir() + ConfigHome = filepath.Join(Home, ".dnote") + DataHome = filepath.Join(Home, ".dnote") + CacheHome = filepath.Join(Home, ".dnote") +} diff --git a/pkg/dirs/dirs_windows_test.go b/pkg/dirs/dirs_windows_test.go new file mode 100644 index 00000000..101071f5 --- /dev/null +++ b/pkg/dirs/dirs_windows_test.go @@ -0,0 +1,57 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//go:build windows + + +package dirs + +import ( + "path/filepath" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestDirs(t *testing.T) { + home := Home + assert.NotEqual(t, home, "", "home is empty") + + configHome := filepath.Join(home, ".dnote") + dataHome := filepath.Join(home, ".dnote") + cacheHome := filepath.Join(home, ".dnote") + + testCases := []struct { + got string + expected string + }{ + { + got: ConfigHome, + expected: configHome, + }, + { + got: DataHome, + expected: dataHome, + }, + { + got: CacheHome, + expected: cacheHome, + }, + } + + for _, tc := range testCases { + assert.Equal(t, tc.got, tc.expected, "result mismatch") + } +} diff --git a/pkg/e2e/server_test.go b/pkg/e2e/server_test.go new file mode 100644 index 00000000..e8ca3da6 --- /dev/null +++ b/pkg/e2e/server_test.go @@ -0,0 +1,353 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "bytes" + "fmt" + "net/http" + "os" + "os/exec" + "strings" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +var testServerBinary string + +func init() { + // Build server binary in temp directory + tmpDir := os.TempDir() + testServerBinary = fmt.Sprintf("%s/dnote-test-server", tmpDir) + buildCmd := exec.Command("go", "build", "-tags", "fts5", "-o", testServerBinary, "../server") + if out, err := buildCmd.CombinedOutput(); err != nil { + panic(fmt.Sprintf("failed to build server: %v\n%s", err, out)) + } +} + +func TestServerStart(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + port := "13456" // Use different port to avoid conflicts with main test server + + // Start server in background + cmd := exec.Command(testServerBinary, "start", "--port", port) + cmd.Env = append(os.Environ(), + "DBPath="+tmpDB, + ) + + if err := cmd.Start(); err != nil { + t.Fatalf("failed to start server: %v", err) + } + + // Ensure cleanup + cleanup := func() { + if cmd.Process != nil { + cmd.Process.Kill() + cmd.Wait() // Wait for process to fully exit + } + } + defer cleanup() + + // Wait for server to start and migrations to run + time.Sleep(3 * time.Second) + + // Verify server responds to health check + resp, err := http.Get(fmt.Sprintf("http://localhost:%s/health", port)) + if err != nil { + t.Fatalf("failed to reach server health endpoint: %v", err) + } + defer resp.Body.Close() + + assert.Equal(t, resp.StatusCode, 200, "health endpoint should return 200") + + // Kill server before checking database to avoid locks + cleanup() + + // Verify database file was created + if _, err := os.Stat(tmpDB); os.IsNotExist(err) { + t.Fatalf("database file was not created at %s", tmpDB) + } + + // Verify migrations ran by checking database + db, err := gorm.Open(sqlite.Open(tmpDB), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open test database: %v", err) + } + + // Verify migrations ran + var count int64 + if err := db.Raw("SELECT COUNT(*) FROM schema_migrations").Scan(&count).Error; err != nil { + t.Fatalf("schema_migrations table not found: %v", err) + } + if count == 0 { + t.Fatal("no migrations were run") + } + + // Verify FTS table exists and is functional + if err := db.Exec("SELECT * FROM notes_fts LIMIT 1").Error; err != nil { + t.Fatalf("notes_fts table not found or not functional: %v", err) + } +} + +func TestServerVersion(t *testing.T) { + cmd := exec.Command("go", "run", "-tags", "fts5", "../server", "version") + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("version command failed: %v", err) + } + + outputStr := string(output) + if !strings.Contains(outputStr, "dnote-server-") { + t.Errorf("expected version output to contain 'dnote-server-', got: %s", outputStr) + } +} + +func TestServerRootCommand(t *testing.T) { + cmd := exec.Command(testServerBinary) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("server command failed: %v", err) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Dnote server - a simple command line notebook"), true, "output should contain description") + assert.Equal(t, strings.Contains(outputStr, "start: Start the server"), true, "output should contain start command") + assert.Equal(t, strings.Contains(outputStr, "version: Print the version"), true, "output should contain version command") +} + +func TestServerStartHelp(t *testing.T) { + cmd := exec.Command(testServerBinary, "start", "--help") + output, _ := cmd.CombinedOutput() + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "dnote-server start [flags]"), true, "output should contain usage") + assert.Equal(t, strings.Contains(outputStr, "--port"), true, "output should contain port flag") + assert.Equal(t, strings.Contains(outputStr, "--baseUrl"), true, "output should contain baseUrl flag") + assert.Equal(t, strings.Contains(outputStr, "--dbPath"), true, "output should contain dbPath flag") + assert.Equal(t, strings.Contains(outputStr, "--disableRegistration"), true, "output should contain disableRegistration flag") +} + +func TestServerStartInvalidConfig(t *testing.T) { + cmd := exec.Command(testServerBinary, "start") + // Set invalid BaseURL to trigger validation failure + cmd.Env = []string{"BaseURL=not-a-valid-url"} + + output, err := cmd.CombinedOutput() + + // Should exit with non-zero status + if err == nil { + t.Fatal("expected command to fail with invalid config") + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Error:"), true, "output should contain error message") + assert.Equal(t, strings.Contains(outputStr, "Invalid BaseURL"), true, "output should mention invalid BaseURL") + assert.Equal(t, strings.Contains(outputStr, "dnote-server start [flags]"), true, "output should show usage") + assert.Equal(t, strings.Contains(outputStr, "--baseUrl"), true, "output should show flags") +} + +func TestServerUnknownCommand(t *testing.T) { + cmd := exec.Command(testServerBinary, "unknown") + output, err := cmd.CombinedOutput() + + // Should exit with non-zero status + if err == nil { + t.Fatal("expected command to fail with unknown command") + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Unknown command"), true, "output should contain unknown command message") + assert.Equal(t, strings.Contains(outputStr, "Dnote server - a simple command line notebook"), true, "output should show help") +} + +func TestServerUserCreate(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + cmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "password123") + output, err := cmd.CombinedOutput() + + if err != nil { + t.Fatalf("user create failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "User created successfully"), true, "output should show success message") + assert.Equal(t, strings.Contains(outputStr, "test@example.com"), true, "output should show email") + + // Verify user exists in database + db, err := gorm.Open(sqlite.Open(tmpDB), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + defer func() { + sqlDB, _ := db.DB() + sqlDB.Close() + }() + + var count int64 + db.Table("users").Count(&count) + assert.Equal(t, count, int64(1), "should have created 1 user") +} + +func TestServerUserCreateShortPassword(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + cmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "short") + output, err := cmd.CombinedOutput() + + // Should fail with short password + if err == nil { + t.Fatal("expected command to fail with short password") + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "password should be longer than 8 characters"), true, "output should show password error") +} + +func TestServerUserResetPassword(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create user first + createCmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "oldpassword123") + if output, err := createCmd.CombinedOutput(); err != nil { + t.Fatalf("failed to create user: %v\nOutput: %s", err, output) + } + + // Reset password + resetCmd := exec.Command(testServerBinary, "user", "reset-password", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "newpassword123") + output, err := resetCmd.CombinedOutput() + + if err != nil { + t.Fatalf("reset-password failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "Password reset successfully"), true, "output should show success message") +} + +func TestServerUserRemove(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create user first + createCmd := exec.Command(testServerBinary, "user", "create", + "--dbPath", tmpDB, + "--email", "test@example.com", + "--password", "password123") + if output, err := createCmd.CombinedOutput(); err != nil { + t.Fatalf("failed to create user: %v\nOutput: %s", err, output) + } + + // Remove user with confirmation + removeCmd := exec.Command(testServerBinary, "user", "remove", + "--dbPath", tmpDB, + "--email", "test@example.com") + + // Pipe "y" to stdin to confirm removal + stdin, err := removeCmd.StdinPipe() + if err != nil { + t.Fatalf("failed to create stdin pipe: %v", err) + } + + // Capture output + stdout, err := removeCmd.StdoutPipe() + if err != nil { + t.Fatalf("failed to create stdout pipe: %v", err) + } + + var stderr bytes.Buffer + removeCmd.Stderr = &stderr + + // Start command + if err := removeCmd.Start(); err != nil { + t.Fatalf("failed to start remove command: %v", err) + } + + // Wait for prompt and send "y" to confirm + if err := assert.RespondToPrompt(stdout, stdin, "Remove user test@example.com?", "y\n", 10*time.Second); err != nil { + t.Fatalf("failed to confirm removal: %v", err) + } + + // Wait for command to finish + if err := removeCmd.Wait(); err != nil { + t.Fatalf("user remove failed: %v\nStderr: %s", err, stderr.String()) + } + + // Verify user was removed + db, err := gorm.Open(sqlite.Open(tmpDB), &gorm.Config{}) + if err != nil { + t.Fatalf("failed to open database: %v", err) + } + defer func() { + sqlDB, _ := db.DB() + sqlDB.Close() + }() + + var count int64 + db.Table("users").Count(&count) + assert.Equal(t, count, int64(0), "should have 0 users after removal") +} + +func TestServerUserCreateHelp(t *testing.T) { + cmd := exec.Command(testServerBinary, "user", "create", "--help") + output, err := cmd.CombinedOutput() + + if err != nil { + t.Fatalf("help command failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + + // Verify help shows double-dash flags for consistency with CLI + assert.Equal(t, strings.Contains(outputStr, "--email"), true, "help should show --email (double dash)") + assert.Equal(t, strings.Contains(outputStr, "--password"), true, "help should show --password (double dash)") + assert.Equal(t, strings.Contains(outputStr, "--dbPath"), true, "help should show --dbPath (double dash)") +} + +func TestServerUserList(t *testing.T) { + tmpDB := t.TempDir() + "/test.db" + + // Create two users + exec.Command(testServerBinary, "user", "create", "--dbPath", tmpDB, "--email", "alice@example.com", "--password", "password123").CombinedOutput() + exec.Command(testServerBinary, "user", "create", "--dbPath", tmpDB, "--email", "bob@example.com", "--password", "password123").CombinedOutput() + + // List users + listCmd := exec.Command(testServerBinary, "user", "list", "--dbPath", tmpDB) + output, err := listCmd.CombinedOutput() + + if err != nil { + t.Fatalf("user list failed: %v\nOutput: %s", err, output) + } + + outputStr := string(output) + assert.Equal(t, strings.Contains(outputStr, "alice@example.com"), true, "output should have alice") + assert.Equal(t, strings.Contains(outputStr, "bob@example.com"), true, "output should have bob") +} diff --git a/pkg/e2e/sync/basic_test.go b/pkg/e2e/sync/basic_test.go new file mode 100644 index 00000000..d49b0cd9 --- /dev/null +++ b/pkg/e2e/sync/basic_test.go @@ -0,0 +1,3811 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "fmt" + "os" + "testing" + + "github.com/dnote/dnote/pkg/assert" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + "github.com/dnote/dnote/pkg/cli/testutils" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/server/database" + apitest "github.com/dnote/dnote/pkg/server/testutils" +) + +func TestSync_Empty(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + return map[string]string{} + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + // Test + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 0, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 0, + serverUserMaxUSN: 0, + }) + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) +} + +func TestSync_oneway(t *testing.T) { + t.Run("cli to api only", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) { + apitest.MustExec(t, env.ServerDB.Model(&user).Update("max_usn", 0), "updating user max_usn") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + } + + assert := func(t *testing.T, env testEnv, user database.User) { + cliDB := env.DB + + // test client + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 3, + clientBookCount: 2, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 3, + serverBookCount: 2, + serverUserMaxUSN: 5, + }) + + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS, cliNote2JS, cliNote1CSS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + + // assert on usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote2JS.USN, 0, "cliNote2JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js2", "cliNote2JS Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + + // test server + var apiBookJS, apiBookCSS database.Book + var apiNote1JS, apiNote2JS, apiNote1CSS database.Note + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Where("uuid = ?", cliNote1JS.UUID).First(&apiNote1JS), "getting js1 note") + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Where("uuid = ?", cliNote2JS.UUID).First(&apiNote2JS), "getting js2 note") + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Where("uuid = ?", cliNote1CSS.UUID).First(&apiNote1CSS), "getting css1 note") + apitest.MustExec(t, env.ServerDB.Model(&database.Book{}).Where("uuid = ?", cliBookJS.UUID).First(&apiBookJS), "getting js book") + apitest.MustExec(t, env.ServerDB.Model(&database.Book{}).Where("uuid = ?", cliBookCSS.UUID).First(&apiBookCSS), "getting css book") + + // assert usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS usn mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + // client must have generated uuids + assert.NotEqual(t, apiNote1JS.UUID, "", "apiNote1JS UUID mismatch") + assert.NotEqual(t, apiNote2JS.UUID, "", "apiNote2JS UUID mismatch") + assert.NotEqual(t, apiNote1CSS.UUID, "", "apiNote1CSS UUID mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote2JS.Deleted, false, "apiNote2JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + // assert on body and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js2", "apiNote2JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + } + + t.Run("stepSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + assert(t, env, user) + }) + + t.Run("fullSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + + assert(t, env, user) + }) + }) + + t.Run("cli to api with edit and delete", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) { + apiDB := env.ServerDB + apitest.MustExec(t, apiDB.Model(&user).Update("max_usn", 0), "updating user max_usn") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css2") + + var nid, nid2 string + cliDB := env.DB + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js3"), &nid) + cliDatabase.MustScan(t, "getting id of note to delete", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "css2"), &nid2) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js3-edited") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "css", nid2) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css4") + } + + assert := func(t *testing.T, env testEnv, user database.User) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 6, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 6, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // test cli + var cliN1, cliN2, cliN3, cliN4, cliN5, cliN6 cliDatabase.Note + var cliB1, cliB2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliN1", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliN1.UUID, &cliN1.Body, &cliN1.USN) + cliDatabase.MustScan(t, "finding cliN2", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliN2.UUID, &cliN2.Body, &cliN2.USN) + cliDatabase.MustScan(t, "finding cliN3", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js3-edited"), &cliN3.UUID, &cliN3.Body, &cliN3.USN) + cliDatabase.MustScan(t, "finding cliN4", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliN4.UUID, &cliN4.Body, &cliN4.USN) + cliDatabase.MustScan(t, "finding cliN5", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css3"), &cliN5.UUID, &cliN5.Body, &cliN5.USN) + cliDatabase.MustScan(t, "finding cliN6", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css4"), &cliN6.UUID, &cliN6.Body, &cliN6.USN) + cliDatabase.MustScan(t, "finding cliB1", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliB1.UUID, &cliB1.Label, &cliB1.USN) + cliDatabase.MustScan(t, "finding cliB2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliB2.UUID, &cliB2.Label, &cliB2.USN) + + // assert on usn + assert.NotEqual(t, cliN1.USN, 0, "cliN1 USN mismatch") + assert.NotEqual(t, cliN2.USN, 0, "cliN2 USN mismatch") + assert.NotEqual(t, cliN3.USN, 0, "cliN3 USN mismatch") + assert.NotEqual(t, cliN4.USN, 0, "cliN4 USN mismatch") + assert.NotEqual(t, cliN5.USN, 0, "cliN5 USN mismatch") + assert.NotEqual(t, cliN6.USN, 0, "cliN6 USN mismatch") + assert.NotEqual(t, cliB1.USN, 0, "cliB1 USN mismatch") + assert.NotEqual(t, cliB2.USN, 0, "cliB2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliN1.Body, "js1", "cliN1 Body mismatch") + assert.Equal(t, cliN2.Body, "js2", "cliN2 Body mismatch") + assert.Equal(t, cliN3.Body, "js3-edited", "cliN3 Body mismatch") + assert.Equal(t, cliN4.Body, "css1", "cliN4 Body mismatch") + assert.Equal(t, cliN5.Body, "css3", "cliN5 Body mismatch") + assert.Equal(t, cliN6.Body, "css4", "cliN6 Body mismatch") + assert.Equal(t, cliB1.Label, "js", "cliB1 Label mismatch") + assert.Equal(t, cliB2.Label, "css", "cliB2 Label mismatch") + // assert on deleted + assert.Equal(t, cliN1.Deleted, false, "cliN1 Deleted mismatch") + assert.Equal(t, cliN2.Deleted, false, "cliN2 Deleted mismatch") + assert.Equal(t, cliN3.Deleted, false, "cliN3 Deleted mismatch") + assert.Equal(t, cliN4.Deleted, false, "cliN4 Deleted mismatch") + assert.Equal(t, cliN5.Deleted, false, "cliN5 Deleted mismatch") + assert.Equal(t, cliN6.Deleted, false, "cliN6 Deleted mismatch") + assert.Equal(t, cliB1.Deleted, false, "cliB1 Deleted mismatch") + assert.Equal(t, cliB2.Deleted, false, "cliB2 Deleted mismatch") + + // test api + var apiN1, apiN2, apiN3, apiN4, apiN5, apiN6 database.Note + var apiB1, apiB2 database.Book + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN1.UUID).First(&apiN1), "finding apiN1") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN2.UUID).First(&apiN2), "finding apiN2") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN3.UUID).First(&apiN3), "finding apiN3") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN4.UUID).First(&apiN4), "finding apiN4") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN5.UUID).First(&apiN5), "finding apiN5") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliN6.UUID).First(&apiN6), "finding apiN6") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliB1.UUID).First(&apiB1), "finding apiB1") + apitest.MustExec(t, apiDB.Where("uuid = ?", cliB2.UUID).First(&apiB2), "finding apiB2") + + // assert on usn + assert.NotEqual(t, apiN1.USN, 0, "apiN1 usn mismatch") + assert.NotEqual(t, apiN2.USN, 0, "apiN2 usn mismatch") + assert.NotEqual(t, apiN3.USN, 0, "apiN3 usn mismatch") + assert.NotEqual(t, apiN4.USN, 0, "apiN4 usn mismatch") + assert.NotEqual(t, apiN5.USN, 0, "apiN5 usn mismatch") + assert.NotEqual(t, apiN6.USN, 0, "apiN6 usn mismatch") + assert.NotEqual(t, apiB1.USN, 0, "apiB1 usn mismatch") + assert.NotEqual(t, apiB2.USN, 0, "apiB2 usn mismatch") + // client must have generated uuids + assert.NotEqual(t, apiN1.UUID, "", "apiN1 UUID mismatch") + assert.NotEqual(t, apiN2.UUID, "", "apiN2 UUID mismatch") + assert.NotEqual(t, apiN3.UUID, "", "apiN3 UUID mismatch") + assert.NotEqual(t, apiN4.UUID, "", "apiN4 UUID mismatch") + assert.NotEqual(t, apiN5.UUID, "", "apiN5 UUID mismatch") + assert.NotEqual(t, apiN6.UUID, "", "apiN6 UUID mismatch") + assert.NotEqual(t, apiB1.UUID, "", "apiB1 UUID mismatch") + assert.NotEqual(t, apiB2.UUID, "", "apiB2 UUID mismatch") + // assert on deleted + assert.Equal(t, apiN1.Deleted, false, "apiN1 Deleted mismatch") + assert.Equal(t, apiN2.Deleted, false, "apiN2 Deleted mismatch") + assert.Equal(t, apiN3.Deleted, false, "apiN3 Deleted mismatch") + assert.Equal(t, apiN4.Deleted, false, "apiN4 Deleted mismatch") + assert.Equal(t, apiN5.Deleted, false, "apiN5 Deleted mismatch") + assert.Equal(t, apiN6.Deleted, false, "apiN6 Deleted mismatch") + assert.Equal(t, apiB1.Deleted, false, "apiB1 Deleted mismatch") + assert.Equal(t, apiB2.Deleted, false, "apiB2 Deleted mismatch") + // assert on body and labels + assert.Equal(t, apiN1.Body, "js1", "apiN1 Body mismatch") + assert.Equal(t, apiN2.Body, "js2", "apiN2 Body mismatch") + assert.Equal(t, apiN3.Body, "js3-edited", "apiN3 Body mismatch") + assert.Equal(t, apiN4.Body, "css1", "apiN4 Body mismatch") + assert.Equal(t, apiN5.Body, "css3", "apiN5 Body mismatch") + assert.Equal(t, apiN6.Body, "css4", "apiN6 Body mismatch") + assert.Equal(t, apiB1.Label, "js", "apiB1 Label mismatch") + assert.Equal(t, apiB2.Label, "css", "apiB2 Label mismatch") + } + + t.Run("stepSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + assert(t, env, user) + }) + + t.Run("fullSync", func(t *testing.T) { + + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + + assert(t, env, user) + }) + }) + + t.Run("api to cli", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + apiDB := env.ServerDB + + apitest.MustExec(t, apiDB.Model(&user).Update("max_usn", 0), "updating user max_usn") + + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + jsNote2UUID := apiCreateNote(t, env, user, jsBookUUID, "js2", "adding js note 2") + cssNote2UUID := apiCreateNote(t, env, user, cssBookUUID, "css2", "adding css note 2") + linuxBookUUID := apiCreateBook(t, env, user, "linux", "adding linux book") + linuxNote1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux1", "adding linux note 1") + apiPatchNote(t, env, user, jsNote2UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, linuxBookUUID), "moving js note 2 to linux") + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + cssNote3UUID := apiCreateNote(t, env, user, cssBookUUID, "css3", "adding css note 3") + bashBookUUID := apiCreateBook(t, env, user, "bash", "adding bash book") + bashNote1UUID := apiCreateNote(t, env, user, bashBookUUID, "bash1", "adding bash note 1") + + // delete the linux book and its two notes + apiDeleteBook(t, env, user, linuxBookUUID, "deleting linux book") + + apiPatchNote(t, env, user, cssNote2UUID, fmt.Sprintf(`{"content": "%s"}`, "css2-edited"), "editing css 2 body") + bashNote2UUID := apiCreateNote(t, env, user, bashBookUUID, "bash2", "adding bash note 2") + linuxBook2UUID := apiCreateBook(t, env, user, "linux", "adding new linux book") + linux2Note1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux-new-1", "adding linux note 1") + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "jsNote2UUID": jsNote2UUID, + "cssBookUUID": cssBookUUID, + "cssNote1UUID": cssNote1UUID, + "cssNote2UUID": cssNote2UUID, + "cssNote3UUID": cssNote3UUID, + "linuxBookUUID": linuxBookUUID, + "linuxNote1UUID": linuxNote1UUID, + "bashBookUUID": bashBookUUID, + "bashNote1UUID": bashNote1UUID, + "bashNote2UUID": bashNote2UUID, + "linuxBook2UUID": linuxBook2UUID, + "linux2Note1UUID": linux2Note1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 6, + clientBookCount: 3, + clientLastMaxUSN: 21, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 9, + serverBookCount: 5, + serverUserMaxUSN: 21, + }) + + // test server + var apiNote1JS, apiNote2JS, apiNote1CSS, apiNote2CSS, apiNote3CSS, apiNote1Bash, apiNote2Bash, apiNote1Linux, apiNote2Linux, apiNote1LinuxDup database.Note + var apiBookJS, apiBookCSS, apiBookBash, apiBookLinux, apiBookLinuxDup database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding api js note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote2UUID"]).First(&apiNote2JS), "finding api js note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1CSS), "finding api css note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote2UUID"]).First(&apiNote2CSS), "finding api css note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote3UUID"]).First(&apiNote3CSS), "finding api css note 3") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxNote1UUID"]).First(&apiNote1Linux), "finding api linux note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote2UUID"]).First(&apiNote2Linux), "finding api linux note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote1UUID"]).First(&apiNote1Bash), "finding api bash note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote2UUID"]).First(&apiNote2Bash), "finding api bash note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linux2Note1UUID"]).First(&apiNote1LinuxDup), "finding api linux 2 note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding api js book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding api css book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashBookUUID"]).First(&apiBookBash), "finding api bash book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBookUUID"]).First(&apiBookLinux), "finding api linux book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBook2UUID"]).First(&apiBookLinuxDup), "finding api linux book 2") + + // assert on server Label + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiNote2CSS.USN, 0, "apiNote2CSS USN mismatch") + assert.NotEqual(t, apiNote3CSS.USN, 0, "apiNote3CSS USN mismatch") + assert.NotEqual(t, apiNote1Linux.USN, 0, "apiNote1Linux USN mismatch") + assert.NotEqual(t, apiNote2Linux.USN, 0, "apiNote2Linux USN mismatch") + assert.NotEqual(t, apiNote1Bash.USN, 0, "apiNote1Bash USN mismatch") + assert.NotEqual(t, apiNote2Bash.USN, 0, "apiNote2Bash USN mismatch") + assert.NotEqual(t, apiNote1LinuxDup.USN, 0, "apiNote1LinuxDup USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apibookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apibookCSS USN mismatch") + assert.NotEqual(t, apiBookBash.USN, 0, "apibookBash USN mismatch") + assert.NotEqual(t, apiBookLinux.USN, 0, "apibookLinux USN mismatch") + assert.NotEqual(t, apiBookLinuxDup.USN, 0, "apiBookLinuxDup USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "", "apiNote2JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote2CSS.Body, "css2-edited", "apiNote2CSS Body mismatch") + assert.Equal(t, apiNote3CSS.Body, "css3", "apiNote3CSS Body mismatch") + assert.Equal(t, apiNote1Linux.Body, "", "apiNote1Linux Body mismatch") + assert.Equal(t, apiNote2Linux.Body, "", "apiNote2Linux Body mismatch") + assert.Equal(t, apiNote1Bash.Body, "bash1", "apiNote1Bash Body mismatch") + assert.Equal(t, apiNote2Bash.Body, "bash2", "apiNote2Bash Body mismatch") + assert.Equal(t, apiNote1LinuxDup.Body, "linux-new-1", "apiNote1LinuxDup Body mismatch") + assert.Equal(t, apiBookJS.Label, "", "apibookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apibookCSS Label mismatch") + assert.Equal(t, apiBookBash.Label, "bash", "apibookBash Label mismatch") + assert.Equal(t, apiBookLinux.Label, "", "apibookLinux Label mismatch") + assert.Equal(t, apiBookLinuxDup.Label, "linux", "apiBookLinuxDup Label mismatch") + // assert on uuids + assert.NotEqual(t, apiNote1JS.UUID, "", "apiNote1JS UUID mismatch") + assert.NotEqual(t, apiNote2JS.UUID, "", "apiNote2JS UUID mismatch") + assert.NotEqual(t, apiNote1CSS.UUID, "", "apiNote1CSS UUID mismatch") + assert.NotEqual(t, apiNote2CSS.UUID, "", "apiNote2CSS UUID mismatch") + assert.NotEqual(t, apiNote3CSS.UUID, "", "apiNote3CSS UUID mismatch") + assert.NotEqual(t, apiNote1Linux.UUID, "", "apiNote1Linux UUID mismatch") + assert.NotEqual(t, apiNote2Linux.UUID, "", "apiNote2Linux UUID mismatch") + assert.NotEqual(t, apiNote1Bash.UUID, "", "apiNote1Bash UUID mismatch") + assert.NotEqual(t, apiNote2Bash.UUID, "", "apiNote2Bash UUID mismatch") + assert.NotEqual(t, apiNote2Bash.UUID, "", "apiNote2Bash UUID mismatch") + assert.NotEqual(t, apiBookJS.UUID, "", "apibookJS UUID mismatch") + assert.NotEqual(t, apiBookCSS.UUID, "", "apibookCSS UUID mismatch") + assert.NotEqual(t, apiBookBash.UUID, "", "apibookBash UUID mismatch") + assert.NotEqual(t, apiBookLinux.UUID, "", "apibookLinux UUID mismatch") + assert.NotEqual(t, apiBookLinuxDup.UUID, "", "apiBookLinuxDup UUID mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote2JS.Deleted, true, "apiNote2JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiNote2CSS.Deleted, false, "apiNote2CSS Deleted mismatch") + assert.Equal(t, apiNote3CSS.Deleted, false, "apiNote3CSS Deleted mismatch") + assert.Equal(t, apiNote1Linux.Deleted, true, "apiNote1Linux Deleted mismatch") + assert.Equal(t, apiNote2Linux.Deleted, true, "apiNote2Linux Deleted mismatch") + assert.Equal(t, apiNote1Bash.Deleted, false, "apiNote1Bash Deleted mismatch") + assert.Equal(t, apiNote2Bash.Deleted, false, "apiNote2Bash Deleted mismatch") + assert.Equal(t, apiNote2Bash.Deleted, false, "apiNote2Bash Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apibookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apibookCSS Deleted mismatch") + assert.Equal(t, apiBookBash.Deleted, false, "apibookBash Deleted mismatch") + assert.Equal(t, apiBookLinux.Deleted, true, "apibookLinux Deleted mismatch") + assert.Equal(t, apiBookLinuxDup.Deleted, false, "apiBookLinuxDup Deleted mismatch") + + // test client + var cliBookCSS, cliBookBash, cliBookLinux cliDatabase.Book + var cliNote1CSS, cliNote2CSS, cliNote3CSS, cliNote1Bash, cliNote2Bash, cliNote1Linux cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT label FROM books WHERE uuid = ?", ids["cssBookUUID"]), &cliBookCSS.Label) + cliDatabase.MustScan(t, "finding cli book bash", cliDB.QueryRow("SELECT label FROM books WHERE uuid = ?", ids["bashBookUUID"]), &cliBookBash.Label) + cliDatabase.MustScan(t, "finding cli book linux2", cliDB.QueryRow("SELECT label FROM books WHERE uuid = ?", ids["linuxBook2UUID"]), &cliBookLinux.Label) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote1CSS.UUID), &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote2CSS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote2CSS.UUID), &cliNote2CSS.Body, &cliNote2CSS.USN) + cliDatabase.MustScan(t, "finding cliNote3CSS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote3CSS.UUID), &cliNote3CSS.Body, &cliNote3CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1Bash", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote1Bash.UUID), &cliNote1Bash.Body, &cliNote1Bash.USN) + cliDatabase.MustScan(t, "finding cliNote2Bash", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote2Bash.UUID), &cliNote2Bash.Body, &cliNote2Bash.USN) + cliDatabase.MustScan(t, "finding cliNote2Bash", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", apiNote1LinuxDup.UUID), &cliNote1Linux.Body, &cliNote1Linux.USN) + + // assert on usn + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS usn mismatch") + assert.NotEqual(t, cliNote2CSS.USN, 0, "cliNote2CSS usn mismatch") + assert.NotEqual(t, cliNote3CSS.USN, 0, "cliNote3CSS usn mismatch") + assert.NotEqual(t, cliNote1Bash.USN, 0, "cliNote1Bash usn mismatch") + assert.NotEqual(t, cliNote2Bash.USN, 0, "cliNote2Bash usn mismatch") + assert.NotEqual(t, cliNote1Linux.USN, 0, "cliNote1Linux usn mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote2CSS.Body, "css2-edited", "cliNote2CSS Body mismatch") + assert.Equal(t, cliNote3CSS.Body, "css3", "cliNote3CSS Body mismatch") + assert.Equal(t, cliNote1Bash.Body, "bash1", "cliNote1Bash Body mismatch") + assert.Equal(t, cliNote2Bash.Body, "bash2", "cliNote2Bash Body mismatch") + assert.Equal(t, cliNote1Linux.Body, "linux-new-1", "cliNote1Linux Body mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookBash.Label, "bash", "cliBookBash Label mismatch") + assert.Equal(t, cliBookLinux.Label, "linux", "cliBookLinux Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote2CSS.Deleted, false, "cliNote2CSS Deleted mismatch") + assert.Equal(t, cliNote3CSS.Deleted, false, "cliNote3CSS Deleted mismatch") + assert.Equal(t, cliNote1Bash.Deleted, false, "cliNote1Bash Deleted mismatch") + assert.Equal(t, cliNote2Bash.Deleted, false, "cliNote2Bash Deleted mismatch") + assert.Equal(t, cliNote1Linux.Deleted, false, "cliNote1Linux Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookBash.Deleted, false, "cliBookBash Deleted mismatch") + assert.Equal(t, cliBookLinux.Deleted, false, "cliBookLinux Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) +} + +func TestSync_twoway(t *testing.T) { + t.Run("once", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + jsNote2UUID := apiCreateNote(t, env, user, jsBookUUID, "js2", "adding js note 2") + cssNote2UUID := apiCreateNote(t, env, user, cssBookUUID, "css2", "adding css note 2") + linuxBookUUID := apiCreateBook(t, env, user, "linux", "adding linux book") + linuxNote1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux1", "adding linux note 1") + apiPatchNote(t, env, user, jsNote2UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, linuxBookUUID), "moving js note 2 to linux") + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + cssNote3UUID := apiCreateNote(t, env, user, cssBookUUID, "css3", "adding css note 3") + bashBookUUID := apiCreateBook(t, env, user, "bash", "adding bash book") + bashNote1UUID := apiCreateNote(t, env, user, bashBookUUID, "bash1", "adding bash note 1") + apiDeleteBook(t, env, user, linuxBookUUID, "deleting linux book") + apiPatchNote(t, env, user, cssNote2UUID, fmt.Sprintf(`{"content": "%s"}`, "css2-edited"), "editing css 2 body") + bashNote2UUID := apiCreateNote(t, env, user, bashBookUUID, "bash2", "adding bash note 2") + linuxBook2UUID := apiCreateBook(t, env, user, "linux", "adding new linux book") + linux2Note1UUID := apiCreateNote(t, env, user, linuxBookUUID, "linux-new-1", "adding linux note 1") + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "algorithms", "-c", "algorithms1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js4") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "algorithms", "-c", "algorithms2") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "math", "-c", "math1") + + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js3"), &nid) + + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "algorithms") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css4") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "jsNote2UUID": jsNote2UUID, + "cssBookUUID": cssBookUUID, + "cssNote1UUID": cssNote1UUID, + "cssNote2UUID": cssNote2UUID, + "cssNote3UUID": cssNote3UUID, + "linuxBookUUID": linuxBookUUID, + "linuxNote1UUID": linuxNote1UUID, + "bashBookUUID": bashBookUUID, + "bashNote1UUID": bashNote1UUID, + "bashNote2UUID": bashNote2UUID, + "linuxBook2UUID": linuxBook2UUID, + "linux2Note1UUID": linux2Note1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 9, + clientBookCount: 6, + clientLastMaxUSN: 27, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 12, + serverBookCount: 8, + serverUserMaxUSN: 27, + }) + + // test client + var cliNote1CSS, cliNote2CSS, cliNote3CSS, cliNote1CSS2, cliNote1Bash, cliNote2Bash, cliNote1Linux, cliNote1Math, cliNote1JS cliDatabase.Note + var cliBookCSS, cliBookCSS2, cliBookBash, cliBookLinux, cliBookMath, cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote2CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css2-edited"), &cliNote2CSS.UUID, &cliNote2CSS.Body, &cliNote2CSS.USN) + cliDatabase.MustScan(t, "finding cliNote3CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css3"), &cliNote3CSS.UUID, &cliNote3CSS.Body, &cliNote3CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS2", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css4"), &cliNote1CSS2.UUID, &cliNote1CSS2.Body, &cliNote1CSS2.USN) + cliDatabase.MustScan(t, "finding cliNote1Bash", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "bash1"), &cliNote1Bash.UUID, &cliNote1Bash.Body, &cliNote1Bash.USN) + cliDatabase.MustScan(t, "finding cliNote2Bash", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "bash2"), &cliNote2Bash.UUID, &cliNote2Bash.Body, &cliNote2Bash.USN) + cliDatabase.MustScan(t, "finding cliNote1Linux", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "linux-new-1"), &cliNote1Linux.UUID, &cliNote1Linux.Body, &cliNote1Linux.USN) + cliDatabase.MustScan(t, "finding cliNote1Math", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "math1"), &cliNote1Math.UUID, &cliNote1Math.Body, &cliNote1Math.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js4"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + cliDatabase.MustScan(t, "finding cliBookBash", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "bash"), &cliBookBash.UUID, &cliBookBash.Label, &cliBookBash.USN) + cliDatabase.MustScan(t, "finding cliBookLinux", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "linux"), &cliBookLinux.UUID, &cliBookLinux.Label, &cliBookLinux.USN) + cliDatabase.MustScan(t, "finding cliBookMath", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "math"), &cliBookMath.UUID, &cliBookMath.Label, &cliBookMath.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliNote2CSS.USN, 0, "cliNote2CSS USN mismatch") + assert.NotEqual(t, cliNote3CSS.USN, 0, "cliNote3CSS USN mismatch") + assert.NotEqual(t, cliNote1CSS2.USN, 0, "cliNote1CSS2 USN mismatch") + assert.NotEqual(t, cliNote1Bash.USN, 0, "cliNote1Bash USN mismatch") + assert.NotEqual(t, cliNote2Bash.USN, 0, "cliNote2Bash USN mismatch") + assert.NotEqual(t, cliNote1Linux.USN, 0, "cliNote1Linux USN mismatch") + assert.NotEqual(t, cliNote1Math.USN, 0, "cliNote1Math USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookCSS2.USN, 0, "cliBookCSS2 USN mismatch") + assert.NotEqual(t, cliBookBash.USN, 0, "cliBookBash USN mismatch") + assert.NotEqual(t, cliBookMath.USN, 0, "cliBookMath USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote2CSS.Body, "css2-edited", "cliNote2CSS Body mismatch") + assert.Equal(t, cliNote3CSS.Body, "css3", "cliNote3CSS Body mismatch") + assert.Equal(t, cliNote1CSS2.Body, "css4", "cliNote1CSS2 Body mismatch") + assert.Equal(t, cliNote1Bash.Body, "bash1", "cliNote1Bash Body mismatch") + assert.Equal(t, cliNote2Bash.Body, "bash2", "cliNote2Bash Body mismatch") + assert.Equal(t, cliNote1Linux.Body, "linux-new-1", "cliNote1Linux Body mismatch") + assert.Equal(t, cliNote1Math.Body, "math1", "cliNote1Math Body mismatch") + assert.Equal(t, cliNote1JS.Body, "js4", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookCSS2.Label, "css_2", "cliBookCSS2 Label mismatch") + assert.Equal(t, cliBookBash.Label, "bash", "cliBookBash Label mismatch") + assert.Equal(t, cliBookMath.Label, "math", "cliBookMath Label mismatch") + assert.Equal(t, cliBookLinux.Label, "linux", "cliBookLinux Label mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote2CSS.Deleted, false, "cliNote2CSS Deleted mismatch") + assert.Equal(t, cliNote3CSS.Deleted, false, "cliNote3CSS Deleted mismatch") + assert.Equal(t, cliNote1CSS2.Deleted, false, "cliNote1CSS2 Deleted mismatch") + assert.Equal(t, cliNote1Bash.Deleted, false, "cliNote1Bash Deleted mismatch") + assert.Equal(t, cliNote2Bash.Deleted, false, "cliNote2Bash Deleted mismatch") + assert.Equal(t, cliNote1Linux.Deleted, false, "cliNote1Linux Deleted mismatch") + assert.Equal(t, cliNote1Math.Deleted, false, "cliNote1Math Deleted mismatch") + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookCSS2.Deleted, false, "cliBookCSS2 Deleted mismatch") + assert.Equal(t, cliBookBash.Deleted, false, "cliBookBash Deleted mismatch") + assert.Equal(t, cliBookMath.Deleted, false, "cliBookMath Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS, apiNote1CSS, apiNote2CSS, apiNote3CSS, apiNote1Linux, apiNote2Linux, apiNote1Bash, apiNote2Bash, apiNote1LinuxDup, apiNote1CSS2, apiNote1Math, apiNote1JS2 database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding api js note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1CSS), "finding api css note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote2UUID"]).First(&apiNote2CSS), "finding api css note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote3UUID"]).First(&apiNote3CSS), "finding api css note 3") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxNote1UUID"]).First(&apiNote1Linux), "finding api linux note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote2UUID"]).First(&apiNote2Linux), "finding api linux note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote1UUID"]).First(&apiNote1Bash), "finding api bash note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashNote2UUID"]).First(&apiNote2Bash), "finding api bash note 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linux2Note1UUID"]).First(&apiNote1LinuxDup), "finding api linux 2 note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS2.UUID).First(&apiNote1CSS2), "finding apiNote1CSS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1Math.UUID).First(&apiNote1Math), "finding apiNote1Math") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS.UUID).First(&apiNote1JS2), "finding apiNote1JS2") + var apiBookJS, apiBookCSS, apiBookLinux, apiBookBash, apiBookLinuxDup, apiBookCSS2, apiBookMath, apiBookJS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding api js book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding api css book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashBookUUID"]).First(&apiBookBash), "finding api bash book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBookUUID"]).First(&apiBookLinux), "finding api linux book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBook2UUID"]).First(&apiBookLinuxDup), "finding api linux book 2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS2.UUID).First(&apiBookCSS2), "finding apiBookCSS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookMath.UUID).First(&apiBookMath), "finding apiBookMath") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS.UUID).First(&apiBookJS2), "finding apiBookJS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS usn mismatch") + assert.NotEqual(t, apiNote2CSS.USN, 0, "apiNote2CSS usn mismatch") + assert.NotEqual(t, apiNote3CSS.USN, 0, "apiNote3CSS usn mismatch") + assert.NotEqual(t, apiNote1Linux.USN, 0, "apiNote1Linux usn mismatch") + assert.NotEqual(t, apiNote2Linux.USN, 0, "apiNote2Linux usn mismatch") + assert.NotEqual(t, apiNote1Bash.USN, 0, "apiNote1Bash usn mismatch") + assert.NotEqual(t, apiNote2Bash.USN, 0, "apiNote2Bash usn mismatch") + assert.NotEqual(t, apiNote1LinuxDup.USN, 0, "apiNote1LinuxDup usn mismatch") + assert.NotEqual(t, apiNote1CSS2.USN, 0, "apiNoteCSS2 usn mismatch") + assert.NotEqual(t, apiNote1Math.USN, 0, "apiNote1Math usn mismatch") + assert.NotEqual(t, apiNote1JS2.USN, 0, "apiNote1JS2 usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + assert.NotEqual(t, apiBookLinux.USN, 0, "apiBookLinux usn mismatch") + assert.NotEqual(t, apiBookBash.USN, 0, "apiBookBash usn mismatch") + assert.NotEqual(t, apiBookLinuxDup.USN, 0, "apiBookLinuxDup usn mismatch") + assert.NotEqual(t, apiBookCSS2.USN, 0, "apiBookCSS2 usn mismatch") + assert.NotEqual(t, apiBookMath.USN, 0, "apiBookMath usn mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS2 usn mismatch") + // assert on note bodys + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote2CSS.Body, "css2-edited", "apiNote2CSS Body mismatch") + assert.Equal(t, apiNote3CSS.Body, "css3", "apiNote3CSS Body mismatch") + assert.Equal(t, apiNote1Linux.Body, "", "apiNote1Linux Body mismatch") + assert.Equal(t, apiNote2Linux.Body, "", "apiNote2Linux Body mismatch") + assert.Equal(t, apiNote1Bash.Body, "bash1", "apiNote1Bash Body mismatch") + assert.Equal(t, apiNote2Bash.Body, "bash2", "apiNote2Bash Body mismatch") + assert.Equal(t, apiNote1LinuxDup.Body, "linux-new-1", "apiNote1LinuxDup Body mismatch") + assert.Equal(t, apiNote1CSS2.Body, "css4", "apiNote1CSS2 Body mismatch") + assert.Equal(t, apiNote1Math.Body, "math1", "apiNote1Math Body mismatch") + assert.Equal(t, apiNote1JS2.Body, "js4", "apiNote1JS2 Body mismatch") + // client must have generated uuids + assert.NotEqual(t, apiNote1CSS2.UUID, "", "apiNote1CSS2 uuid mismatch") + assert.NotEqual(t, apiNote1Math.UUID, "", "apiNote1Math uuid mismatch") + assert.NotEqual(t, apiNote1JS2.UUID, "", "apiNote1JS2 uuid mismatch") + // assert on labels + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookLinux.Label, "", "apiBookLinux Label mismatch") + assert.Equal(t, apiBookBash.Label, "bash", "apiBookBash Label mismatch") + assert.Equal(t, apiBookLinuxDup.Label, "linux", "apiBookLinuxDup Label mismatch") + assert.Equal(t, apiBookCSS2.Label, "css_2", "apiBookCSS2 Label mismatch") + assert.Equal(t, apiBookMath.Label, "math", "apiBookMath Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js", "apiBookJS2 Label mismatch") + // assert on note deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiNote2CSS.Deleted, false, "apiNote2CSS Deleted mismatch") + assert.Equal(t, apiNote3CSS.Deleted, false, "apiNote3CSS Deleted mismatch") + assert.Equal(t, apiNote1Linux.Deleted, true, "apiNote1Linux Deleted mismatch") + assert.Equal(t, apiNote2Linux.Deleted, true, "apiNote2Linux Deleted mismatch") + assert.Equal(t, apiNote1Bash.Deleted, false, "apiNote1Bash Deleted mismatch") + assert.Equal(t, apiNote2Bash.Deleted, false, "apiNote2Bash Deleted mismatch") + assert.Equal(t, apiNote1LinuxDup.Deleted, false, "apiNote1LinuxDup Deleted mismatch") + assert.Equal(t, apiNote1CSS2.Deleted, false, "apiNote1CSS2 Deleted mismatch") + assert.Equal(t, apiNote1Math.Deleted, false, "apiNote1Math Deleted mismatch") + assert.Equal(t, apiNote1JS2.Deleted, false, "apiNote1JS2 Deleted mismatch") + // assert on book deleted + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookLinux.Deleted, true, "apiBookLinux Deleted mismatch") + assert.Equal(t, apiBookBash.Deleted, false, "apiBookBash Deleted mismatch") + assert.Equal(t, apiBookLinuxDup.Deleted, false, "apiBookLinuxDup Deleted mismatch") + assert.Equal(t, apiBookCSS2.Deleted, false, "apiBookCSS2 Deleted mismatch") + assert.Equal(t, apiBookMath.Deleted, false, "apiBookMath Deleted mismatch") + assert.Equal(t, apiBookJS2.Deleted, false, "apiBookJS2 Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("twice", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "math", "-c", "math1") + + var nid string + cliDB := env.DB + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "math1"), &nid) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "math", nid, "-c", "math1-edited") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + scssBookUUID := apiCreateBook(t, env, user, "scss", "adding a scss book") + apiPatchNote(t, env, user, cssNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, scssBookUUID), "moving css note 1 to scss") + + var n1UUID string + cliDatabase.MustScan(t, "getting math1-edited note UUID", cliDB.QueryRow("SELECT uuid FROM notes WHERE body = ?", "math1-edited"), &n1UUID) + apiPatchNote(t, env, user, n1UUID, fmt.Sprintf(`{"content": "%s", "public": true}`, "math1-edited"), "editing math1 note") + + cssNote2UUID := apiCreateNote(t, env, user, cssBookUUID, "css2", "adding css note 2") + apiDeleteBook(t, env, user, cssBookUUID, "deleting css book") + + bashBookUUID := apiCreateBook(t, env, user, "bash", "adding a bash book") + algorithmsBookUUID := apiCreateBook(t, env, user, "algorithms", "adding a algorithms book") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js3") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "algorithms", "-c", "algorithms1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + "scssBookUUID": scssBookUUID, + "cssNote1UUID": cssNote1UUID, + "cssNote2UUID": cssNote2UUID, + "bashBookUUID": bashBookUUID, + "algorithmsBookUUID": algorithmsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + cliDB := env.DB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 5, + clientBookCount: 6, + clientLastMaxUSN: 17, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 6, + serverBookCount: 7, + serverUserMaxUSN: 17, + }) + + // test client + var cliNote1JS, cliNote2JS, cliNote1SCSS, cliNote1Math, cliNote1Alg2 cliDatabase.Note + var cliBookJS, cliBookSCSS, cliBookMath, cliBookBash, cliBookAlg, cliBookAlg2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js3"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliNote1SCSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1SCSS.UUID, &cliNote1SCSS.Body, &cliNote1SCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1Math", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "math1-edited"), &cliNote1Math.UUID, &cliNote1Math.Body, &cliNote1Math.USN) + cliDatabase.MustScan(t, "finding cliNote1Alg2", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "algorithms1"), &cliNote1Alg2.UUID, &cliNote1Alg2.Body, &cliNote1Alg2.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookSCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "scss"), &cliBookSCSS.UUID, &cliBookSCSS.Label, &cliBookSCSS.USN) + cliDatabase.MustScan(t, "finding cliBookMath", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "math"), &cliBookMath.UUID, &cliBookMath.Label, &cliBookMath.USN) + cliDatabase.MustScan(t, "finding cliBookBash", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "bash"), &cliBookBash.UUID, &cliBookBash.Label, &cliBookBash.USN) + cliDatabase.MustScan(t, "finding cliBookAlg", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "algorithms"), &cliBookAlg.UUID, &cliBookAlg.Label, &cliBookAlg.USN) + cliDatabase.MustScan(t, "finding cliBookAlg2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "algorithms_2"), &cliBookAlg2.UUID, &cliBookAlg2.Label, &cliBookAlg2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote2JS.USN, 0, "cliNote2JS USN mismatch") + assert.NotEqual(t, cliNote1SCSS.USN, 0, "cliNote1SCSS USN mismatch") + assert.NotEqual(t, cliNote1Math.USN, 0, "cliNote1Math USN mismatch") + assert.NotEqual(t, cliNote1Alg2.USN, 0, "cliNote1Alg2 USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookSCSS.USN, 0, "cliBookSCSS USN mismatch") + assert.NotEqual(t, cliBookMath.USN, 0, "cliBookMath USN mismatch") + assert.NotEqual(t, cliBookBash.USN, 0, "cliBookBash USN mismatch") + assert.NotEqual(t, cliBookAlg.USN, 0, "cliBookAlg USN mismatch") + assert.NotEqual(t, cliBookAlg2.USN, 0, "cliBookAlg2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js3", "cliNote2JS Body mismatch") + assert.Equal(t, cliNote1SCSS.Body, "css1", "cliNote1SCSS Body mismatch") + assert.Equal(t, cliNote1Math.Body, "math1-edited", "cliNote1Math Body mismatch") + assert.Equal(t, cliNote1Alg2.Body, "algorithms1", "cliNote1Alg2 Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookSCSS.Label, "scss", "cliBookSCSS Label mismatch") + assert.Equal(t, cliBookMath.Label, "math", "cliBookMath Label mismatch") + assert.Equal(t, cliBookBash.Label, "bash", "cliBookBash Label mismatch") + assert.Equal(t, cliBookAlg.Label, "algorithms", "cliBookAlg Label mismatch") + assert.Equal(t, cliBookAlg2.Label, "algorithms_2", "cliBookAlg2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliNote1SCSS.Deleted, false, "cliNote1SCSS Deleted mismatch") + assert.Equal(t, cliNote1Math.Deleted, false, "cliNote1Math Deleted mismatch") + assert.Equal(t, cliNote1Alg2.Deleted, false, "cliNote1Alg2 Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookSCSS.Deleted, false, "cliBookSCSS Deleted mismatch") + assert.Equal(t, cliBookMath.Deleted, false, "cliBookMath Deleted mismatch") + assert.Equal(t, cliBookBash.Deleted, false, "cliBookBash Deleted mismatch") + assert.Equal(t, cliBookAlg.Deleted, false, "cliBookAlg Deleted mismatch") + assert.Equal(t, cliBookAlg2.Deleted, false, "cliBookAlg2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS, apiNote1SCSS, apiNote2CSS, apiNote1Math, apiNote1Alg database.Note + var apiBookJS, apiBookCSS, apiBookSCSS, apiBookMath, apiBookBash, apiBookAlg, apiBookAlg2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote2UUID"]).First(&apiNote2CSS), "finding apiNote2CSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1SCSS), "finding apiNote1SCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1Math.UUID).First(&apiNote1Math), "finding apiNote1Math") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1Alg2.UUID).First(&apiNote1Alg), "finding apiNote1Alg") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["bashBookUUID"]).First(&apiBookBash), "finding apiBookBash") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["scssBookUUID"]).First(&apiBookSCSS), "finding apiBookSCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["algorithmsBookUUID"]).First(&apiBookAlg), "finding apiBookAlg") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookAlg2.UUID).First(&apiBookAlg2), "finding apiBookAlg2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookMath.UUID).First(&apiBookMath), "finding apiBookMath") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1SCSS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote2CSS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1Math.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1Alg.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBook1Alg usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + assert.NotEqual(t, apiBookSCSS.USN, 0, "apibookSCSS usn mismatch") + assert.NotEqual(t, apiBookMath.USN, 0, "apiBookMath usn mismatch") + assert.NotEqual(t, apiBookBash.USN, 0, "apiBookBash usn mismatch") + assert.NotEqual(t, apiBookAlg.USN, 0, "apiBookAlg usn mismatch") + assert.NotEqual(t, apiBookAlg2.USN, 0, "apiBookAlg2 usn mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js3", "apiNote2JS Body mismatch") + assert.Equal(t, apiNote1SCSS.Body, "css1", "apiNote1SCSS Body mismatch") + assert.Equal(t, apiNote2CSS.Body, "", "apiNote2CSS Body mismatch") + assert.Equal(t, apiNote1Math.Body, "math1-edited", "apiNote1Math Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookSCSS.Label, "scss", "apiBookSCSS Label mismatch") + assert.Equal(t, apiBookMath.Label, "math", "apiBookMath Label mismatch") + assert.Equal(t, apiBookBash.Label, "bash", "apiBookBash Label mismatch") + assert.Equal(t, apiBookAlg.Label, "algorithms", "apiBookAlg Label mismatch") + assert.Equal(t, apiBookAlg2.Label, "algorithms_2", "apiBookAlg2 Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote2JS.Deleted, false, "apiNote2JS Deleted mismatch") + assert.Equal(t, apiNote1SCSS.Deleted, false, "apiNote1SCSS Deleted mismatch") + assert.Equal(t, apiNote2CSS.Deleted, true, "apiNote2CSS Deleted mismatch") + assert.Equal(t, apiNote1Math.Deleted, false, "apiNote1Math Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, true, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookSCSS.Deleted, false, "apiBookSCSS Deleted mismatch") + assert.Equal(t, apiBookMath.Deleted, false, "apiBookMath Deleted mismatch") + assert.Equal(t, apiBookBash.Deleted, false, "apiBookBash Deleted mismatch") + assert.Equal(t, apiBookAlg.Deleted, false, "apiBookAlg Deleted mismatch") + assert.Equal(t, apiBookAlg2.Deleted, false, "apiBookAlg2 Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("three times", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + goBookUUID := apiCreateBook(t, env, user, "go", "adding a go book") + goNote1UUID := apiCreateNote(t, env, user, goBookUUID, "go1", "adding go note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "html", "-c", "html1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "goBookUUID": goBookUUID, + "goNote1UUID": goNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // test client + var cliNote1JS, cliNote1CSS, cliNote1Go, cliNote1HTML cliDatabase.Note + var cliBookJS, cliBookCSS, cliBookGo, cliBookHTML cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1Go", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "go1"), &cliNote1Go.UUID, &cliNote1Go.Body, &cliNote1Go.USN) + cliDatabase.MustScan(t, "finding cliNote1HTML", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "html1"), &cliNote1HTML.UUID, &cliNote1HTML.Body, &cliNote1HTML.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookGo", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "go"), &cliBookGo.UUID, &cliBookGo.Label, &cliBookGo.USN) + cliDatabase.MustScan(t, "finding cliBookHTML", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "html"), &cliBookHTML.UUID, &cliBookHTML.Label, &cliBookHTML.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliNote1Go.USN, 0, "cliNote1Go USN mismatch") + assert.NotEqual(t, cliNote1HTML.USN, 0, "cliNote1HTML USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookGo.USN, 0, "cliBookGo USN mismatch") + assert.NotEqual(t, cliBookHTML.USN, 0, "cliBookHTML USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1Go.Body, "go1", "cliNote1Go Body mismatch") + assert.Equal(t, cliNote1HTML.Body, "html1", "cliNote1HTML Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookGo.Label, "go", "cliBookGo Label mismatch") + assert.Equal(t, cliBookHTML.Label, "html", "cliBookHTML Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote1Go.Deleted, false, "cliNote1Go Deleted mismatch") + assert.Equal(t, cliNote1HTML.Deleted, false, "cliNote1HTML Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookGo.Deleted, false, "cliBookGo Deleted mismatch") + assert.Equal(t, cliBookHTML.Deleted, false, "cliBookHTML Deleted mismatch") + + // test server + var apiNote1JS, apiNote1CSS, apiNote1Go, apiNote1HTML database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["goNote1UUID"]).First(&apiNote1Go), "finding apiNote1Go") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding apiNote1CSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1HTML.UUID).First(&apiNote1HTML), "finding apiNote1HTML") + var apiBookJS, apiBookCSS, apiBookGo, apiBookHTML database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["goBookUUID"]).First(&apiBookGo), "finding apiBookGo") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS.UUID).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookHTML.UUID).First(&apiBookHTML), "finding apiBookHTML") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiNote1Go.USN, 0, "apiNote1Go USN mismatch") + assert.NotEqual(t, apiNote1HTML.USN, 0, "apiNote1HTM USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookGo.USN, 0, "apiBookGo USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookHTML.USN, 0, "apiBookHTML USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote1Go.Body, "go1", "apiNote1Go Body mismatch") + assert.Equal(t, apiNote1HTML.Body, "html1", "apiNote1HTM Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookGo.Label, "go", "apiBookGo Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookHTML.Label, "html", "apiBookHTML Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) +} + +func TestSync(t *testing.T) { + t.Run("client adds a book and a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + + return map[string]string{} + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test client + // assert on bodys and labels + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS.UUID).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS.UUID).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 5, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE label = ?", "js"), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client edits a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client edits a book by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test client + var cliBookJS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookJS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server adds a book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 1, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 1, + serverUserMaxUSN: 1, + }) + + // test server + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + // assert on bodys and labels + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE label = ?", "js"), &cliBookJS.Label, &cliBookJS.USN) + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server edits a book by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-new-label"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test server + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiBookJS.Label, "js-new-label", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + // assert on bodys and labels + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + assert.Equal(t, cliBookJS.Label, "js-new-label", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test server + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server adds a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server edits a note body", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited"), "editing js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server moves a note to another book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["cssBookUUID"]), &cliBookCSS.Label, &cliBookCSS.USN) + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server deletes the same book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + // 4. on cli + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 6, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server deletes the same note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + // 4. on cli + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + cliDB := env.DB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds a note with same body", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test client + var cliNote1JS, cliNote2JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? and uuid != ?", "js1", ids["jsNote1UUID"]), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js1", "cliNote2JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js1", "apiNote2JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds a book with same label", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test client + var cliNote1JS, cliNote1JS2 cliDatabase.Note + var cliBookJS, cliBookJS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS2", + cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? AND uuid !=?", "js1", ids["jsNote1UUID"]), &cliNote1JS2.UUID, &cliNote1JS2.Body, &cliNote1JS2.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookJS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS2.Body, "js1", "cliNote1JS2 Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookJS2.Label, "js_2", "cliBookJS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1JS2.Deleted, false, "cliNote1JS2 Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookJS2.Deleted, false, "cliBookJS2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote1JS2 database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS2.UUID).First(&apiNote1JS2), "finding apiNote1JS2") + var apiBookJS, apiBookJS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS2.UUID).First(&apiBookJS2), "finding apiBookJS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1JS2.USN, 0, "apiNote1JS2 USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS2.Body, "js1", "apiNote1JS2 Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js_2", "apiBookJS2 Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds two sets of books with same labels", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + cssNote1UUID := apiCreateNote(t, env, user, cssBookUUID, "css1", "adding css note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + "cssNote1UUID": cssNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // test client + var cliNote1JS, cliNote1JS2, cliNote1CSS, cliNote1CSS2 cliDatabase.Note + var cliBookJS, cliBookJS2, cliBookCSS, cliBookCSS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS2", + cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? AND uuid != ?", "js1", ids["jsNote1UUID"]), &cliNote1JS2.UUID, &cliNote1JS2.Body, &cliNote1JS2.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE uuid = ?", ids["cssNote1UUID"]), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS2", + cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ? AND uuid != ?", "css1", ids["cssNote1UUID"]), &cliNote1CSS2.UUID, &cliNote1CSS2.Body, &cliNote1CSS2.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookJS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS2.Body, "js1", "cliNote1JS2 Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1CSS2.Body, "css1", "cliNote1CSS2 Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookJS2.Label, "js_2", "cliBookJS2 Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookCSS2.Label, "css_2", "cliBookCSS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1JS2.Deleted, false, "cliNote1JS2 Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliNote1CSS2.Deleted, false, "cliNote1CSS2 Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookJS2.Deleted, false, "cliBookJS2 Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookCSS2.Deleted, false, "cliBookCSS2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote1JS2, apiNote1CSS, apiNote1CSS2 database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1JS2.UUID).First(&apiNote1JS2), "finding apiNote1JS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssNote1UUID"]).First(&apiNote1CSS), "finding apiNote1CSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS2.UUID).First(&apiNote1CSS2), "finding apiNote1CSS2") + var apiBookJS, apiBookJS2, apiBookCSS, apiBookCSS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS2.UUID).First(&apiBookJS2), "finding apiBookJS2") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS2.UUID).First(&apiBookCSS2), "finding apiBookCSS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1JS2.USN, 0, "apiNote1JS2 USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiNote1CSS2.USN, 0, "apiNote1CSS2 USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS2 USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookCSS2.USN, 0, "apiBookCSS2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS2.Body, "js1", "apiNote1JS2 Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS2 Body mismatch") + assert.Equal(t, apiNote1CSS2.Body, "css1", "apiNote1CSS2 Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js_2", "apiBookJS2 Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookCSS2.Label, "css_2", "apiBookCSS2 Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds notes to the same book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test client + var cliNote1JS, cliNote2JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js2", "cliNote2JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js2", "apiNote2JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server and client adds a book with the same label and notes in it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js2") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test client + var cliNote1JS, cliNote2JS cliDatabase.Note + var cliBookJS, cliBookJS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote2JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNote2JS.UUID, &cliNote2JS.Body, &cliNote2JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookJS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote2JS.Body, "js2", "cliNote2JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookJS2.Label, "js_2", "cliBookJS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote2JS.Deleted, false, "cliNote2JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookJS2.Deleted, false, "cliBookJS2 Deleted mismatch") + + // test server + var apiNote1JS, apiNote2JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote2JS.UUID).First(&apiNote2JS), "finding apiNote2JS") + var apiBookJS, apiBookJS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookJS2.UUID).First(&apiBookJS2), "finding apiBookJS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote2JS.USN, 0, "apiNote2JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookJS2.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote2JS.Body, "js2", "apiNote2JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookJS2.Label, "js_2", "apiBookJS2 USN mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server edits bodys of the same note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited-from-client") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited-from-server"), "editing js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + resolvedBody := "<<<<<<< Local\njs1-edited-from-client\n=======\njs1-edited-from-server\n>>>>>>> Server\n" + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, resolvedBody, "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, resolvedBody, "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("clients deletes a note and server edits its body", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited"), "editing js note 1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 3, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 3, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("clients deletes a note and server moves it to another book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding css book") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + var nid string + cliDatabase.MustScan(t, "getting id of note to remove", cliDB.QueryRow("SELECT rowid FROM notes WHERE uuid = ?", jsNote1UUID), &nid) + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveNote, cliBinaryName, "remove", "js", nid) + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + "cssBookUUID": cssBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["cssBookUUID"]), &cliBookCSS.Label, &cliBookCSS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, ids["cssNote1UUID"], "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a note and client edits it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + // 4. on cli + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, ids["jsBookUUID"], "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a book and client edits it by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteNote(t, env, user, jsNote1UUID, "deleting js note 1") + + // 4. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("server deletes a book and client edits a note in it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + cliDB := env.DB + + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiDeleteBook(t, env, user, jsBookUUID, "deleting js book") + + // 4. on cli + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-c", "js1-edited") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 6, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1-edited", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliNote1JS cliDatabase.Note + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1-edited", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, ids["jsBookUUID"], "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a book and server edits it by renaming it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-edited"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 1, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 5, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + + // test client + var cliBookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.Label, &cliBookJS.USN) + + // test usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client deletes a book and server edits a note in it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"content": "%s"}`, "js1-edited"), "editing js1 note") + + // 4. on cli + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.ConfirmRemoveBook, cliBinaryName, "remove", "js") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 0, + clientBookCount: 0, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 6, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["jsBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, true, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, true, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server edit a book by renaming it to a same name", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-edited"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test client + var cliBookJS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookJS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js-edited", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server edit a book by renaming it to different names", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", "-n", "js-edited-client") + + // 3. on server + apiPatchBook(t, env, user, jsBookUUID, fmt.Sprintf(`{"name": "%s"}`, "js-edited-server"), "editing js book") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + // In this case, server's change wins and overwrites that of client's + + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 4, + }) + + // test client + var cliBookJS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE uuid = ?", ids["jsBookUUID"]), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookJS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js-edited-server", "cliBookJS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js-edited-server", "apiBookJS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client moves a note", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "1", "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server each moves a note to a same book", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "1", "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 2, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 2, + serverUserMaxUSN: 5, + }) + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, ids["cssBookUUID"], "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + + // test client + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client and server each moves a note to different books", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + linuxBookUUID := apiCreateBook(t, env, user, "linux", "adding a linux book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + apiPatchNote(t, env, user, jsNote1UUID, fmt.Sprintf(`{"book_uuid": "%s"}`, cssBookUUID), "moving js note 1 to css book") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "1", "-b", "linux") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + "linuxBookUUID": linuxBookUUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + expectedNote1JSBody := `<<<<<<< Local +Moved to the book linux +======= +Moved to the book css +>>>>>>> Server + +js1` + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 4, + clientLastMaxUSN: 7, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 4, + serverUserMaxUSN: 7, + }) + + // test client + var cliBookJS, cliBookCSS, cliBookLinux, cliBookConflicts cliDatabase.Book + var cliNote1JS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookLinux", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "linux"), &cliBookLinux.UUID, &cliBookLinux.Label, &cliBookLinux.USN) + cliDatabase.MustScan(t, "finding cliBookConflicts", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "conflicts"), &cliBookConflicts.UUID, &cliBookConflicts.Label, &cliBookConflicts.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE uuid = ?", ids["jsNote1UUID"]), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookLinux.USN, 0, "cliBookLinux USN mismatch") + assert.NotEqual(t, cliBookConflicts.USN, 0, "cliBookConflicts USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, expectedNote1JSBody, "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookConflicts.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookLinux.Label, "linux", "cliBookLinux Label mismatch") + assert.Equal(t, cliBookConflicts.Label, "conflicts", "cliBookConflicts Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookLinux.Deleted, false, "cliBookLinux Deleted mismatch") + assert.Equal(t, cliBookConflicts.Deleted, false, "cliBookConflicts Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + assert.Equal(t, cliBookLinux.Dirty, false, "cliBookLinux Dirty mismatch") + assert.Equal(t, cliBookConflicts.Dirty, false, "cliBookConflicts Dirty mismatch") + + // test server + var apiNote1JS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + var apiBookJS, apiBookCSS, apiBookLinux, apiBookConflicts database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["linuxBookUUID"]).First(&apiBookLinux), "finding apiBookLinux") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookConflicts.UUID).First(&apiBookConflicts), "finding apiBookConflicts") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookConflicts.USN, 0, "apiBookConflicts USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, expectedNote1JSBody, "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, apiBookConflicts.UUID, "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookLinux.Label, "linux", "apiBookLinux Label mismatch") + assert.Equal(t, apiBookConflicts.Label, "conflicts", "apiBookConflicts Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookLinux.Deleted, false, "apiBookLinux Deleted mismatch") + assert.Equal(t, apiBookConflicts.Deleted, false, "apiBookConflicts Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client adds a new book and moves a note into it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + cliDB := env.DB + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", cliDB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", "js", nid, "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 5, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 5, + }) + + // test client + var cliBookJS, cliBookCSS cliDatabase.Book + var cliNote1JS, cliNote1CSS cliDatabase.Note + cliDatabase.MustScan(t, "finding cli book js", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cli book css", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.BookUUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, book_uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.BookUUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1CSS.BookUUID, cliBookCSS.UUID, "cliNote1CSS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliNote1CSS.Dirty, false, "cliNote1CSS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + + // test server + var apiNote1JS, apiNote1CSS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding apiNote1CSS") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS.UUID).First(&apiBookCSS), "finding apiBookCSS") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, apiBookCSS.UUID, "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote1CSS.BookUUID, apiBookCSS.UUID, "apiNote1CSS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) + + t.Run("client adds a duplicate book and moves a note into it", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + // 1. on server + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + // 2. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // 3. on server + cssBookUUID := apiCreateBook(t, env, user, "css", "adding a css book") + + // 3. on cli + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + var nid string + cliDatabase.MustScan(t, "getting id of note to edit", env.DB.QueryRow("SELECT rowid FROM notes WHERE body = ?", "js1"), &nid) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "edit", nid, "-b", "css") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "cssBookUUID": cssBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 3, + clientLastMaxUSN: 6, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 3, + serverUserMaxUSN: 6, + }) + + // test client + var cliBookJS, cliBookCSS, cliBookCSS2 cliDatabase.Book + var cliNote1JS, cliNote1CSS cliDatabase.Note + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS2", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.BookUUID, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, book_uuid, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.BookUUID, &cliNote1CSS.USN) + + // assert on usn + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + assert.NotEqual(t, cliBookCSS2.USN, 0, "cliBookCSS2 USN mismatch") + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1JS.BookUUID, cliBookCSS2.UUID, "cliNote1JS BookUUID mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliNote1CSS.BookUUID, cliBookCSS2.UUID, "cliNote1CSS BookUUID mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + assert.Equal(t, cliBookCSS2.Label, "css_2", "cliBookCSS2 Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + assert.Equal(t, cliBookCSS2.Deleted, false, "cliBookCSS2 Deleted mismatch") + // assert on dirty + assert.Equal(t, cliNote1JS.Dirty, false, "cliNote1JS Dirty mismatch") + assert.Equal(t, cliNote1CSS.Dirty, false, "cliNote1CSS Dirty mismatch") + assert.Equal(t, cliBookJS.Dirty, false, "cliBookJS Dirty mismatch") + assert.Equal(t, cliBookCSS.Dirty, false, "cliBookCSS Dirty mismatch") + assert.Equal(t, cliBookCSS2.Dirty, false, "cliBookCSS2 Dirty mismatch") + + // test server + var apiNote1JS, apiNote1CSS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding apiNote1JS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding apiNote1CSS") + var apiBookJS, apiBookCSS, apiBookCSS2 database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding apiBookJS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["cssBookUUID"]).First(&apiBookCSS), "finding apiBookCSS") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS2.UUID).First(&apiBookCSS2), "finding apiBookCSS2") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS USN mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS USN mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS USN mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS USN mismatch") + assert.NotEqual(t, apiBookCSS2.USN, 0, "apiBookCSS2 USN mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1JS.BookUUID, apiBookCSS2.UUID, "apiNote1JS BookUUID mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiNote1CSS.BookUUID, apiBookCSS2.UUID, "apiNote1CSS BookUUID mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + assert.Equal(t, apiBookCSS2.Label, "css_2", "apiBookCSS2 Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + assert.Equal(t, apiBookCSS2.Deleted, false, "apiBookCSS2 Deleted mismatch") + } + + testSyncCmd(t, false, setup, assert) + testSyncCmd(t, true, setup, assert) + }) +} + +func TestFullSync(t *testing.T) { + t.Run("consecutively with stepSync", func(t *testing.T) { + setup := func(t *testing.T, env testEnv, user database.User) map[string]string { + jsBookUUID := apiCreateBook(t, env, user, "js", "adding a js book") + jsNote1UUID := apiCreateNote(t, env, user, jsBookUUID, "js1", "adding js note 1") + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + return map[string]string{ + "jsBookUUID": jsBookUUID, + "jsNote1UUID": jsNote1UUID, + } + } + + assert := func(t *testing.T, env testEnv, user database.User, ids map[string]string) { + cliDB := env.DB + apiDB := env.ServerDB + + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // test client + var cliNote1JS, cliNote1CSS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body, &cliNote1JS.USN) + cliDatabase.MustScan(t, "finding cliNote1CSS", cliDB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body, &cliNote1CSS.USN) + cliDatabase.MustScan(t, "finding cliBookJS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding cliBookCSS", cliDB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + + // test usn + assert.NotEqual(t, cliNote1JS.USN, 0, "cliNote1JS USN mismatch") + assert.NotEqual(t, cliNote1CSS.USN, 0, "cliNote1CSS USN mismatch") + assert.NotEqual(t, cliBookJS.USN, 0, "cliBookJS USN mismatch") + assert.NotEqual(t, cliBookCSS.USN, 0, "cliBookCSS USN mismatch") + // assert on bodys and labels + assert.Equal(t, cliNote1JS.Body, "js1", "cliNote1JS Body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "cliNote1CSS Body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "cliBookJS Label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "cliBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, cliNote1JS.Deleted, false, "cliNote1JS Deleted mismatch") + assert.Equal(t, cliNote1CSS.Deleted, false, "cliNote1CSS Deleted mismatch") + assert.Equal(t, cliBookJS.Deleted, false, "cliBookJS Deleted mismatch") + assert.Equal(t, cliBookCSS.Deleted, false, "cliBookCSS Deleted mismatch") + + // test server + var apiNote1JS, apiNote1CSS database.Note + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsNote1UUID"]).First(&apiNote1JS), "finding api js note 1") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliNote1CSS.UUID).First(&apiNote1CSS), "finding api css note 1") + var apiBookJS, apiBookCSS database.Book + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, ids["jsBookUUID"]).First(&apiBookJS), "finding api js book") + apitest.MustExec(t, apiDB.Where("user_id = ? AND uuid = ?", user.ID, cliBookCSS.UUID).First(&apiBookCSS), "finding api css book") + + // assert on usn + assert.NotEqual(t, apiNote1JS.USN, 0, "apiNote1JS usn mismatch") + assert.NotEqual(t, apiNote1CSS.USN, 0, "apiNote1CSS usn mismatch") + assert.NotEqual(t, apiBookJS.USN, 0, "apiBookJS usn mismatch") + assert.NotEqual(t, apiBookCSS.USN, 0, "apiBookCSS usn mismatch") + // assert on bodys and labels + assert.Equal(t, apiNote1JS.Body, "js1", "apiNote1JS Body mismatch") + assert.Equal(t, apiNote1CSS.Body, "css1", "apiNote1CSS Body mismatch") + assert.Equal(t, apiBookJS.Label, "js", "apiBookJS Label mismatch") + assert.Equal(t, apiBookCSS.Label, "css", "apiBookCSS Label mismatch") + // assert on deleted + assert.Equal(t, apiNote1JS.Deleted, false, "apiNote1JS Deleted mismatch") + assert.Equal(t, apiNote1CSS.Deleted, false, "apiNote1CSS Deleted mismatch") + assert.Equal(t, apiBookJS.Deleted, false, "apiBookJS Deleted mismatch") + assert.Equal(t, apiBookCSS.Deleted, false, "apiBookCSS Deleted mismatch") + } + + t.Run("stepSync then fullSync", func(t *testing.T) { + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + ids := setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + assert(t, env, user, ids) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + assert(t, env, user, ids) + }) + + t.Run("fullSync then stepSync", func(t *testing.T) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + ids := setup(t, env, user) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + assert(t, env, user, ids) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + assert(t, env, user, ids) + }) + }) +} + +func TestSync_FreshClientConcurrent(t *testing.T) { + // Test the core issue: Fresh client (never synced, lastMaxUSN=0) syncing to a server + // that already has data uploaded by another client. + // + // Scenario: + // 1. Client A creates local notes (never synced, lastMaxUSN=0, lastSyncAt=0) + // 2. Client B uploads same book names to server first + // 3. Client A syncs + // + // Expected: Client A should pull server data first, detect duplicate book names, + // rename local books to avoid conflicts (js→js_2), then upload successfully. + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Client A: Create local data (never sync) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + + // Client B: Upload same book names to server via API + jsBookUUID := apiCreateBook(t, env, user, "js", "client B creating js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "client B creating css book") + apiCreateNote(t, env, user, jsBookUUID, "js2", "client B note") + apiCreateNote(t, env, user, cssBookUUID, "css2", "client B note") + + // Client A syncs - should handle the conflict gracefully + // Expected: pulls server data, renames local books to js_2/css_2, uploads successfully + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify: Should have 4 books and 4 notes on both client and server + // USN breakdown: 2 books + 2 notes from Client B (USN 1-4), then 2 books + 2 notes from Client A (USN 5-8) + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // Verify server has all 4 books with correct names + var svrBookJS, svrBookCSS, svrBookJS2, svrBookCSS2 database.Book + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js").First(&svrBookJS), "finding server book 'js'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css").First(&svrBookCSS), "finding server book 'css'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js_2").First(&svrBookJS2), "finding server book 'js_2'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css_2").First(&svrBookCSS2), "finding server book 'css_2'") + + assert.Equal(t, svrBookJS.Label, "js", "server should have book 'js' (Client B)") + assert.Equal(t, svrBookCSS.Label, "css", "server should have book 'css' (Client B)") + assert.Equal(t, svrBookJS2.Label, "js_2", "server should have book 'js_2' (Client A renamed)") + assert.Equal(t, svrBookCSS2.Label, "css_2", "server should have book 'css_2' (Client A renamed)") + + // Verify server has all 4 notes with correct content + var svrNoteJS1, svrNoteJS2, svrNoteCSS1, svrNoteCSS2 database.Note + apitest.MustExec(t, env.ServerDB.Where("body = ?", "js1").First(&svrNoteJS1), "finding server note 'js1'") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "js2").First(&svrNoteJS2), "finding server note 'js2'") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "css1").First(&svrNoteCSS1), "finding server note 'css1'") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "css2").First(&svrNoteCSS2), "finding server note 'css2'") + + assert.Equal(t, svrNoteJS1.BookUUID, svrBookJS2.UUID, "note 'js1' should belong to book 'js_2' (Client A)") + assert.Equal(t, svrNoteJS2.BookUUID, svrBookJS.UUID, "note 'js2' should belong to book 'js' (Client B)") + assert.Equal(t, svrNoteCSS1.BookUUID, svrBookCSS2.UUID, "note 'css1' should belong to book 'css_2' (Client A)") + assert.Equal(t, svrNoteCSS2.BookUUID, svrBookCSS.UUID, "note 'css2' should belong to book 'css' (Client B)") + + // Verify client has all 4 books + var cliBookJS, cliBookCSS, cliBookJS2, cliBookCSS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding client book 'js'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding client book 'css'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding client book 'js_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + cliDatabase.MustScan(t, "finding client book 'css_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + + // Verify client UUIDs match server + assert.Equal(t, cliBookJS.UUID, svrBookJS.UUID, "client 'js' UUID should match server") + assert.Equal(t, cliBookCSS.UUID, svrBookCSS.UUID, "client 'css' UUID should match server") + assert.Equal(t, cliBookJS2.UUID, svrBookJS2.UUID, "client 'js_2' UUID should match server") + assert.Equal(t, cliBookCSS2.UUID, svrBookCSS2.UUID, "client 'css_2' UUID should match server") + + // Verify all books have non-zero USN (synced successfully) + assert.NotEqual(t, cliBookJS.USN, 0, "client 'js' should have non-zero USN") + assert.NotEqual(t, cliBookCSS.USN, 0, "client 'css' should have non-zero USN") + assert.NotEqual(t, cliBookJS2.USN, 0, "client 'js_2' should have non-zero USN") + assert.NotEqual(t, cliBookCSS2.USN, 0, "client 'css_2' should have non-zero USN") + + // Verify client has all 4 notes + var cliNoteJS1, cliNoteJS2, cliNoteCSS1, cliNoteCSS2 cliDatabase.Note + cliDatabase.MustScan(t, "finding client note 'js1'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js1"), &cliNoteJS1.UUID, &cliNoteJS1.Body, &cliNoteJS1.USN) + cliDatabase.MustScan(t, "finding client note 'js2'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "js2"), &cliNoteJS2.UUID, &cliNoteJS2.Body, &cliNoteJS2.USN) + cliDatabase.MustScan(t, "finding client note 'css1'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css1"), &cliNoteCSS1.UUID, &cliNoteCSS1.Body, &cliNoteCSS1.USN) + cliDatabase.MustScan(t, "finding client note 'css2'", env.DB.QueryRow("SELECT uuid, body, usn FROM notes WHERE body = ?", "css2"), &cliNoteCSS2.UUID, &cliNoteCSS2.Body, &cliNoteCSS2.USN) + + // Verify client note UUIDs match server + assert.Equal(t, cliNoteJS1.UUID, svrNoteJS1.UUID, "client note 'js1' UUID should match server") + assert.Equal(t, cliNoteJS2.UUID, svrNoteJS2.UUID, "client note 'js2' UUID should match server") + assert.Equal(t, cliNoteCSS1.UUID, svrNoteCSS1.UUID, "client note 'css1' UUID should match server") + assert.Equal(t, cliNoteCSS2.UUID, svrNoteCSS2.UUID, "client note 'css2' UUID should match server") + + // Verify all notes have non-zero USN (synced successfully) + assert.NotEqual(t, cliNoteJS1.USN, 0, "client note 'js1' should have non-zero USN") + assert.NotEqual(t, cliNoteJS2.USN, 0, "client note 'js2' should have non-zero USN") + assert.NotEqual(t, cliNoteCSS1.USN, 0, "client note 'css1' should have non-zero USN") + assert.NotEqual(t, cliNoteCSS2.USN, 0, "client note 'css2' should have non-zero USN") +} + +// TestSync_ConvergeSameBookNames tests that two clients don't enter an infinite sync loop if they +// try to sync books with the same names. Books shouldn't get marked dirty when re-downloaded from server. +func TestSync_ConvergeSameBookNames(t *testing.T) { + env := setupTestEnv(t) + tmpDir := t.TempDir() + + // Setup two separate client databases + client1DB := fmt.Sprintf("%s/client1.db", tmpDir) + client2DB := fmt.Sprintf("%s/client2.db", tmpDir) + defer os.Remove(client1DB) + defer os.Remove(client2DB) + + // Set up sessions + user := setupUser(t, env) + db1 := testutils.MustOpenDatabase(t, client1DB) + db2 := testutils.MustOpenDatabase(t, client2DB) + defer db1.Close() + defer db2.Close() + + // Client 1: First sync to empty server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "add", "testbook", "-c", "client1 note1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "add", "anotherbook", "-c", "client1 note2") + login(t, db1, env.ServerDB, user) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "sync") + checkState(t, db1, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Client 2: Sync (downloads client 1's data, adds own notes) ===== + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "add", "testbook", "-c", "client2 note1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "add", "anotherbook", "-c", "client2 note2") + login(t, db2, env.ServerDB, user) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "sync") + // Verify state after client2 sync + checkState(t, db2, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // Client 1: Sync again. It downloads client2's changes (2 extra notes). + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "sync") + + // Verify MaxUSN did not increase (client1 should only download, not upload) + // Client1 still has: 2 original books + 4 notes (2 own + 2 from client2) + checkState(t, db1, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // Verify no infinite loop: alternate syncing + // Both clients should be able to sync without any changes (MaxUSN stays at 8) + for range 3 { + // Client 2 syncs + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DB, "sync") + + // Verify client2 state unchanged + checkState(t, db2, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + + // Client 1 syncs + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client1DB, "sync") + + // Verify client1 state unchanged + checkState(t, db1, user, env.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 2, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 2, + serverUserMaxUSN: 8, + }) + } +} diff --git a/pkg/e2e/sync/edge_cases_test.go b/pkg/e2e/sync/edge_cases_test.go new file mode 100644 index 00000000..f833cadc --- /dev/null +++ b/pkg/e2e/sync/edge_cases_test.go @@ -0,0 +1,163 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "io" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/google/uuid" + "github.com/pkg/errors" +) + +// TestSync_EmptyFragmentPreservesLastMaxUSN verifies that last_max_usn is not reset to 0 +// when sync receives an empty response from the server. +// +// Scenario: Client has orphaned note (references non-existent book). During sync: +// 1. Downloads data successfully (last_max_usn=3) +// 2. Upload fails (orphaned note -> 500 error, triggers retry stepSync) +// 3. Retry stepSync gets 0 fragments (already at latest USN) +// 4. last_max_usn should stay at 3, not reset to 0 +func TestSync_EmptyFragmentPreservesLastMaxUSN(t *testing.T) { + env := setupTestEnv(t) + user := setupUserAndLogin(t, env) + + // Create data on server (max_usn=3) + bookUUID := apiCreateBook(t, env, user, "javascript", "creating book via API") + apiCreateNote(t, env, user, bookUUID, "note1 content", "creating note1 via API") + apiCreateNote(t, env, user, bookUUID, "note2 content", "creating note2 via API") + + // Create orphaned note locally (will fail to upload) + orphanedNote := cliDatabase.Note{ + UUID: uuid.New().String(), + BookUUID: uuid.New().String(), // non-existent book + Body: "orphaned note content", + AddedOn: 1234567890, + EditedOn: 0, + USN: 0, + Deleted: false, + Dirty: true, + } + if err := orphanedNote.Insert(env.DB); err != nil { + t.Fatal(err) + } + + // Run sync (downloads data, upload fails, retry gets 0 fragments) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify last_max_usn is preserved at 3, not reset to 0 + var lastMaxUSN int + cliDatabase.MustScan(t, "finding system last_max_usn", + env.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), + &lastMaxUSN) + + assert.Equal(t, lastMaxUSN, 3, "last_max_usn should be 3 after syncing") +} + +// TestSync_ConcurrentInitialSync reproduces the issue where two clients with identical +// local data syncing simultaneously to an empty server results in 500 errors. +// +// This demonstrates the race condition: +// - Client1 starts sync to empty server, gets empty server state +// - Client2 syncs. +// - Client1 tries to create same books → 409 "duplicate" +// - Client1 tries to create notes with wrong UUIDs → 500 "record not found" +// - stepSync recovers by renaming local books with _2 suffix +func TestSync_ConcurrentInitialSync(t *testing.T) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "javascript", "-c", "js note from client1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 1, + clientBookCount: 1, + clientLastMaxUSN: 2, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 1, + serverBookCount: 1, + serverUserMaxUSN: 2, + }) + + // Step 2: Switch to new empty server to simulate concurrent initial sync scenario + switchToEmptyServer(t, &env) + user = setupUserAndLogin(t, env) + + // Set up client2 with separate database + client2DB, client2DBPath := cliDatabase.InitTestFileDB(t) + login(t, client2DB, env.ServerDB, user) + client2DB.Close() // Close so CLI can access the database + + // Step 3: Client1 syncs to empty server, but during sync Client2 uploads same data + // This simulates the race condition deterministically + raceCallback := func(stdout io.Reader, stdin io.WriteCloser) error { + // Wait for empty server prompt to ensure Client1 has called GetSyncState + clitest.MustWaitForPrompt(t, stdout, clitest.PromptEmptyServer) + + // Now Client2 creates the same book and note via CLI (creating the race condition) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DBPath, "add", "javascript", "-c", "js note from client2") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DBPath, "sync") + + // User confirms sync + if _, err := io.WriteString(stdin, "y\n"); err != nil { + return errors.Wrap(err, "confirming sync") + } + + return nil + } + + // Client1 continues sync - will hit 409 conflict, then 500 error, then recover + clitest.MustWaitDnoteCmd(t, env.CmdOpts, raceCallback, cliBinaryName, "sync") + + // After sync: + // - Server has 2 books: "javascript" (from client2) and "javascript_2" (from client1 renamed) + // - Server has 2 notes + // - Both clients should converge to the same state + expectedState := systemState{ + clientNoteCount: 2, // both notes + clientBookCount: 2, // javascript and javascript_2 + clientLastMaxUSN: 4, // 2 books + 2 notes + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + } + checkState(t, env.DB, user, env.ServerDB, expectedState) + + // Client2 syncs again to download client1's data + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "--dbPath", client2DBPath, "sync") + client2DB = clitest.MustOpenDatabase(t, client2DBPath) + defer client2DB.Close() + + // Client2 should have converged to the same state as client1 + checkState(t, client2DB, user, env.ServerDB, expectedState) + + // Verify no orphaned notes on server + var orphanedCount int + if err := env.ServerDB.Raw(` + SELECT COUNT(*) FROM notes + WHERE book_uuid NOT IN (SELECT uuid FROM books) + `).Scan(&orphanedCount).Error; err != nil { + t.Fatal(err) + } + assert.Equal(t, orphanedCount, 0, "server should have no orphaned notes after sync") +} diff --git a/pkg/e2e/sync/empty_server_test.go b/pkg/e2e/sync/empty_server_test.go new file mode 100644 index 00000000..31756ed2 --- /dev/null +++ b/pkg/e2e/sync/empty_server_test.go @@ -0,0 +1,728 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "database/sql" + "fmt" + "io" + "os" + "strconv" + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/server/database" + apitest "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" +) + +func TestSync_EmptyServer(t *testing.T) { + t.Run("sync to empty server after syncing to non-empty server", func(t *testing.T) { + // Test server data loss/wipe scenario (disaster recovery): + // Verify empty server detection works when the server loses all its data + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to a completely new empty server + switchToEmptyServer(t, &env) + + // Recreate user and session on new server + user = setupUserAndLogin(t, env) + + // Step 3: Sync again - should detect empty server and prompt user + // User confirms with "y" + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.UserConfirmEmptyServerSync, cliBinaryName, "sync") + + // Step 4: Verify data was uploaded to the empty server + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Verify the content is correct on both client and server + var cliNote1JS, cliNote1CSS cliDatabase.Note + var cliBookJS, cliBookCSS cliDatabase.Book + cliDatabase.MustScan(t, "finding cliNote1JS", env.DB.QueryRow("SELECT uuid, body FROM notes WHERE body = ?", "js1"), &cliNote1JS.UUID, &cliNote1JS.Body) + cliDatabase.MustScan(t, "finding cliNote1CSS", env.DB.QueryRow("SELECT uuid, body FROM notes WHERE body = ?", "css1"), &cliNote1CSS.UUID, &cliNote1CSS.Body) + cliDatabase.MustScan(t, "finding cliBookJS", env.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label) + cliDatabase.MustScan(t, "finding cliBookCSS", env.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label) + + assert.Equal(t, cliNote1JS.Body, "js1", "js note body mismatch") + assert.Equal(t, cliNote1CSS.Body, "css1", "css note body mismatch") + assert.Equal(t, cliBookJS.Label, "js", "js book label mismatch") + assert.Equal(t, cliBookCSS.Label, "css", "css book label mismatch") + + // Verify on server side + var serverNoteJS, serverNoteCSS database.Note + var serverBookJS, serverBookCSS database.Book + apitest.MustExec(t, env.ServerDB.Where("body = ?", "js1").First(&serverNoteJS), "finding server note js1") + apitest.MustExec(t, env.ServerDB.Where("body = ?", "css1").First(&serverNoteCSS), "finding server note css1") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js").First(&serverBookJS), "finding server book js") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css").First(&serverBookCSS), "finding server book css") + + assert.Equal(t, serverNoteJS.Body, "js1", "server js note body mismatch") + assert.Equal(t, serverNoteCSS.Body, "css1", "server css note body mismatch") + assert.Equal(t, serverBookJS.Label, "js", "server js book label mismatch") + assert.Equal(t, serverBookCSS.Label, "css", "server css book label mismatch") + }) + + t.Run("user cancels empty server prompt", func(t *testing.T) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify initial sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to empty server + switchToEmptyServer(t, &env) + user = setupUserAndLogin(t, env) + + // Step 3: Sync again but user cancels with "n" + output, err := clitest.WaitDnoteCmd(t, env.CmdOpts, clitest.UserCancelEmptyServerSync, cliBinaryName, "sync") + if err == nil { + t.Fatal("Expected sync to fail when user cancels, but it succeeded") + } + + // Verify the prompt appeared + if !strings.Contains(output, clitest.PromptEmptyServer) { + t.Fatalf("Expected empty server warning in output, got: %s", output) + } + + // Step 4: Verify local state unchanged (transaction rolled back) + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 0, + serverBookCount: 0, + serverUserMaxUSN: 0, + }) + + // Verify items still have original USN and dirty=false + var book cliDatabase.Book + var note cliDatabase.Note + cliDatabase.MustScan(t, "checking book state", env.DB.QueryRow("SELECT usn, dirty FROM books WHERE label = ?", "js"), &book.USN, &book.Dirty) + cliDatabase.MustScan(t, "checking note state", env.DB.QueryRow("SELECT usn, dirty FROM notes WHERE body = ?", "js1"), ¬e.USN, ¬e.Dirty) + + assert.NotEqual(t, book.USN, 0, "book USN should not be reset") + assert.NotEqual(t, note.USN, 0, "note USN should not be reset") + assert.Equal(t, book.Dirty, false, "book should not be marked dirty") + assert.Equal(t, note.Dirty, false, "note should not be marked dirty") + }) + + t.Run("all local data is marked deleted - should not upload", func(t *testing.T) { + // Test edge case: Server MaxUSN=0, local MaxUSN>0, but all items are deleted=true + // Should NOT prompt because there's nothing to upload + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to server + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify initial sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Delete all local notes and books (mark as deleted) + cliDatabase.MustExec(t, "marking all books deleted", env.DB, "UPDATE books SET deleted = 1") + cliDatabase.MustExec(t, "marking all notes deleted", env.DB, "UPDATE notes SET deleted = 1") + + // Step 3: Switch to empty server + switchToEmptyServer(t, &env) + user = setupUserAndLogin(t, env) + + // Step 4: Sync - should NOT prompt because bookCount=0 and noteCount=0 (counting only deleted=0) + // This should complete without user interaction + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify no data was uploaded (server still empty, but client still has deleted items) + // Check server is empty + var serverNoteCount, serverBookCount int64 + apitest.MustExec(t, env.ServerDB.Model(&database.Note{}).Count(&serverNoteCount), "counting server notes") + apitest.MustExec(t, env.ServerDB.Model(&database.Book{}).Count(&serverBookCount), "counting server books") + assert.Equal(t, serverNoteCount, int64(0), "server should have no notes") + assert.Equal(t, serverBookCount, int64(0), "server should have no books") + + // Check client still has the deleted items locally + var clientNoteCount, clientBookCount int + cliDatabase.MustScan(t, "counting client notes", env.DB.QueryRow("SELECT count(*) FROM notes WHERE deleted = 1"), &clientNoteCount) + cliDatabase.MustScan(t, "counting client books", env.DB.QueryRow("SELECT count(*) FROM books WHERE deleted = 1"), &clientBookCount) + assert.Equal(t, clientNoteCount, 2, "client should still have 2 deleted notes") + assert.Equal(t, clientBookCount, 2, "client should still have 2 deleted books") + + // Verify lastMaxUSN was reset to 0 + var lastMaxUSN int + cliDatabase.MustScan(t, "getting lastMaxUSN", env.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &lastMaxUSN) + assert.Equal(t, lastMaxUSN, 0, "lastMaxUSN should be reset to 0") + }) + + t.Run("race condition - other client uploads first", func(t *testing.T) { + // This test exercises a race condition that can occur during sync: + // While Client A is waiting for user input, Client B uploads data to the server. + // + // The empty server scenario is the natural place to test this because + // an empty server detection triggers a prompt, at which point the test + // can make client B upload data. We trigger the race condition deterministically. + // + // Test flow: + // - Client A detects empty server and prompts user + // - While waiting for confirmation, Client B uploads the same data via API + // - Client A continues and handles the 409 conflict gracefully by: + // 1. Detecting the 409 error when trying to CREATE books that already exist + // 2. Running stepSync to pull the server's books (js, css) + // 3. mergeBook renames local conflicts (js→js_2, css→css_2) + // 4. Retrying sendChanges to upload the renamed books + // - Result: Both clients' data is preserved (4 books total) + + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + + // Step 1: Create local data and sync to establish lastMaxUSN > 0 + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + + // Verify initial sync succeeded + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to new empty server to simulate switching to empty server + switchToEmptyServer(t, &env) + + // Create user on new server and login + user = setupUserAndLogin(t, env) + + // Step 3: Trigger sync which will detect empty server and prompt user + // Inside the callback (before confirming), we simulate Client B uploading via API. + // We wait for the empty server prompt to ensure Client B uploads AFTER + // GetSyncState but BEFORE the sync decision, creating the race condition deterministically + raceCallback := func(stdout io.Reader, stdin io.WriteCloser) error { + // First, wait for the prompt to ensure Client A has obtained the sync state from the server. + clitest.MustWaitForPrompt(t, stdout, clitest.PromptEmptyServer) + + // Now Client B uploads the same data via API (after Client A got the sync state from the server + // but before its sync decision) + // This creates the race condition: Client A thinks server is empty, but Client B uploads data + jsBookUUID := apiCreateBook(t, env, user, "js", "client B creating js book") + cssBookUUID := apiCreateBook(t, env, user, "css", "client B creating css book") + apiCreateNote(t, env, user, jsBookUUID, "js1", "client B creating js note") + apiCreateNote(t, env, user, cssBookUUID, "css1", "client B creating css note") + + // Now user confirms + if _, err := io.WriteString(stdin, "y\n"); err != nil { + return errors.Wrap(err, "confirming sync") + } + + return nil + } + + // Step 4: Client A runs sync with race condition + // The 409 conflict is automatically handled: + // - When 409 is detected, isBehind flag is set + // - stepSync pulls Client B's data + // - mergeBook renames Client A's books to js_2, css_2 + // - Renamed books are uploaded + // - Both clients' data is preserved. + clitest.MustWaitDnoteCmd(t, env.CmdOpts, raceCallback, cliBinaryName, "sync") + + // Verify final state - both clients' data preserved + checkState(t, env.DB, user, env.ServerDB, systemState{ + clientNoteCount: 4, // Both clients' notes + clientBookCount: 4, // js, css, js_2, css_2 + clientLastMaxUSN: 8, // 4 from Client B + 4 from Client A's renamed books/notes + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // Verify server has both clients' books + var svrBookJS, svrBookCSS, svrBookJS2, svrBookCSS2 database.Book + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js").First(&svrBookJS), "finding server book 'js'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css").First(&svrBookCSS), "finding server book 'css'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "js_2").First(&svrBookJS2), "finding server book 'js_2'") + apitest.MustExec(t, env.ServerDB.Where("label = ?", "css_2").First(&svrBookCSS2), "finding server book 'css_2'") + + assert.Equal(t, svrBookJS.Label, "js", "server should have book 'js' (Client B)") + assert.Equal(t, svrBookCSS.Label, "css", "server should have book 'css' (Client B)") + assert.Equal(t, svrBookJS2.Label, "js_2", "server should have book 'js_2' (Client A renamed)") + assert.Equal(t, svrBookCSS2.Label, "css_2", "server should have book 'css_2' (Client A renamed)") + + // Verify client has all books + var cliBookJS, cliBookCSS, cliBookJS2, cliBookCSS2 cliDatabase.Book + cliDatabase.MustScan(t, "finding client book 'js'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js"), &cliBookJS.UUID, &cliBookJS.Label, &cliBookJS.USN) + cliDatabase.MustScan(t, "finding client book 'css'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css"), &cliBookCSS.UUID, &cliBookCSS.Label, &cliBookCSS.USN) + cliDatabase.MustScan(t, "finding client book 'js_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "js_2"), &cliBookJS2.UUID, &cliBookJS2.Label, &cliBookJS2.USN) + cliDatabase.MustScan(t, "finding client book 'css_2'", env.DB.QueryRow("SELECT uuid, label, usn FROM books WHERE label = ?", "css_2"), &cliBookCSS2.UUID, &cliBookCSS2.Label, &cliBookCSS2.USN) + + // Verify client UUIDs match server + assert.Equal(t, cliBookJS.UUID, svrBookJS.UUID, "client 'js' UUID should match server") + assert.Equal(t, cliBookCSS.UUID, svrBookCSS.UUID, "client 'css' UUID should match server") + assert.Equal(t, cliBookJS2.UUID, svrBookJS2.UUID, "client 'js_2' UUID should match server") + assert.Equal(t, cliBookCSS2.UUID, svrBookCSS2.UUID, "client 'css_2' UUID should match server") + + // Verify all items have non-zero USN (synced successfully) + assert.NotEqual(t, cliBookJS.USN, 0, "client 'js' should have non-zero USN") + assert.NotEqual(t, cliBookCSS.USN, 0, "client 'css' should have non-zero USN") + assert.NotEqual(t, cliBookJS2.USN, 0, "client 'js_2' should have non-zero USN") + assert.NotEqual(t, cliBookCSS2.USN, 0, "client 'css_2' should have non-zero USN") + }) + + t.Run("sync to server A, then B, then back to A, then back to B", func(t *testing.T) { + // Test switching between two actual servers to verify: + // 1. Empty server detection works when switching to empty server + // 2. No false detection when switching back to non-empty servers + // 3. Both servers maintain independent state across multiple switches + + env := setupTestEnv(t) + + // Create Server A with its own database + serverA, serverDBA, err := setupTestServer(t, serverTime) + if err != nil { + t.Fatal(errors.Wrap(err, "setting up server A")) + } + defer serverA.Close() + + // Create Server B with its own database + serverB, serverDBB, err := setupTestServer(t, serverTime) + if err != nil { + t.Fatal(errors.Wrap(err, "setting up server B")) + } + defer serverB.Close() + + // Step 1: Set up user on Server A and sync + apiEndpointA := fmt.Sprintf("%s/api", serverA.URL) + + userA := apitest.SetupUserData(serverDBA, "alice@example.com", "pass1234") + sessionA := apitest.SetupSession(serverDBA, userA) + cliDatabase.MustExec(t, "inserting session_key", env.DB, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, sessionA.Key) + cliDatabase.MustExec(t, "inserting session_key_expiry", env.DB, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, sessionA.ExpiresAt.Unix()) + + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "--apiEndpoint", apiEndpointA) + + // Verify sync to Server A succeeded + checkState(t, env.DB, userA, serverDBA, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 2: Switch to Server B (empty) and sync + apiEndpointB := fmt.Sprintf("%s/api", serverB.URL) + + // Set up user on Server B + userB := apitest.SetupUserData(serverDBB, "alice@example.com", "pass1234") + sessionB := apitest.SetupSession(serverDBB, userB) + cliDatabase.MustExec(t, "updating session_key for B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.Key, consts.SystemSessionKey) + cliDatabase.MustExec(t, "updating session_key_expiry for B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.ExpiresAt.Unix(), consts.SystemSessionKeyExpiry) + + // Should detect empty server and prompt + clitest.MustWaitDnoteCmd(t, env.CmdOpts, clitest.UserConfirmEmptyServerSync, cliBinaryName, "sync", "--apiEndpoint", apiEndpointB) + + // Verify Server B now has data + checkState(t, env.DB, userB, serverDBB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 3: Switch back to Server A and sync + cliDatabase.MustExec(t, "updating session_key back to A", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionA.Key, consts.SystemSessionKey) + cliDatabase.MustExec(t, "updating session_key_expiry back to A", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionA.ExpiresAt.Unix(), consts.SystemSessionKeyExpiry) + + // Should NOT trigger empty server detection (Server A has MaxUSN > 0) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "--apiEndpoint", apiEndpointA) + + // Verify Server A still has its data + checkState(t, env.DB, userA, serverDBA, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + + // Step 4: Switch back to Server B and sync again + cliDatabase.MustExec(t, "updating session_key back to B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.Key, consts.SystemSessionKey) + cliDatabase.MustExec(t, "updating session_key_expiry back to B", env.DB, "UPDATE system SET value = ? WHERE key = ?", sessionB.ExpiresAt.Unix(), consts.SystemSessionKeyExpiry) + + // Should NOT trigger empty server detection (Server B now has MaxUSN > 0 from Step 2) + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "--apiEndpoint", apiEndpointB) + + // Verify both servers maintain independent state + checkState(t, env.DB, userB, serverDBB, systemState{ + clientNoteCount: 2, + clientBookCount: 2, + clientLastMaxUSN: 4, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 2, + serverBookCount: 2, + serverUserMaxUSN: 4, + }) + }) + + t.Run("two clients with identical copied database sync to empty server", func(t *testing.T) { + // Suppose we have two clients and server becomes empty (migration). + // After the first client sync to empty server, the second client should trigger full sync. + // Without the full sync, client2 will do step sync asking for changes after its stale USN, + // get nothing from server, and potentially orphan notes during full sync. + + // Step 1: Create client1 with data and sync to ORIGINAL server + env1 := setupTestEnv(t) + user := setupUserAndLogin(t, env1) + + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "js", "-c", "js1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "css", "-c", "css1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + // Add more data to create a higher USN + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "go", "-c", "go1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "rust", "-c", "rust1") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + // Verify initial sync succeeded (now with 4 notes, 4 books, USN=8) + checkState(t, env1.DB, user, env1.ServerDB, systemState{ + clientNoteCount: 4, + clientBookCount: 4, + clientLastMaxUSN: 8, + clientLastSyncAt: serverTime.Unix(), + serverNoteCount: 4, + serverBookCount: 4, + serverUserMaxUSN: 8, + }) + + // Step 2: Create client2 by copying client1's database (simulating same DB on two devices) + env2 := setupTestEnv(t) + // Copy the database file from client1 to client2 + dbPath1 := env1.DB.Filepath + dbPath2 := env2.DB.Filepath + // Close both DBs before copying + env1.DB.Close() + env2.DB.Close() + + // Copy the database file + input, err := os.ReadFile(dbPath1) + if err != nil { + t.Fatal(errors.Wrap(err, "reading client1 database")) + } + if err := os.WriteFile(dbPath2, input, 0644); err != nil { + t.Fatal(errors.Wrap(err, "writing client2 database")) + } + + // Reopen databases + env1.DB, err = cliDatabase.Open(dbPath1) + if err != nil { + t.Fatal(errors.Wrap(err, "reopening client1 database")) + } + env2.DB, err = cliDatabase.Open(dbPath2) + if err != nil { + t.Fatal(errors.Wrap(err, "reopening client2 database")) + } + + // Verify client2 has identical data and USN=8 (stale) - same as client1 + // Note: at this point there's no server to compare against, we just check counts + var client2MaxUSN, client2NoteCount, client2BookCount int + cliDatabase.MustScan(t, "getting client2 maxUSN", + env2.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), + &client2MaxUSN) + cliDatabase.MustScan(t, "counting client2 notes", + env2.DB.QueryRow("SELECT count(*) FROM notes WHERE deleted = 0"), + &client2NoteCount) + cliDatabase.MustScan(t, "counting client2 books", + env2.DB.QueryRow("SELECT count(*) FROM books WHERE deleted = 0"), + &client2BookCount) + + assert.Equal(t, client2MaxUSN, 8, "client2 should have same maxUSN=8 as client1") + assert.Equal(t, client2NoteCount, 4, "client2 should have 4 notes") + assert.Equal(t, client2BookCount, 4, "client2 should have 4 books") + + // Step 3: Switch client1 to new empty server + switchToEmptyServer(t, &env1) + + // Point client2 to the same new server + env2.Server = env1.Server + env2.ServerDB = env1.ServerDB + + // Update client2's API endpoint config to point to env1's server + apiEndpoint := fmt.Sprintf("%s/api", env1.Server.URL) + updateConfigAPIEndpoint(t, env2.TmpDir, apiEndpoint) + + // Create same user on new server + user = setupUserAndLogin(t, env1) + + // Setup session for client2 (same user, same server) + login(t, env2.DB, env2.ServerDB, user) + + // Step 4: Client1 syncs ONLY FIRST 2 BOOKS to empty server (simulates partial upload) + // This creates the stale USN scenario: client2 has maxUSN=8, but server will only have maxUSN=4 + clitest.MustWaitDnoteCmd(t, env1.CmdOpts, clitest.UserConfirmEmptyServerSync, + cliBinaryName, "sync") + + // Delete the last 2 books from client1 to prevent them being on server + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "remove", "go", "-y") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "remove", "rust", "-y") + + // Sync deletions to server + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + + // Verify server has 2 active books/notes (go/rust deleted) + var serverNoteCount, serverBookCount int64 + apitest.MustExec(t, env1.ServerDB.Model(&database.Note{}).Where("deleted = ?", false).Count(&serverNoteCount), "counting active server notes") + apitest.MustExec(t, env1.ServerDB.Model(&database.Book{}).Where("deleted = ?", false).Count(&serverBookCount), "counting active server books") + assert.Equal(t, int(serverNoteCount), 2, "server should have 2 active notes (go/rust deleted)") + assert.Equal(t, int(serverBookCount), 2, "server should have 2 active books (go/rust deleted)") + + // Step 5: Client2 syncs + // CRITICAL: Client2 has lastMaxUSN=8 (from copied DB), but server's max_usn is now ~8 but only has 2 books + // Client2 will ask for changes after USN=8, get nothing, then try to upload its 4 books + // This should trigger the orphaned notes scenario or require full sync + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "sync") + + // Step 6: Verify client2 has all data and NO orphaned notes + var orphanedCount int + cliDatabase.MustScan(t, "checking for orphaned notes", + env2.DB.QueryRow(` + SELECT COUNT(*) FROM notes + WHERE deleted = 0 + AND book_uuid NOT IN (SELECT uuid FROM books WHERE deleted = 0) + `), &orphanedCount) + + assert.Equal(t, orphanedCount, 0, "client2 should have no orphaned notes") + + // Verify client2 converged with server state + // Note: checkState counts ALL records (including deleted ones) + // During full sync, cleanLocalBooks/cleanLocalNotes DELETE local records not on server + // So client2 ends up with only the 2 active books/notes + // Server has 4 total (2 active + 2 deleted) + var client2LastMaxUSN, client2LastSyncAt int + var serverUserMaxUSN int + cliDatabase.MustScan(t, "getting client2 lastMaxUSN", + env2.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), + &client2LastMaxUSN) + var lastSyncAtStr string + cliDatabase.MustScan(t, "getting client2 lastSyncAt", + env2.DB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), + &lastSyncAtStr) + lastSyncAtInt, _ := strconv.ParseInt(lastSyncAtStr, 10, 64) + client2LastSyncAt = int(lastSyncAtInt) + + apitest.MustExec(t, env2.ServerDB.Table("users").Select("max_usn").Where("id = ?", user.ID).Scan(&serverUserMaxUSN), "getting server user max_usn") + + checkState(t, env2.DB, user, env2.ServerDB, systemState{ + clientNoteCount: 2, // Only active notes (deleted ones removed by cleanLocalNotes) + clientBookCount: 2, // Only active books (deleted ones removed by cleanLocalBooks) + clientLastMaxUSN: client2LastMaxUSN, + clientLastSyncAt: int64(client2LastSyncAt), + serverNoteCount: 4, // 2 active + 2 deleted + serverBookCount: 4, // 2 active + 2 deleted + serverUserMaxUSN: serverUserMaxUSN, + }) + + // Verify both clients have the expected books (css, js only - go/rust deleted) + var client1BookCSS, client1BookJS, client2BookCSS, client2BookJS cliDatabase.Book + cliDatabase.MustScan(t, "finding client1 book 'css'", + env1.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "css"), + &client1BookCSS.UUID, &client1BookCSS.Label) + cliDatabase.MustScan(t, "finding client1 book 'js'", + env1.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "js"), + &client1BookJS.UUID, &client1BookJS.Label) + cliDatabase.MustScan(t, "finding client2 book 'css'", + env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "css"), + &client2BookCSS.UUID, &client2BookCSS.Label) + cliDatabase.MustScan(t, "finding client2 book 'js'", + env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "js"), + &client2BookJS.UUID, &client2BookJS.Label) + + assert.Equal(t, client1BookCSS.Label, "css", "client1 should have css book") + assert.Equal(t, client1BookJS.Label, "js", "client1 should have js book") + assert.Equal(t, client2BookCSS.Label, "css", "client2 should have css book") + assert.Equal(t, client2BookJS.Label, "js", "client2 should have js book") + + // Verify go and rust books are deleted/absent on both clients + var client2BookGo, client2BookRust cliDatabase.Book + errGo := env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "go").Scan(&client2BookGo.UUID, &client2BookGo.Label) + assert.Equal(t, errGo, sql.ErrNoRows, "client2 should not have non-deleted 'go' book") + errRust := env2.DB.QueryRow("SELECT uuid, label FROM books WHERE label = ? AND deleted = 0", "rust").Scan(&client2BookRust.UUID, &client2BookRust.Label) + assert.Equal(t, errRust, sql.ErrNoRows, "client2 should not have non-deleted 'rust' book") + }) + + t.Run("client with local data syncs after another client uploads to empty server - should not orphan notes", func(t *testing.T) { + // This test reproduces the scenario where: + // 1. Client1 has local data and syncs to original server + // 2. Client2 has DIFFERENT local data and syncs to SAME original server + // 3. Both clients switch to NEW empty server + // 4. Client1 uploads to the new empty server (sets FullSyncBefore) + // 5. Client2 syncs - should trigger full sync AND upload its local data + // WITHOUT orphaning notes due to cleanLocalBooks deleting them first + + // Step 1: Create client1 with local data on original server + env1 := setupTestEnv(t) + user := setupUserAndLogin(t, env1) + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "add", "client1-book", "-c", "client1-note") + clitest.RunDnoteCmd(t, env1.CmdOpts, cliBinaryName, "sync") + + // Step 2: Create client2 with DIFFERENT local data on SAME original server + env2 := setupTestEnv(t) + // Point env2 to env1's server (the original server) + env2.Server = env1.Server + env2.ServerDB = env1.ServerDB + apiEndpoint := fmt.Sprintf("%s/api", env1.Server.URL) + updateConfigAPIEndpoint(t, env2.TmpDir, apiEndpoint) + + // Login client2 to the same server + login(t, env2.DB, env2.ServerDB, user) + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "add", "client2-book", "-c", "client2-note") + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "sync") + + // Step 3: Both clients switch to NEW empty server + switchToEmptyServer(t, &env1) + env2.Server = env1.Server + env2.ServerDB = env1.ServerDB + apiEndpoint = fmt.Sprintf("%s/api", env1.Server.URL) + updateConfigAPIEndpoint(t, env2.TmpDir, apiEndpoint) + + // Create same user on new server + user = setupUserAndLogin(t, env1) + login(t, env2.DB, env2.ServerDB, user) + + // Step 4: Client1 uploads to empty server + clitest.MustWaitDnoteCmd(t, env1.CmdOpts, clitest.UserConfirmEmptyServerSync, cliBinaryName, "sync") + + // Verify server has client1's data and FullSyncBefore is set + var serverUser database.User + apitest.MustExec(t, env1.ServerDB.Where("id = ?", user.ID).First(&serverUser), "getting server user state") + assert.Equal(t, serverUser.MaxUSN > 0, true, "server should have data after client1 upload") + assert.Equal(t, serverUser.FullSyncBefore > 0, true, "server should have FullSyncBefore set") + + // Step 5: Client2 syncs - should trigger full sync due to FullSyncBefore + // CRITICAL: Client2 has local data (client2-book, client2-note) that should be uploaded + // Without the fix, cleanLocalBooks will delete client2-book before upload, orphaning client2-note + clitest.RunDnoteCmd(t, env2.CmdOpts, cliBinaryName, "sync") + + // Step 6: Verify NO orphaned notes on client2 + var orphanedCount int + cliDatabase.MustScan(t, "checking for orphaned notes on client2", + env2.DB.QueryRow(` + SELECT COUNT(*) FROM notes + WHERE deleted = 0 + AND book_uuid NOT IN (SELECT uuid FROM books WHERE deleted = 0) + `), &orphanedCount) + + assert.Equal(t, orphanedCount, 0, "client2 should have no orphaned notes after sync") + + // Step 7: Verify client2's data was uploaded to server + var client2BookOnServer database.Book + err := env2.ServerDB.Where("label = ? AND deleted = ?", "client2-book", false).First(&client2BookOnServer).Error + assert.Equal(t, err, nil, "client2-book should exist on server") + + var client2NoteOnServer database.Note + err = env2.ServerDB.Where("body = ? AND deleted = ?", "client2-note", false).First(&client2NoteOnServer).Error + assert.Equal(t, err, nil, "client2-note should exist on server") + + // Step 8: Verify server has data from BOTH clients + // Note: Both clients had synced to original server, so they each have 2 books + 2 notes locally. + // When switching to new empty server, client1 uploads 2 books + 2 notes (USN 1-4). + // Client2 then does full sync, downloads client1's uploads, marks its local data as dirty, + // and uploads its version of the same 2 books + 2 notes with potentially different UUIDs. + // The exact count depends on UUID conflict resolution, but we verify both original books exist. + var serverBookCount, serverNoteCount int64 + apitest.MustExec(t, env2.ServerDB.Model(&database.Book{}).Where("deleted = ?", false).Count(&serverBookCount), "counting active server books") + apitest.MustExec(t, env2.ServerDB.Model(&database.Note{}).Where("deleted = ?", false).Count(&serverNoteCount), "counting active server notes") + + // The main assertion: both original client books should exist + var client1BookExists, client2BookExists bool + err = env2.ServerDB.Model(&database.Book{}).Where("label = ? AND deleted = ?", "client1-book", false).First(&database.Book{}).Error + client1BookExists = (err == nil) + err = env2.ServerDB.Model(&database.Book{}).Where("label = ? AND deleted = ?", "client2-book", false).First(&database.Book{}).Error + client2BookExists = (err == nil) + + assert.Equal(t, client1BookExists, true, "server should have client1-book") + assert.Equal(t, client2BookExists, true, "server should have client2-book") + assert.Equal(t, serverBookCount >= 2, true, "server should have at least 2 books") + assert.Equal(t, serverNoteCount >= 2, true, "server should have at least 2 notes") + }) +} diff --git a/pkg/e2e/sync/main_test.go b/pkg/e2e/sync/main_test.go new file mode 100644 index 00000000..188e3a99 --- /dev/null +++ b/pkg/e2e/sync/main_test.go @@ -0,0 +1,54 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "bytes" + "fmt" + "log" + "os" + "os/exec" + "testing" + "time" + + "github.com/pkg/errors" +) + +var cliBinaryName string +var serverTime = time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC) + +var testDir = "./tmp/" + +func init() { + cliBinaryName = fmt.Sprintf("%s/test-cli", testDir) +} + +func TestMain(m *testing.M) { + // Build CLI binary without hardcoded API endpoint + // Each test will create its own server and config file + cmd := exec.Command("go", "build", "--tags", "fts5", "-o", cliBinaryName, "github.com/dnote/dnote/pkg/cli") + + var stderr bytes.Buffer + cmd.Stderr = &stderr + + if err := cmd.Run(); err != nil { + log.Print(errors.Wrap(err, "building a CLI binary").Error()) + log.Print(stderr.String()) + os.Exit(1) + } + + os.Exit(m.Run()) +} diff --git a/pkg/e2e/sync/testutils.go b/pkg/e2e/sync/testutils.go new file mode 100644 index 00000000..a5dfdf87 --- /dev/null +++ b/pkg/e2e/sync/testutils.go @@ -0,0 +1,295 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package sync + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/cli/consts" + cliDatabase "github.com/dnote/dnote/pkg/cli/database" + clitest "github.com/dnote/dnote/pkg/cli/testutils" + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/app" + "github.com/dnote/dnote/pkg/server/controllers" + "github.com/dnote/dnote/pkg/server/database" + apitest "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +// testEnv holds the test environment for a single test +type testEnv struct { + DB *cliDatabase.DB + CmdOpts clitest.RunDnoteCmdOptions + Server *httptest.Server + ServerDB *gorm.DB + TmpDir string +} + +// setupTestEnv creates an isolated test environment with its own database and temp directory +func setupTestEnv(t *testing.T) testEnv { + tmpDir := t.TempDir() + + // Create .dnote directory + dnoteDir := filepath.Join(tmpDir, consts.DnoteDirName) + if err := os.MkdirAll(dnoteDir, 0755); err != nil { + t.Fatal(errors.Wrap(err, "creating dnote directory")) + } + + // Create database at the expected path + dbPath := filepath.Join(dnoteDir, consts.DnoteDBFileName) + db := cliDatabase.InitTestFileDBRaw(t, dbPath) + + // Create server + server, serverDB := setupNewServer(t) + + // Create config file with this server's endpoint + apiEndpoint := fmt.Sprintf("%s/api", server.URL) + updateConfigAPIEndpoint(t, tmpDir, apiEndpoint) + + // Create command options with XDG paths pointing to temp dir + cmdOpts := clitest.RunDnoteCmdOptions{ + Env: []string{ + fmt.Sprintf("XDG_CONFIG_HOME=%s", tmpDir), + fmt.Sprintf("XDG_DATA_HOME=%s", tmpDir), + fmt.Sprintf("XDG_CACHE_HOME=%s", tmpDir), + }, + } + + return testEnv{ + DB: db, + CmdOpts: cmdOpts, + Server: server, + ServerDB: serverDB, + TmpDir: tmpDir, + } +} + +// setupTestServer creates a test server with its own database +func setupTestServer(t *testing.T, serverTime time.Time) (*httptest.Server, *gorm.DB, error) { + db := apitest.InitMemoryDB(t) + + mockClock := clock.NewMock() + mockClock.SetNow(serverTime) + + a := app.NewTest() + a.Clock = mockClock + a.EmailBackend = &apitest.MockEmailbackendImplementation{} + a.DB = db + + server, err := controllers.NewServer(&a) + if err != nil { + return nil, nil, errors.Wrap(err, "initializing server") + } + + return server, db, nil +} + +// setupNewServer creates a new server and returns the server and database. +// This is useful when a test needs to switch to a new empty server. +func setupNewServer(t *testing.T) (*httptest.Server, *gorm.DB) { + server, serverDB, err := setupTestServer(t, serverTime) + if err != nil { + t.Fatal(errors.Wrap(err, "setting up new test server")) + } + t.Cleanup(func() { server.Close() }) + + return server, serverDB +} + +// updateConfigAPIEndpoint updates the config file with the given API endpoint +func updateConfigAPIEndpoint(t *testing.T, tmpDir string, apiEndpoint string) { + dnoteDir := filepath.Join(tmpDir, consts.DnoteDirName) + configPath := filepath.Join(dnoteDir, consts.ConfigFilename) + configContent := fmt.Sprintf("apiEndpoint: %s\n", apiEndpoint) + if err := os.WriteFile(configPath, []byte(configContent), 0644); err != nil { + t.Fatal(errors.Wrap(err, "writing config file")) + } +} + +// switchToEmptyServer closes the current server and creates a new empty server, +// updating the config file to point to it. +func switchToEmptyServer(t *testing.T, env *testEnv) { + // Close old server + env.Server.Close() + + // Create new empty server + env.Server, env.ServerDB = setupNewServer(t) + + // Update config file to point to new server + apiEndpoint := fmt.Sprintf("%s/api", env.Server.URL) + updateConfigAPIEndpoint(t, env.TmpDir, apiEndpoint) +} + +// setupUser creates a test user in the server database +func setupUser(t *testing.T, env testEnv) database.User { + user := apitest.SetupUserData(env.ServerDB, "alice@example.com", "pass1234") + + return user +} + +// setupUserAndLogin creates a test user and logs them in on the CLI +func setupUserAndLogin(t *testing.T, env testEnv) database.User { + user := setupUser(t, env) + login(t, env.DB, env.ServerDB, user) + + return user +} + +// login logs in the user in CLI +func login(t *testing.T, db *cliDatabase.DB, serverDB *gorm.DB, user database.User) { + session := apitest.SetupSession(serverDB, user) + + cliDatabase.MustExec(t, "inserting session_key", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKey, session.Key) + cliDatabase.MustExec(t, "inserting session_key_expiry", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemSessionKeyExpiry, session.ExpiresAt.Unix()) +} + +// apiCreateBook creates a book via the API and returns its UUID +func apiCreateBook(t *testing.T, env testEnv, user database.User, name, message string) string { + res := doHTTPReq(t, env, "POST", "/v3/books", fmt.Sprintf(`{"name": "%s"}`, name), message, user) + + var resp controllers.CreateBookResp + if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { + t.Fatal(errors.Wrap(err, "decoding payload for adding book")) + return "" + } + + return resp.Book.UUID +} + +// apiPatchBook updates a book via the API +func apiPatchBook(t *testing.T, env testEnv, user database.User, uuid, payload, message string) { + doHTTPReq(t, env, "PATCH", fmt.Sprintf("/v3/books/%s", uuid), payload, message, user) +} + +// apiDeleteBook deletes a book via the API +func apiDeleteBook(t *testing.T, env testEnv, user database.User, uuid, message string) { + doHTTPReq(t, env, "DELETE", fmt.Sprintf("/v3/books/%s", uuid), "", message, user) +} + +// apiCreateNote creates a note via the API and returns its UUID +func apiCreateNote(t *testing.T, env testEnv, user database.User, bookUUID, body, message string) string { + res := doHTTPReq(t, env, "POST", "/v3/notes", fmt.Sprintf(`{"book_uuid": "%s", "content": "%s"}`, bookUUID, body), message, user) + + var resp controllers.CreateNoteResp + if err := json.NewDecoder(res.Body).Decode(&resp); err != nil { + t.Fatal(errors.Wrap(err, "decoding payload for adding note")) + return "" + } + + return resp.Result.UUID +} + +// apiPatchNote updates a note via the API +func apiPatchNote(t *testing.T, env testEnv, user database.User, noteUUID, payload, message string) { + doHTTPReq(t, env, "PATCH", fmt.Sprintf("/v3/notes/%s", noteUUID), payload, message, user) +} + +// apiDeleteNote deletes a note via the API +func apiDeleteNote(t *testing.T, env testEnv, user database.User, noteUUID, message string) { + doHTTPReq(t, env, "DELETE", fmt.Sprintf("/v3/notes/%s", noteUUID), "", message, user) +} + +// doHTTPReq performs an authenticated HTTP request and checks for errors +func doHTTPReq(t *testing.T, env testEnv, method, path, payload, message string, user database.User) *http.Response { + apiEndpoint := fmt.Sprintf("%s/api", env.Server.URL) + endpoint := fmt.Sprintf("%s%s", apiEndpoint, path) + + req, err := http.NewRequest(method, endpoint, strings.NewReader(payload)) + if err != nil { + panic(errors.Wrap(err, "constructing http request")) + } + + res := apitest.HTTPAuthDo(t, env.ServerDB, req, user) + if res.StatusCode >= 400 { + bs, err := io.ReadAll(res.Body) + if err != nil { + panic(errors.Wrap(err, "parsing response body for error")) + } + + t.Errorf("%s. HTTP status %d. Message: %s", message, res.StatusCode, string(bs)) + } + + return res +} + +// setupFunc is a function that sets up test data and returns IDs for assertions +type setupFunc func(t *testing.T, env testEnv, user database.User) map[string]string + +// assertFunc is a function that asserts the expected state after sync +type assertFunc func(t *testing.T, env testEnv, user database.User, ids map[string]string) + +// testSyncCmd is a test helper that sets up a test environment, runs setup, syncs, and asserts +func testSyncCmd(t *testing.T, fullSync bool, setup setupFunc, assert assertFunc) { + env := setupTestEnv(t) + + user := setupUserAndLogin(t, env) + ids := setup(t, env, user) + + if fullSync { + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync", "-f") + } else { + clitest.RunDnoteCmd(t, env.CmdOpts, cliBinaryName, "sync") + } + + assert(t, env, user, ids) +} + +// systemState represents the expected state of the sync system +type systemState struct { + clientNoteCount int + clientBookCount int + clientLastMaxUSN int + clientLastSyncAt int64 + serverNoteCount int64 + serverBookCount int64 + serverUserMaxUSN int +} + +// checkState compares the state of the client and the server with the given system state +func checkState(t *testing.T, clientDB *cliDatabase.DB, user database.User, serverDB *gorm.DB, expected systemState) { + var clientBookCount, clientNoteCount int + cliDatabase.MustScan(t, "counting client notes", clientDB.QueryRow("SELECT count(*) FROM notes"), &clientNoteCount) + cliDatabase.MustScan(t, "counting client books", clientDB.QueryRow("SELECT count(*) FROM books"), &clientBookCount) + assert.Equal(t, clientNoteCount, expected.clientNoteCount, "client note count mismatch") + assert.Equal(t, clientBookCount, expected.clientBookCount, "client book count mismatch") + + var clientLastMaxUSN int + var clientLastSyncAt int64 + cliDatabase.MustScan(t, "finding system last_max_usn", clientDB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastMaxUSN), &clientLastMaxUSN) + cliDatabase.MustScan(t, "finding system last_sync_at", clientDB.QueryRow("SELECT value FROM system WHERE key = ?", consts.SystemLastSyncAt), &clientLastSyncAt) + assert.Equal(t, clientLastMaxUSN, expected.clientLastMaxUSN, "client last_max_usn mismatch") + assert.Equal(t, clientLastSyncAt, expected.clientLastSyncAt, "client last_sync_at mismatch") + + var serverBookCount, serverNoteCount int64 + apitest.MustExec(t, serverDB.Model(&database.Note{}).Count(&serverNoteCount), "counting server notes") + apitest.MustExec(t, serverDB.Model(&database.Book{}).Count(&serverBookCount), "counting api notes") + assert.Equal(t, serverNoteCount, expected.serverNoteCount, "server note count mismatch") + assert.Equal(t, serverBookCount, expected.serverBookCount, "server book count mismatch") + var serverUser database.User + apitest.MustExec(t, serverDB.Where("id = ?", user.ID).First(&serverUser), "finding user") + assert.Equal(t, serverUser.MaxUSN, expected.serverUserMaxUSN, "user max_usn mismatch") +} diff --git a/pkg/prompt/prompt.go b/pkg/prompt/prompt.go new file mode 100644 index 00000000..262685e9 --- /dev/null +++ b/pkg/prompt/prompt.go @@ -0,0 +1,53 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Package prompt provides utilities for interactive yes/no prompts +package prompt + +import ( + "bufio" + "fmt" + "io" + "strings" +) + +// FormatQuestion formats a yes/no question with the appropriate choice indicator +func FormatQuestion(question string, optimistic bool) string { + choices := "(y/N)" + if optimistic { + choices = "(Y/n)" + } + return fmt.Sprintf("%s %s", question, choices) +} + +// ReadYesNo reads and parses a yes/no response from the given reader. +// Returns true if confirmed, respecting optimistic mode. +// In optimistic mode, empty input is treated as confirmation. +func ReadYesNo(r io.Reader, optimistic bool) (bool, error) { + reader := bufio.NewReader(r) + input, err := reader.ReadString('\n') + if err != nil { + return false, err + } + + input = strings.ToLower(strings.TrimSpace(input)) + confirmed := input == "y" + + if optimistic { + confirmed = confirmed || input == "" + } + + return confirmed, nil +} diff --git a/pkg/prompt/prompt_test.go b/pkg/prompt/prompt_test.go new file mode 100644 index 00000000..6d5eb597 --- /dev/null +++ b/pkg/prompt/prompt_test.go @@ -0,0 +1,145 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package prompt + +import ( + "strings" + "testing" + + "github.com/dnote/dnote/pkg/assert" +) + +func TestFormatQuestion(t *testing.T) { + testCases := []struct { + question string + optimistic bool + expected string + }{ + { + question: "Are you sure?", + optimistic: false, + expected: "Are you sure? (y/N)", + }, + { + question: "Continue?", + optimistic: true, + expected: "Continue? (Y/n)", + }, + } + + for _, tc := range testCases { + t.Run(tc.question, func(t *testing.T) { + result := FormatQuestion(tc.question, tc.optimistic) + assert.Equal(t, result, tc.expected, "formatted question mismatch") + }) + } +} + +func TestReadYesNo(t *testing.T) { + testCases := []struct { + name string + input string + optimistic bool + expected bool + }{ + { + name: "pessimistic with y", + input: "y\n", + optimistic: false, + expected: true, + }, + { + name: "pessimistic with Y (uppercase)", + input: "Y\n", + optimistic: false, + expected: true, + }, + { + name: "pessimistic with n", + input: "n\n", + optimistic: false, + expected: false, + }, + { + name: "pessimistic with empty", + input: "\n", + optimistic: false, + expected: false, + }, + { + name: "pessimistic with whitespace", + input: " \n", + optimistic: false, + expected: false, + }, + { + name: "optimistic with y", + input: "y\n", + optimistic: true, + expected: true, + }, + { + name: "optimistic with n", + input: "n\n", + optimistic: true, + expected: false, + }, + { + name: "optimistic with empty", + input: "\n", + optimistic: true, + expected: true, + }, + { + name: "optimistic with whitespace", + input: " \n", + optimistic: true, + expected: true, + }, + { + name: "invalid input defaults to no", + input: "maybe\n", + optimistic: false, + expected: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + // Create a reader with test input + reader := strings.NewReader(tc.input) + + // Test ReadYesNo + result, err := ReadYesNo(reader, tc.optimistic) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + assert.Equal(t, result, tc.expected, "ReadYesNo result mismatch") + }) + } +} + +func TestReadYesNo_Error(t *testing.T) { + // Test error case with EOF (empty reader) + reader := strings.NewReader("") + + _, err := ReadYesNo(reader, false) + if err == nil { + t.Fatal("expected error when reading from empty reader") + } +} + diff --git a/server/.gitignore b/pkg/server/.gitignore similarity index 76% rename from server/.gitignore rename to pkg/server/.gitignore index 7de9e987..9835f3e3 100644 --- a/server/.gitignore +++ b/pkg/server/.gitignore @@ -5,3 +5,5 @@ tmp/ test-dnote /dist /build +server +/static diff --git a/pkg/server/README.md b/pkg/server/README.md new file mode 100644 index 00000000..bf8bb4fd --- /dev/null +++ b/pkg/server/README.md @@ -0,0 +1 @@ +This directory contains the Dnote server. It comprises of the web interface, the web API, and the background jobs. diff --git a/pkg/server/app/app.go b/pkg/server/app/app.go new file mode 100644 index 00000000..cfaaf319 --- /dev/null +++ b/pkg/server/app/app.go @@ -0,0 +1,71 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/mailer" + "gorm.io/gorm" + "github.com/pkg/errors" +) + +var ( + // ErrEmptyDB is an error for missing database connection in the app configuration + ErrEmptyDB = errors.New("No database connection was provided") + // ErrEmptyClock is an error for missing clock in the app configuration + ErrEmptyClock = errors.New("No clock was provided") + // ErrEmptyBaseURL is an error for missing BaseURL content in the app configuration + ErrEmptyBaseURL = errors.New("No BaseURL was provided") + // ErrEmptyEmailBackend is an error for missing EmailBackend content in the app configuration + ErrEmptyEmailBackend = errors.New("No EmailBackend was provided") + // ErrEmptyHTTP500Page is an error for missing HTTP 500 page content + ErrEmptyHTTP500Page = errors.New("No HTTP 500 error page was set") +) + +// App is an application context +type App struct { + DB *gorm.DB + Clock clock.Clock + EmailBackend mailer.Backend + Files map[string][]byte + HTTP500Page []byte + BaseURL string + DisableRegistration bool + Port string + DBPath string + AssetBaseURL string +} + +// Validate validates the app configuration +func (a *App) Validate() error { + if a.BaseURL == "" { + return ErrEmptyBaseURL + } + if a.Clock == nil { + return ErrEmptyClock + } + if a.EmailBackend == nil { + return ErrEmptyEmailBackend + } + if a.DB == nil { + return ErrEmptyDB + } + if a.HTTP500Page == nil { + return ErrEmptyHTTP500Page + } + + return nil +} diff --git a/pkg/server/app/books.go b/pkg/server/app/books.go new file mode 100644 index 00000000..f222b816 --- /dev/null +++ b/pkg/server/app/books.go @@ -0,0 +1,105 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/helpers" + "gorm.io/gorm" + "github.com/pkg/errors" +) + +// CreateBook creates a book with the next usn and updates the user's max_usn +func (a *App) CreateBook(user database.User, name string) (database.Book, error) { + tx := a.DB.Begin() + + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + tx.Rollback() + return database.Book{}, errors.Wrap(err, "incrementing user max_usn") + } + + uuid, err := helpers.GenUUID() + if err != nil { + tx.Rollback() + return database.Book{}, err + } + + book := database.Book{ + UUID: uuid, + UserID: user.ID, + Label: name, + AddedOn: a.Clock.Now().UnixNano(), + USN: nextUSN, + } + if err := tx.Create(&book).Error; err != nil { + tx.Rollback() + return book, errors.Wrap(err, "inserting book") + } + + tx.Commit() + + return book, nil +} + +// DeleteBook marks a book deleted with the next usn and updates the user's max_usn +func (a *App) DeleteBook(tx *gorm.DB, user database.User, book database.Book) (database.Book, error) { + if user.ID != book.UserID { + return book, errors.New("Not allowed") + } + + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + return book, errors.Wrap(err, "incrementing user max_usn") + } + + if err := tx.Model(&book). + Updates(map[string]interface{}{ + "usn": nextUSN, + "deleted": true, + "label": "", + }).Error; err != nil { + return book, errors.Wrap(err, "deleting book") + } + + return book, nil +} + +// UpdateBook updaates the book, the usn and the user's max_usn +func (a *App) UpdateBook(tx *gorm.DB, user database.User, book database.Book, label *string) (database.Book, error) { + if user.ID != book.UserID { + return book, errors.New("Not allowed") + } + + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + return book, errors.Wrap(err, "incrementing user max_usn") + } + + if label != nil { + book.Label = *label + } + + book.USN = nextUSN + book.EditedOn = a.Clock.Now().UnixNano() + book.Deleted = false + + if err := tx.Save(&book).Error; err != nil { + return book, errors.Wrap(err, "updating the book") + } + + return book, nil +} diff --git a/server/api/operations/books_test.go b/pkg/server/app/books_test.go similarity index 51% rename from server/api/operations/books_test.go rename to pkg/server/app/books_test.go index 9887a98d..8953c613 100644 --- a/server/api/operations/books_test.go +++ b/pkg/server/app/books_test.go @@ -1,37 +1,31 @@ -/* Copyright (C) 2019 Monomax Software Pty Ltd +/* Copyright 2025 Dnote Authors * - * This file is part of Dnote. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * Dnote is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. + * http://www.apache.org/licenses/LICENSE-2.0 * - * Dnote is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with Dnote. If not, see . + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package operations +package app import ( "fmt" "testing" - "github.com/dnote/dnote/server/api/clock" - "github.com/dnote/dnote/server/database" - "github.com/dnote/dnote/server/testutils" + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" "github.com/pkg/errors" ) -func init() { - testutils.InitTestDB() -} - func TestCreateBook(t *testing.T) { testCases := []struct { userUSN int @@ -57,23 +51,24 @@ func TestCreateBook(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData() - db := database.DBConn + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() + user := testutils.SetupUserData(db, "user@test.com", "password123") testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - c := clock.NewMock() + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() - book, err := CreateBook(user, c, tc.label) + book, err := a.CreateBook(user, tc.label) if err != nil { t.Fatal(errors.Wrap(err, "creating book")) } - var bookCount int + var bookCount int64 var bookRecord database.Book var userRecord database.User @@ -87,16 +82,16 @@ func TestCreateBook(t *testing.T) { t.Fatal(errors.Wrap(err, "finding user")) } - testutils.AssertEqual(t, bookCount, 1, "book count mismatch") - testutils.AssertEqual(t, bookRecord.UserID, user.ID, "book user_id mismatch") - testutils.AssertEqual(t, bookRecord.Label, tc.label, "book label mismatch") - testutils.AssertEqual(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") + assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") + assert.Equal(t, bookRecord.Label, tc.label, "book label mismatch") + assert.Equal(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") - testutils.AssertNotEqual(t, book.UUID, "", "book uuid should have been generated") - testutils.AssertEqual(t, book.UserID, user.ID, "returned book user_id mismatch") - testutils.AssertEqual(t, book.Label, tc.label, "returned book label mismatch") - testutils.AssertEqual(t, book.USN, tc.expectedUSN, "returned book usn mismatch") - testutils.AssertEqual(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") + assert.NotEqual(t, book.UUID, "", "book uuid should have been generated") + assert.Equal(t, book.UserID, user.ID, "returned book user_id mismatch") + assert.Equal(t, book.Label, tc.label, "returned book label mismatch") + assert.Equal(t, book.USN, tc.expectedUSN, "returned book usn mismatch") + assert.Equal(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") }() } } @@ -122,27 +117,28 @@ func TestDeleteBook(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData() - db := database.DBConn + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() + user := testutils.SetupUserData(db, "user@test.com", "password123") testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) book := database.Book{UserID: user.ID, Label: "js", Deleted: false} testutils.MustExec(t, db.Save(&book), fmt.Sprintf("preparing book for test case %d", idx)) tx := db.Begin() - ret, err := DeleteBook(tx, user, book) + a := NewTest() + a.DB = db + ret, err := a.DeleteBook(tx, user, book) if err != nil { tx.Rollback() t.Fatal(errors.Wrap(err, "deleting book")) } tx.Commit() - var bookCount int + var bookCount int64 var bookRecord database.Book var userRecord database.User @@ -150,18 +146,18 @@ func TestDeleteBook(t *testing.T) { testutils.MustExec(t, db.First(&bookRecord), fmt.Sprintf("finding book for test case %d", idx)) testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) - testutils.AssertEqual(t, bookCount, 1, "book count mismatch") - testutils.AssertEqual(t, bookRecord.UserID, user.ID, "book user_id mismatch") - testutils.AssertEqual(t, bookRecord.Label, "", "book label mismatch") - testutils.AssertEqual(t, bookRecord.Deleted, true, "book deleted flag mismatch") - testutils.AssertEqual(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") + assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") + assert.Equal(t, bookRecord.Label, "", "book label mismatch") + assert.Equal(t, bookRecord.Deleted, true, "book deleted flag mismatch") + assert.Equal(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") - testutils.AssertEqual(t, ret.UserID, user.ID, "returned book user_id mismatch") - testutils.AssertEqual(t, ret.Label, "", "returned book label mismatch") - testutils.AssertEqual(t, ret.Deleted, true, "returned book deleted flag mismatch") - testutils.AssertEqual(t, ret.USN, tc.expectedUSN, "returned book label mismatch") + assert.Equal(t, ret.UserID, user.ID, "returned book user_id mismatch") + assert.Equal(t, ret.Label, "", "returned book label mismatch") + assert.Equal(t, ret.Deleted, true, "returned book deleted flag mismatch") + assert.Equal(t, ret.USN, tc.expectedUSN, "returned book label mismatch") - testutils.AssertEqual(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") + assert.Equal(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") }() } } @@ -200,23 +196,24 @@ func TestUpdateBook(t *testing.T) { for idx, tc := range testCases { func() { - defer testutils.ClearData() - db := database.DBConn + db := testutils.InitMemoryDB(t) - user := testutils.SetupUserData() + user := testutils.SetupUserData(db, "user@test.com", "password123") testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - anotherUser := testutils.SetupUserData() + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) - c := clock.NewMock() - b := database.Book{UserID: user.ID, Deleted: false, Label: tc.expectedLabel} testutils.MustExec(t, db.Save(&b), fmt.Sprintf("preparing book for test case %d", idx)) - tx := db.Begin() + c := clock.NewMock() + a := NewTest() + a.DB = db + a.Clock = c - book, err := UpdateBook(tx, c, user, b, tc.payloadLabel) + tx := db.Begin() + book, err := a.UpdateBook(tx, user, b, tc.payloadLabel) if err != nil { tx.Rollback() t.Fatal(errors.Wrap(err, "updating book")) @@ -224,25 +221,25 @@ func TestUpdateBook(t *testing.T) { tx.Commit() - var bookCount int + var bookCount int64 var bookRecord database.Book var userRecord database.User testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting books for test case %d", idx)) testutils.MustExec(t, db.First(&bookRecord), fmt.Sprintf("finding book for test case %d", idx)) testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) - testutils.AssertEqual(t, bookCount, 1, "book count mismatch") + assert.Equal(t, bookCount, int64(1), "book count mismatch") - testutils.AssertEqual(t, bookRecord.UserID, user.ID, "book user_id mismatch") - testutils.AssertEqual(t, bookRecord.Label, tc.expectedLabel, "book label mismatch") - testutils.AssertEqual(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") - testutils.AssertEqual(t, bookRecord.EditedOn, c.Now().UnixNano(), "book edited_on mismatch") - testutils.AssertEqual(t, book.UserID, user.ID, "returned book user_id mismatch") - testutils.AssertEqual(t, book.Label, tc.expectedLabel, "returned book label mismatch") - testutils.AssertEqual(t, book.USN, tc.expectedUSN, "returned book usn mismatch") - testutils.AssertEqual(t, book.EditedOn, c.Now().UnixNano(), "returned book edited_on mismatch") + assert.Equal(t, bookRecord.UserID, user.ID, "book user_id mismatch") + assert.Equal(t, bookRecord.Label, tc.expectedLabel, "book label mismatch") + assert.Equal(t, bookRecord.USN, tc.expectedUSN, "book label mismatch") + assert.Equal(t, bookRecord.EditedOn, c.Now().UnixNano(), "book edited_on mismatch") + assert.Equal(t, book.UserID, user.ID, "returned book user_id mismatch") + assert.Equal(t, book.Label, tc.expectedLabel, "returned book label mismatch") + assert.Equal(t, book.USN, tc.expectedUSN, "returned book usn mismatch") + assert.Equal(t, book.EditedOn, c.Now().UnixNano(), "returned book edited_on mismatch") - testutils.AssertEqual(t, userRecord.MaxUSN, tc.expectedUserUSN, "user max_usn mismatch") + assert.Equal(t, userRecord.MaxUSN, tc.expectedUserUSN, "user max_usn mismatch") }() } } diff --git a/pkg/server/app/doc.go b/pkg/server/app/doc.go new file mode 100644 index 00000000..2a3f3695 --- /dev/null +++ b/pkg/server/app/doc.go @@ -0,0 +1,19 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* +Package app implements application business logic +*/ +package app diff --git a/pkg/server/app/email.go b/pkg/server/app/email.go new file mode 100644 index 00000000..fc86cda9 --- /dev/null +++ b/pkg/server/app/email.go @@ -0,0 +1,129 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "fmt" + "net/url" + "strings" + + "github.com/dnote/dnote/pkg/server/mailer" + "github.com/pkg/errors" +) + +var defaultSender = "admin@getdnote.com" + +// GetSenderEmail returns the sender email +func GetSenderEmail(baseURL, want string) (string, error) { + addr, err := getNoreplySender(baseURL) + if err != nil { + return "", errors.Wrap(err, "getting sender email address") + } + + return addr, nil +} + +func getDomainFromURL(rawURL string) (string, error) { + u, err := url.Parse(rawURL) + if err != nil { + return "", errors.Wrap(err, "parsing url") + } + + host := u.Hostname() + parts := strings.Split(host, ".") + if len(parts) < 2 { + return host, nil + } + domain := parts[len(parts)-2] + "." + parts[len(parts)-1] + + return domain, nil +} + +func getNoreplySender(baseURL string) (string, error) { + domain, err := getDomainFromURL(baseURL) + if err != nil { + return "", errors.Wrap(err, "parsing base url") + } + + addr := fmt.Sprintf("noreply@%s", domain) + return addr, nil +} + +// SendWelcomeEmail sends welcome email +func (a *App) SendWelcomeEmail(email string) error { + from, err := GetSenderEmail(a.BaseURL, defaultSender) + if err != nil { + return errors.Wrap(err, "getting the sender email") + } + + data := mailer.WelcomeTmplData{ + AccountEmail: email, + BaseURL: a.BaseURL, + } + + if err := a.EmailBackend.SendEmail(mailer.EmailTypeWelcome, from, []string{email}, data); err != nil { + return errors.Wrapf(err, "sending welcome email for %s", email) + } + + return nil +} + +// SendPasswordResetEmail sends password reset email +func (a *App) SendPasswordResetEmail(email, tokenValue string) error { + if email == "" { + return ErrEmailRequired + } + + from, err := GetSenderEmail(a.BaseURL, defaultSender) + if err != nil { + return errors.Wrap(err, "getting the sender email") + } + + data := mailer.EmailResetPasswordTmplData{ + AccountEmail: email, + Token: tokenValue, + BaseURL: a.BaseURL, + } + + if err := a.EmailBackend.SendEmail(mailer.EmailTypeResetPassword, from, []string{email}, data); err != nil { + if errors.Cause(err) == mailer.ErrSMTPNotConfigured { + return ErrInvalidSMTPConfig + } + + return errors.Wrapf(err, "sending password reset email for %s", email) + } + + return nil +} + +// SendPasswordResetAlertEmail sends email that notifies users of a password change +func (a *App) SendPasswordResetAlertEmail(email string) error { + from, err := GetSenderEmail(a.BaseURL, defaultSender) + if err != nil { + return errors.Wrap(err, "getting the sender email") + } + + data := mailer.EmailResetPasswordAlertTmplData{ + AccountEmail: email, + BaseURL: a.BaseURL, + } + + if err := a.EmailBackend.SendEmail(mailer.EmailTypeResetPasswordAlert, from, []string{email}, data); err != nil { + return errors.Wrapf(err, "sending password reset alert email for %s", email) + } + + return nil +} diff --git a/pkg/server/app/email_test.go b/pkg/server/app/email_test.go new file mode 100644 index 00000000..e0a73aef --- /dev/null +++ b/pkg/server/app/email_test.go @@ -0,0 +1,77 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/testutils" +) + +func TestSendWelcomeEmail(t *testing.T) { + emailBackend := testutils.MockEmailbackendImplementation{} + a := NewTest() + a.EmailBackend = &emailBackend + a.BaseURL = "http://example.com" + + if err := a.SendWelcomeEmail("alice@example.com"); err != nil { + t.Fatal(err, "failed to perform") + } + + assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") + assert.Equal(t, emailBackend.Emails[0].From, "noreply@example.com", "email sender mismatch") + assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") + +} + +func TestSendPasswordResetEmail(t *testing.T) { + emailBackend := testutils.MockEmailbackendImplementation{} + a := NewTest() + a.EmailBackend = &emailBackend + a.BaseURL = "http://example.com" + + if err := a.SendPasswordResetEmail("alice@example.com", "mockTokenValue"); err != nil { + t.Fatal(err, "failed to perform") + } + + assert.Equalf(t, len(emailBackend.Emails), 1, "email queue count mismatch") + assert.Equal(t, emailBackend.Emails[0].From, "noreply@example.com", "email sender mismatch") + assert.DeepEqual(t, emailBackend.Emails[0].To, []string{"alice@example.com"}, "email sender mismatch") + +} + +func TestGetSenderEmail(t *testing.T) { + testCases := []struct { + baseURL string + expectedSender string + }{ + { + baseURL: "https://www.example.com", + expectedSender: "noreply@example.com", + }, + { + baseURL: "https://www.example2.com", + expectedSender: "alice@example2.com", + }, + } + + for _, tc := range testCases { + t.Run(fmt.Sprintf("base url %s", tc.baseURL), func(t *testing.T) { + }) + } +} diff --git a/pkg/server/app/errors.go b/pkg/server/app/errors.go new file mode 100644 index 00000000..67635b23 --- /dev/null +++ b/pkg/server/app/errors.go @@ -0,0 +1,82 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +type appError string + +func (e appError) Error() string { + return string(e) +} + +func (e appError) Public() string { + return string(e) +} + +var ( + // ErrNotFound an error that indicates that the given resource is not found + ErrNotFound appError = "not found" + // ErrLoginInvalid is an error for invalid login + ErrLoginInvalid appError = "Wrong email and password combination" + + // ErrDuplicateEmail is an error for duplicate email + ErrDuplicateEmail appError = "duplicate email" + // ErrEmailRequired is an error for missing email + ErrEmailRequired appError = "Please enter an email" + // ErrPasswordRequired is an error for missing email + ErrPasswordRequired appError = "Please enter a password" + // ErrPasswordTooShort is an error for short password + ErrPasswordTooShort appError = "password should be longer than 8 characters" + // ErrPasswordConfirmationMismatch is an error for password ans password confirmation not matching + ErrPasswordConfirmationMismatch appError = "password confirmation does not match password" + + // ErrLoginRequired is an error for not authenticated + ErrLoginRequired appError = "login required" + + // ErrBookUUIDRequired is an error for note missing book uuid + ErrBookUUIDRequired appError = "book uuid required" + // ErrBookNameRequired is an error for note missing book name + ErrBookNameRequired appError = "book name required" + // ErrDuplicateBook is an error for duplicate book + ErrDuplicateBook appError = "duplicate book exists" + + // ErrEmptyUpdate is an error for empty update params + ErrEmptyUpdate appError = "update is empty" + + // ErrInvalidUUID is an error for invalid uuid + ErrInvalidUUID appError = "invalid uuid" + + // ErrInvalidSMTPConfig is an error for invalid SMTP configuration + ErrInvalidSMTPConfig appError = "SMTP is not configured" + + // ErrInvalidToken is an error for invalid token + ErrInvalidToken appError = "invalid token" + // ErrMissingToken is an error for missing token + ErrMissingToken appError = "missing token" + // ErrExpiredToken is an error for missing token + ErrExpiredToken appError = "This token has expired." + + // ErrPasswordResetTokenExpired is an error for expired password reset token + ErrPasswordResetTokenExpired appError = "this link has been expired. Please request a new password reset link." + // ErrInvalidPasswordChangeInput is an error for changing password + ErrInvalidPasswordChangeInput appError = "Both current and new passwords are required to change the password." + + ErrInvalidPassword appError = "Invalid current password." + // ErrEmailTooLong is an error for email length exceeding the limit + ErrEmailTooLong appError = "Email is too long." + + // ErrUserHasExistingResources is an error for attempting to remove a user with existing notes or books + ErrUserHasExistingResources appError = "cannot remove user with existing notes or books" +) diff --git a/pkg/server/app/helpers.go b/pkg/server/app/helpers.go new file mode 100644 index 00000000..437ca9ae --- /dev/null +++ b/pkg/server/app/helpers.go @@ -0,0 +1,53 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "time" + + "github.com/dnote/dnote/pkg/server/database" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +// incrementUserUSN increment the given user's max_usn by 1 +// and returns the new, incremented max_usn +func incrementUserUSN(tx *gorm.DB, userID int) (int, error) { + // First, get the current max_usn to detect transition from empty server + var user database.User + if err := tx.Select("max_usn, full_sync_before").Where("id = ?", userID).First(&user).Error; err != nil { + return 0, errors.Wrap(err, "getting current user state") + } + + // If transitioning from empty server (MaxUSN=0) to non-empty (MaxUSN=1), + // set full_sync_before to current timestamp to force all other clients to full sync + if user.MaxUSN == 0 && user.FullSyncBefore == 0 { + currentTime := time.Now().Unix() + if err := tx.Table("users").Where("id = ?", userID).Update("full_sync_before", currentTime).Error; err != nil { + return 0, errors.Wrap(err, "setting full_sync_before on empty server transition") + } + } + + if err := tx.Table("users").Where("id = ?", userID).Update("max_usn", gorm.Expr("max_usn + 1")).Error; err != nil { + return 0, errors.Wrap(err, "incrementing user max_usn") + } + + if err := tx.Select("max_usn").Where("id = ?", userID).First(&user).Error; err != nil { + return 0, errors.Wrap(err, "getting the updated user max_usn") + } + + return user.MaxUSN, nil +} diff --git a/pkg/server/app/helpers_test.go b/pkg/server/app/helpers_test.go new file mode 100644 index 00000000..93486ecd --- /dev/null +++ b/pkg/server/app/helpers_test.go @@ -0,0 +1,67 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "fmt" + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" +) + +func TestIncremenetUserUSN(t *testing.T) { + testCases := []struct { + maxUSN int + expectedMaxUSN int + }{ + { + maxUSN: 1, + expectedMaxUSN: 2, + }, + { + maxUSN: 1988, + expectedMaxUSN: 1989, + }, + } + + // set up + for idx, tc := range testCases { + func() { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.maxUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + + // execute + tx := db.Begin() + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + t.Fatal(errors.Wrap(err, "incrementing the user usn")) + } + tx.Commit() + + // test + var userRecord database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + + assert.Equal(t, userRecord.MaxUSN, tc.expectedMaxUSN, fmt.Sprintf("user max_usn mismatch for case %d", idx)) + assert.Equal(t, nextUSN, tc.expectedMaxUSN, fmt.Sprintf("next_usn mismatch for case %d", idx)) + }() + } +} diff --git a/pkg/server/app/notes.go b/pkg/server/app/notes.go new file mode 100644 index 00000000..4bdeaaf6 --- /dev/null +++ b/pkg/server/app/notes.go @@ -0,0 +1,291 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "errors" + "time" + + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/helpers" + pkgErrors "github.com/pkg/errors" + "gorm.io/gorm" +) + +// CreateNote creates a note with the next usn and updates the user's max_usn. +// It returns the created note. +func (a *App) CreateNote(user database.User, bookUUID, content string, addedOn *int64, editedOn *int64, client string) (database.Note, error) { + tx := a.DB.Begin() + + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + tx.Rollback() + return database.Note{}, pkgErrors.Wrap(err, "incrementing user max_usn") + } + + var noteAddedOn int64 + if addedOn == nil { + noteAddedOn = a.Clock.Now().UnixNano() + } else { + noteAddedOn = *addedOn + } + + var noteEditedOn int64 + if editedOn == nil { + noteEditedOn = 0 + } else { + noteEditedOn = *editedOn + } + + uuid, err := helpers.GenUUID() + if err != nil { + tx.Rollback() + return database.Note{}, err + } + + note := database.Note{ + UUID: uuid, + BookUUID: bookUUID, + UserID: user.ID, + AddedOn: noteAddedOn, + EditedOn: noteEditedOn, + USN: nextUSN, + Body: content, + Client: client, + } + if err := tx.Create(¬e).Error; err != nil { + tx.Rollback() + return note, pkgErrors.Wrap(err, "inserting note") + } + + tx.Commit() + + return note, nil +} + +// UpdateNoteParams is the parameters for updating a note +type UpdateNoteParams struct { + BookUUID *string + Content *string +} + +// GetBookUUID gets the bookUUID from the UpdateNoteParams +func (r UpdateNoteParams) GetBookUUID() string { + if r.BookUUID == nil { + return "" + } + + return *r.BookUUID +} + +// GetContent gets the content from the UpdateNoteParams +func (r UpdateNoteParams) GetContent() string { + if r.Content == nil { + return "" + } + + return *r.Content +} + +// UpdateNote creates a note with the next usn and updates the user's max_usn +func (a *App) UpdateNote(tx *gorm.DB, user database.User, note database.Note, p *UpdateNoteParams) (database.Note, error) { + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + return note, pkgErrors.Wrap(err, "incrementing user max_usn") + } + + if p.BookUUID != nil { + note.BookUUID = p.GetBookUUID() + } + if p.Content != nil { + note.Body = p.GetContent() + } + + note.USN = nextUSN + note.EditedOn = a.Clock.Now().UnixNano() + note.Deleted = false + + if err := tx.Save(¬e).Error; err != nil { + return note, pkgErrors.Wrap(err, "editing note") + } + + return note, nil +} + +// DeleteNote marks a note deleted with the next usn and updates the user's max_usn +func (a *App) DeleteNote(tx *gorm.DB, user database.User, note database.Note) (database.Note, error) { + nextUSN, err := incrementUserUSN(tx, user.ID) + if err != nil { + return note, pkgErrors.Wrap(err, "incrementing user max_usn") + } + + if err := tx.Model(¬e). + Updates(map[string]interface{}{ + "usn": nextUSN, + "deleted": true, + "body": "", + }).Error; err != nil { + return note, pkgErrors.Wrap(err, "deleting note") + } + + return note, nil +} + +// GetUserNoteByUUID retrives a digest by the uuid for the given user +func (a *App) GetUserNoteByUUID(userID int, uuid string) (*database.Note, error) { + var ret database.Note + err := a.DB.Where("user_id = ? AND uuid = ?", userID, uuid).First(&ret).Error + + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, nil + } + if err != nil { + return nil, pkgErrors.Wrap(err, "finding digest") + } + + return &ret, nil +} + +// GetNotesParams is params for finding notes +type GetNotesParams struct { + Year int + Month int + Page int + Books []string + Search string + PerPage int +} + +type ftsParams struct { + HighlightAll bool +} + +func getFTSBodyExpression(params *ftsParams) string { + if params != nil && params.HighlightAll { + return "highlight(notes_fts, 0, '', '') AS body" + } + + return "snippet(notes_fts, 0, '', '', '...', 50) AS body" +} + +func selectFTSFields(conn *gorm.DB, params *ftsParams) *gorm.DB { + bodyExpr := getFTSBodyExpression(params) + + return conn.Select(` +notes.id, +notes.uuid, +notes.created_at, +notes.updated_at, +notes.book_uuid, +notes.user_id, +notes.added_on, +notes.edited_on, +notes.usn, +notes.deleted, +` + bodyExpr) +} + +func getNotesBaseQuery(db *gorm.DB, userID int, q GetNotesParams) *gorm.DB { + conn := db.Where( + "notes.user_id = ? AND notes.deleted = ?", + userID, false, + ) + + if q.Search != "" { + conn = selectFTSFields(conn, nil) + conn = conn.Joins("INNER JOIN notes_fts ON notes_fts.rowid = notes.id") + conn = conn.Where("notes_fts MATCH ?", q.Search) + } + + if len(q.Books) > 0 { + conn = conn.Joins("INNER JOIN books ON books.uuid = notes.book_uuid"). + Where("books.label in (?)", q.Books) + } + + if q.Year != 0 || q.Month != 0 { + dateLowerbound, dateUpperbound := getDateBounds(q.Year, q.Month) + conn = conn.Where("notes.added_on >= ? AND notes.added_on < ?", dateLowerbound, dateUpperbound) + } + + return conn +} + +func getDateBounds(year, month int) (int64, int64) { + var yearUpperbound, monthUpperbound int + + if month == 12 { + monthUpperbound = 1 + yearUpperbound = year + 1 + } else { + monthUpperbound = month + 1 + yearUpperbound = year + } + + lower := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC).UnixNano() + upper := time.Date(yearUpperbound, time.Month(monthUpperbound), 1, 0, 0, 0, 0, time.UTC).UnixNano() + + return lower, upper +} + +func orderGetNotes(conn *gorm.DB) *gorm.DB { + return conn.Order("notes.updated_at DESC, notes.id DESC") +} + +func paginate(conn *gorm.DB, page, perPage int) *gorm.DB { + // Paginate + if page > 0 { + offset := perPage * (page - 1) + conn = conn.Offset(offset) + } + + conn = conn.Limit(perPage) + + return conn +} + +// GetNotesResult is the result of getting notes +type GetNotesResult struct { + Notes []database.Note + Total int64 +} + +// GetNotes returns a list of matching notes +func (a *App) GetNotes(userID int, params GetNotesParams) (GetNotesResult, error) { + conn := getNotesBaseQuery(a.DB, userID, params) + + var total int64 + if err := conn.Model(database.Note{}).Count(&total).Error; err != nil { + return GetNotesResult{}, pkgErrors.Wrap(err, "counting total") + } + + notes := []database.Note{} + if total != 0 { + conn = orderGetNotes(conn) + conn = database.PreloadNote(conn) + conn = paginate(conn, params.Page, params.PerPage) + + if err := conn.Find(¬es).Error; err != nil { + return GetNotesResult{}, pkgErrors.Wrap(err, "finding notes") + } + } + + res := GetNotesResult{ + Notes: notes, + Total: total, + } + + return res, nil +} diff --git a/pkg/server/app/notes_test.go b/pkg/server/app/notes_test.go new file mode 100644 index 00000000..a165312d --- /dev/null +++ b/pkg/server/app/notes_test.go @@ -0,0 +1,515 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "fmt" + "strings" + "testing" + "time" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" +) + +func TestCreateNote(t *testing.T) { + serverTime := time.Date(2017, time.March, 14, 21, 15, 0, 0, time.UTC) + + ts1 := time.Date(2018, time.November, 12, 10, 11, 0, 0, time.UTC).UnixNano() + ts2 := time.Date(2018, time.November, 15, 0, 1, 10, 0, time.UTC).UnixNano() + + testCases := []struct { + userUSN int + addedOn *int64 + editedOn *int64 + expectedUSN int + expectedAddedOn int64 + expectedEditedOn int64 + }{ + { + userUSN: 8, + addedOn: nil, + editedOn: nil, + expectedUSN: 9, + expectedAddedOn: serverTime.UnixNano(), + expectedEditedOn: 0, + }, + { + userUSN: 102229, + addedOn: &ts1, + editedOn: nil, + expectedUSN: 102230, + expectedAddedOn: ts1, + expectedEditedOn: 0, + }, + { + userUSN: 8099, + addedOn: &ts1, + editedOn: &ts2, + expectedUSN: 8100, + expectedAddedOn: ts1, + expectedEditedOn: ts2, + }, + } + + for idx, tc := range testCases { + func() { + // Create a new clock for each test case to avoid race conditions in parallel tests + mockClock := clock.NewMock() + mockClock.SetNow(serverTime) + + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + fmt.Println(user) + + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + + b1 := database.Book{UserID: user.ID, Label: "js", Deleted: false} + testutils.MustExec(t, db.Save(&b1), fmt.Sprintf("preparing b1 for test case %d", idx)) + + a := NewTest() + a.DB = db + a.Clock = mockClock + + if _, err := a.CreateNote(user, b1.UUID, "note content", tc.addedOn, tc.editedOn, ""); err != nil { + t.Fatal(errors.Wrapf(err, "creating note for test case %d", idx)) + } + + var bookCount, noteCount int64 + var noteRecord database.Note + var userRecord database.User + + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), fmt.Sprintf("counting book for test case %d", idx)) + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), fmt.Sprintf("counting notes for test case %d", idx)) + testutils.MustExec(t, db.First(¬eRecord), fmt.Sprintf("finding note for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + + assert.Equal(t, bookCount, int64(1), "book count mismatch") + assert.Equal(t, noteCount, int64(1), "note count mismatch") + assert.NotEqual(t, noteRecord.UUID, "", "note UUID should have been generated") + assert.Equal(t, noteRecord.UserID, user.ID, "note UserID mismatch") + assert.Equal(t, noteRecord.Body, "note content", "note Body mismatch") + assert.Equal(t, noteRecord.Deleted, false, "note Deleted mismatch") + assert.Equal(t, noteRecord.USN, tc.expectedUSN, "note Label mismatch") + assert.Equal(t, noteRecord.AddedOn, tc.expectedAddedOn, "note AddedOn mismatch") + assert.Equal(t, noteRecord.EditedOn, tc.expectedEditedOn, "note EditedOn mismatch") + + assert.Equal(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") + + // Assert FTS table is updated + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", noteRecord.ID).Scan(&ftsBody), fmt.Sprintf("querying notes_fts for test case %d", idx)) + assert.Equal(t, ftsBody, "note content", "FTS body mismatch") + var searchCount int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE notes_fts MATCH ?", "content").Scan(&searchCount), "searching notes_fts") + assert.Equal(t, searchCount, int64(1), "Note should still be searchable") + }() + } +} + +func TestCreateNote_EmptyBody(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Create note with empty body + note, err := a.CreateNote(user, b1.UUID, "", nil, nil, "") + if err != nil { + t.Fatal(errors.Wrap(err, "creating note with empty body")) + } + + // Assert FTS entry exists with empty body + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsBody), "querying notes_fts for empty body note") + assert.Equal(t, ftsBody, "", "FTS body should be empty for note created with empty body") +} + +func TestUpdateNote(t *testing.T) { + testCases := []struct { + userUSN int + }{ + { + userUSN: 8, + }, + { + userUSN: 102229, + }, + { + userUSN: 8099, + }, + } + + for idx, tc := range testCases { + t.Run(fmt.Sprintf("test case %d", idx), func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), "preparing user max_usn for test case") + + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), "preparing user max_usn for test case") + + b1 := database.Book{UserID: user.ID, Label: "js", Deleted: false} + testutils.MustExec(t, db.Save(&b1), "preparing b1 for test case") + + note := database.Note{UserID: user.ID, Deleted: false, Body: "test content", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e), "preparing note for test case") + + // Assert FTS table has original content + var ftsBodyBefore string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsBodyBefore), "querying notes_fts before update") + assert.Equal(t, ftsBodyBefore, "test content", "FTS body mismatch before update") + + c := clock.NewMock() + content := "updated test content" + + a := NewTest() + a.DB = db + a.Clock = c + + tx := db.Begin() + if _, err := a.UpdateNote(tx, user, note, &UpdateNoteParams{ + Content: &content, + }); err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "updating note")) + } + tx.Commit() + + var bookCount, noteCount int64 + var noteRecord database.Note + var userRecord database.User + + testutils.MustExec(t, db.Model(&database.Book{}).Count(&bookCount), "counting book for test case") + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), "counting notes for test case") + testutils.MustExec(t, db.First(¬eRecord), "finding note for test case") + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), "finding user for test case") + + expectedUSN := tc.userUSN + 1 + assert.Equal(t, bookCount, int64(1), "book count mismatch") + assert.Equal(t, noteCount, int64(1), "note count mismatch") + assert.Equal(t, noteRecord.UserID, user.ID, "note UserID mismatch") + assert.Equal(t, noteRecord.Body, content, "note Body mismatch") + assert.Equal(t, noteRecord.Deleted, false, "note Deleted mismatch") + assert.Equal(t, noteRecord.USN, expectedUSN, "note USN mismatch") + assert.Equal(t, userRecord.MaxUSN, expectedUSN, "user MaxUSN mismatch") + + // Assert FTS table is updated with new content + var ftsBodyAfter string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", noteRecord.ID).Scan(&ftsBodyAfter), "querying notes_fts after update") + assert.Equal(t, ftsBodyAfter, content, "FTS body mismatch after update") + var searchCount int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE notes_fts MATCH ?", "updated").Scan(&searchCount), "searching notes_fts") + assert.Equal(t, searchCount, int64(1), "Note should still be searchable") + }) + } +} + +func TestUpdateNote_SameContent(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + note := database.Note{UserID: user.ID, Deleted: false, Body: "test content", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e), "preparing note") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Update note with same content + sameContent := "test content" + tx := db.Begin() + _, err := a.UpdateNote(tx, user, note, &UpdateNoteParams{ + Content: &sameContent, + }) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "updating note with same content")) + } + tx.Commit() + + // Assert FTS still has the same content + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsBody), "querying notes_fts after update") + assert.Equal(t, ftsBody, "test content", "FTS body should still be 'test content'") + + // Assert it's still searchable + var searchCount int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE notes_fts MATCH ?", "test").Scan(&searchCount), "searching notes_fts") + assert.Equal(t, searchCount, int64(1), "Note should still be searchable") +} + +func TestDeleteNote(t *testing.T) { + testCases := []struct { + userUSN int + expectedUSN int + }{ + { + userUSN: 3, + expectedUSN: 4, + }, + { + userUSN: 9787, + expectedUSN: 9788, + }, + { + userUSN: 787, + expectedUSN: 788, + }, + } + + for idx, tc := range testCases { + func() { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + testutils.MustExec(t, db.Model(&user).Update("max_usn", tc.userUSN), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + + anotherUser := testutils.SetupUserData(db, "another@test.com", "password123") + testutils.MustExec(t, db.Model(&anotherUser).Update("max_usn", 55), fmt.Sprintf("preparing user max_usn for test case %d", idx)) + + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), fmt.Sprintf("preparing b1 for test case %d", idx)) + + note := database.Note{UserID: user.ID, Deleted: false, Body: "test content", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e), fmt.Sprintf("preparing note for test case %d", idx)) + + // Assert FTS table has content before delete + var ftsCountBefore int64 + testutils.MustExec(t, db.Raw("SELECT COUNT(*) FROM notes_fts WHERE rowid = ?", note.ID).Scan(&ftsCountBefore), fmt.Sprintf("counting notes_fts before delete for test case %d", idx)) + assert.Equal(t, ftsCountBefore, int64(1), "FTS should have entry before delete") + + a := NewTest() + a.DB = db + + tx := db.Begin() + ret, err := a.DeleteNote(tx, user, note) + if err != nil { + tx.Rollback() + t.Fatal(errors.Wrap(err, "deleting note")) + } + tx.Commit() + + var noteCount int64 + var noteRecord database.Note + var userRecord database.User + + testutils.MustExec(t, db.Model(&database.Note{}).Count(¬eCount), fmt.Sprintf("counting notes for test case %d", idx)) + testutils.MustExec(t, db.First(¬eRecord), fmt.Sprintf("finding note for test case %d", idx)) + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&userRecord), fmt.Sprintf("finding user for test case %d", idx)) + + assert.Equal(t, noteCount, int64(1), "note count mismatch") + + assert.Equal(t, noteRecord.UserID, user.ID, "note user_id mismatch") + assert.Equal(t, noteRecord.Body, "", "note content mismatch") + assert.Equal(t, noteRecord.Deleted, true, "note deleted flag mismatch") + assert.Equal(t, noteRecord.USN, tc.expectedUSN, "note label mismatch") + assert.Equal(t, userRecord.MaxUSN, tc.expectedUSN, "user max_usn mismatch") + + assert.Equal(t, ret.UserID, user.ID, "note user_id mismatch") + assert.Equal(t, ret.Body, "", "note content mismatch") + assert.Equal(t, ret.Deleted, true, "note deleted flag mismatch") + assert.Equal(t, ret.USN, tc.expectedUSN, "note label mismatch") + + // Assert FTS body is empty after delete (row still exists but content is cleared) + var ftsBody string + testutils.MustExec(t, db.Raw("SELECT body FROM notes_fts WHERE rowid = ?", noteRecord.ID).Scan(&ftsBody), fmt.Sprintf("querying notes_fts after delete for test case %d", idx)) + assert.Equal(t, ftsBody, "", "FTS body should be empty after delete") + }() + } +} + +func TestGetNotes_FTSSearch(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + // Create notes with different content + note1 := database.Note{UserID: user.ID, Deleted: false, Body: "foo bar baz bar", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e1), "preparing note1") + + note2 := database.Note{UserID: user.ID, Deleted: false, Body: "hello run foo", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e2), "preparing note2") + + note3 := database.Note{UserID: user.ID, Deleted: false, Body: "running quz succeeded", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e3), "preparing note3") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Search "baz" + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "baz", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search")) + } + assert.Equal(t, result.Total, int64(1), "Should find 1 note with 'baz'") + assert.Equal(t, len(result.Notes), 1, "Should return 1 note") + for i, note := range result.Notes { + assert.Equal(t, strings.Contains(note.Body, "baz"), true, fmt.Sprintf("Note %d should contain highlighted dnote", i)) + } + + // Search for "running" - should return 1 note + result, err = a.GetNotes(user.ID, GetNotesParams{ + Search: "running", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for review")) + } + assert.Equal(t, result.Total, int64(2), "Should find 2 note with 'running'") + assert.Equal(t, len(result.Notes), 2, "Should return 2 notes") + assert.Equal(t, result.Notes[0].Body, "running quz succeeded", "Should return the review note with highlighting") + assert.Equal(t, result.Notes[1].Body, "hello run foo", "Should return the review note with highlighting") + + // Search for non-existent term - should return 0 notes + result, err = a.GetNotes(user.ID, GetNotesParams{ + Search: "nonexistent", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for nonexistent")) + } + + assert.Equal(t, result.Total, int64(0), "Should find 0 notes with 'nonexistent'") + assert.Equal(t, len(result.Notes), 0, "Should return 0 notes") +} + +func TestGetNotes_FTSSearch_Snippet(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + // Create a long note to test snippet truncation with "..." + // The snippet limit is 50 tokens, so we generate enough words to exceed it + longBody := strings.Repeat("filler ", 100) + "the important keyword appears here" + longNote := database.Note{UserID: user.ID, Deleted: false, Body: longBody, BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(&longNote), "preparing long note") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + // Search for "keyword" in long note - should return snippet with "..." + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "keyword", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for keyword")) + } + + assert.Equal(t, result.Total, int64(1), "Should find 1 note with 'keyword'") + assert.Equal(t, len(result.Notes), 1, "Should return 1 note") + // The snippet should contain "..." to indicate truncation and the highlighted keyword + assert.Equal(t, strings.Contains(result.Notes[0].Body, "..."), true, "Snippet should contain '...' for truncation") + assert.Equal(t, strings.Contains(result.Notes[0].Body, "keyword"), true, "Snippet should contain highlighted keyword") +} + +func TestGetNotes_FTSSearch_ShortWord(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + // Create notes with short words + note1 := database.Note{UserID: user.ID, Deleted: false, Body: "a b c", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e1), "preparing note1") + + note2 := database.Note{UserID: user.ID, Deleted: false, Body: "d", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e2), "preparing note2") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "a", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for 'a'")) + } + + assert.Equal(t, result.Total, int64(1), "Should find 1 note") + assert.Equal(t, len(result.Notes), 1, "Should return 1 note") + assert.Equal(t, strings.Contains(result.Notes[0].Body, "a"), true, "Should contain highlighted 'a'") +} + +func TestGetNotes_All(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "user@test.com", "password123") + b1 := database.Book{UserID: user.ID, Label: "testBook"} + testutils.MustExec(t, db.Save(&b1), "preparing book") + + note1 := database.Note{UserID: user.ID, Deleted: false, Body: "a b c", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e1), "preparing note1") + + note2 := database.Note{UserID: user.ID, Deleted: false, Body: "d", BookUUID: b1.UUID} + testutils.MustExec(t, db.Save(¬e2), "preparing note2") + + a := NewTest() + a.DB = db + a.Clock = clock.NewMock() + + result, err := a.GetNotes(user.ID, GetNotesParams{ + Search: "", + Page: 1, + PerPage: 30, + }) + if err != nil { + t.Fatal(errors.Wrap(err, "getting notes with FTS search for 'a'")) + } + + assert.Equal(t, result.Total, int64(2), "Should not find all notes") + assert.Equal(t, len(result.Notes), 2, "Should not find all notes") + + for _, note := range result.Notes { + assert.Equal(t, strings.Contains(note.Body, ""), false, "There should be no keywords") + assert.Equal(t, strings.Contains(note.Body, ""), false, "There should be no keywords") + } + assert.Equal(t, result.Notes[0].Body, "d", "Full content should be returned") + assert.Equal(t, result.Notes[1].Body, "a b c", "Full content should be returned") +} diff --git a/pkg/server/app/sessions.go b/pkg/server/app/sessions.go new file mode 100644 index 00000000..6c230d76 --- /dev/null +++ b/pkg/server/app/sessions.go @@ -0,0 +1,65 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "time" + + "github.com/dnote/dnote/pkg/server/crypt" + "github.com/dnote/dnote/pkg/server/database" + "gorm.io/gorm" + "github.com/pkg/errors" +) + +// CreateSession returns a new session for the user of the given id +func (a *App) CreateSession(userID int) (database.Session, error) { + key, err := crypt.GetRandomStr(32) + if err != nil { + return database.Session{}, errors.Wrap(err, "generating key") + } + + session := database.Session{ + UserID: userID, + Key: key, + LastUsedAt: time.Now(), + ExpiresAt: time.Now().Add(24 * 100 * time.Hour), + } + + if err := a.DB.Save(&session).Error; err != nil { + return database.Session{}, errors.Wrap(err, "saving session") + } + + return session, nil +} + +// DeleteUserSessions deletes all existing sessions for the given user. It effectively +// invalidates all existing sessions. +func (a *App) DeleteUserSessions(db *gorm.DB, userID int) error { + if err := db.Where("user_id = ?", userID).Delete(&database.Session{}).Error; err != nil { + return errors.Wrap(err, "deleting sessions") + } + + return nil +} + +// DeleteSession deletes the session that match the given info +func (a *App) DeleteSession(sessionKey string) error { + if err := a.DB.Where("key = ?", sessionKey).Delete(&database.Session{}).Error; err != nil { + return errors.Wrap(err, "deleting the session") + } + + return nil +} diff --git a/pkg/server/app/testutils.go b/pkg/server/app/testutils.go new file mode 100644 index 00000000..248f4784 --- /dev/null +++ b/pkg/server/app/testutils.go @@ -0,0 +1,36 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "github.com/dnote/dnote/pkg/clock" + "github.com/dnote/dnote/pkg/server/assets" + "github.com/dnote/dnote/pkg/server/testutils" +) + +// NewTest returns an app for a testing environment +func NewTest() App { + return App{ + Clock: clock.NewMock(), + EmailBackend: &testutils.MockEmailbackendImplementation{}, + HTTP500Page: assets.MustGetHTTP500ErrorPage(), + BaseURL: "http://127.0.0.0.1", + Port: "3000", + DisableRegistration: false, + DBPath: "", + AssetBaseURL: "", + } +} diff --git a/pkg/server/app/users.go b/pkg/server/app/users.go new file mode 100644 index 00000000..9c167b69 --- /dev/null +++ b/pkg/server/app/users.go @@ -0,0 +1,214 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "errors" + + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/helpers" + "github.com/dnote/dnote/pkg/server/log" + pkgErrors "github.com/pkg/errors" + "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" +) + +// validatePassword validates a password +func validatePassword(password string) error { + if len(password) < 8 { + return ErrPasswordTooShort + } + + return nil +} + +// TouchLastLoginAt updates the last login timestamp +func (a *App) TouchLastLoginAt(user database.User, tx *gorm.DB) error { + t := a.Clock.Now() + if err := tx.Model(&user).Update("last_login_at", &t).Error; err != nil { + return pkgErrors.Wrap(err, "updating last_login_at") + } + + return nil +} + +// CreateUser creates a user +func (a *App) CreateUser(email, password string, passwordConfirmation string) (database.User, error) { + if email == "" { + return database.User{}, ErrEmailRequired + } + + if err := validatePassword(password); err != nil { + return database.User{}, err + } + + if password != passwordConfirmation { + return database.User{}, ErrPasswordConfirmationMismatch + } + + tx := a.DB.Begin() + + var count int64 + if err := tx.Model(&database.User{}).Where("email = ?", email).Count(&count).Error; err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "counting user") + } + if count > 0 { + tx.Rollback() + return database.User{}, ErrDuplicateEmail + } + + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost) + if err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "hashing password") + } + + uuid, err := helpers.GenUUID() + if err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "generating UUID") + } + + user := database.User{ + UUID: uuid, + Email: database.ToNullString(email), + Password: database.ToNullString(string(hashedPassword)), + } + if err = tx.Save(&user).Error; err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "saving user") + } + + if err := a.TouchLastLoginAt(user, tx); err != nil { + tx.Rollback() + return database.User{}, pkgErrors.Wrap(err, "updating last login") + } + + tx.Commit() + + return user, nil +} + +// GetUserByEmail finds a user by email +func (a *App) GetUserByEmail(email string) (*database.User, error) { + var user database.User + err := a.DB.Where("email = ?", email).First(&user).Error + if errors.Is(err, gorm.ErrRecordNotFound) { + return nil, ErrNotFound + } else if err != nil { + return nil, err + } + + return &user, nil +} + +// GetAllUsers retrieves all users from the database +func (a *App) GetAllUsers() ([]database.User, error) { + var users []database.User + err := a.DB.Find(&users).Error + if err != nil { + return nil, pkgErrors.Wrap(err, "finding users") + } + + return users, nil +} + +// Authenticate authenticates a user +func (a *App) Authenticate(email, password string) (*database.User, error) { + user, err := a.GetUserByEmail(email) + if err != nil { + return nil, err + } + + err = bcrypt.CompareHashAndPassword([]byte(user.Password.String), []byte(password)) + if err != nil { + return nil, ErrLoginInvalid + } + + return user, nil +} + +// UpdateUserPassword updates a user's password with validation +func UpdateUserPassword(db *gorm.DB, user *database.User, newPassword string) error { + // Validate password + if err := validatePassword(newPassword); err != nil { + return err + } + + // Hash the password + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) + if err != nil { + return pkgErrors.Wrap(err, "hashing password") + } + + // Update the password + if err := db.Model(&user).Update("password", string(hashedPassword)).Error; err != nil { + return pkgErrors.Wrap(err, "updating password") + } + + return nil +} + +// RemoveUser removes a user from the system +// Returns an error if the user has any notes or books +func (a *App) RemoveUser(email string) error { + // Find the user + user, err := a.GetUserByEmail(email) + if err != nil { + return err + } + + // Check if user has any notes + var noteCount int64 + if err := a.DB.Model(&database.Note{}).Where("user_id = ? AND deleted = ?", user.ID, false).Count(¬eCount).Error; err != nil { + return pkgErrors.Wrap(err, "counting notes") + } + if noteCount > 0 { + return ErrUserHasExistingResources + } + + // Check if user has any books + var bookCount int64 + if err := a.DB.Model(&database.Book{}).Where("user_id = ? AND deleted = ?", user.ID, false).Count(&bookCount).Error; err != nil { + return pkgErrors.Wrap(err, "counting books") + } + if bookCount > 0 { + return ErrUserHasExistingResources + } + + // Delete user + if err := a.DB.Delete(&user).Error; err != nil { + return pkgErrors.Wrap(err, "deleting user") + } + + return nil +} + +// SignIn signs in a user +func (a *App) SignIn(user *database.User) (*database.Session, error) { + err := a.TouchLastLoginAt(*user, a.DB) + if err != nil { + log.ErrorWrap(err, "touching login timestamp") + } + + session, err := a.CreateSession(user.ID) + if err != nil { + return nil, pkgErrors.Wrap(err, "creating session") + } + + return &session, nil +} diff --git a/pkg/server/app/users_test.go b/pkg/server/app/users_test.go new file mode 100644 index 00000000..52183520 --- /dev/null +++ b/pkg/server/app/users_test.go @@ -0,0 +1,423 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package app + +import ( + "testing" + + "github.com/dnote/dnote/pkg/assert" + "github.com/dnote/dnote/pkg/server/database" + "github.com/dnote/dnote/pkg/server/testutils" + "github.com/pkg/errors" + "golang.org/x/crypto/bcrypt" +) + +func TestValidatePassword(t *testing.T) { + testCases := []struct { + name string + password string + wantErr error + }{ + { + name: "valid password", + password: "password123", + wantErr: nil, + }, + { + name: "valid password exactly 8 chars", + password: "12345678", + wantErr: nil, + }, + { + name: "password too short", + password: "1234567", + wantErr: ErrPasswordTooShort, + }, + { + name: "empty password", + password: "", + wantErr: ErrPasswordTooShort, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + err := validatePassword(tc.password) + assert.Equal(t, err, tc.wantErr, "error mismatch") + }) + } +} + +func TestCreateUser_ProValue(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + if _, err := a.CreateUser("alice@example.com", "pass1234", "pass1234"); err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + var userCount int64 + var userRecord database.User + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + testutils.MustExec(t, db.First(&userRecord), "finding user") + + assert.Equal(t, userCount, int64(1), "book count mismatch") + +} + +func TestGetUserByEmail(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + a := NewTest() + a.DB = db + + foundUser, err := a.GetUserByEmail("alice@example.com") + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, foundUser.Email.String, "alice@example.com", "email mismatch") + assert.Equal(t, foundUser.ID, user.ID, "user ID mismatch") + }) + + t.Run("not found", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + + user, err := a.GetUserByEmail("nonexistent@example.com") + + assert.Equal(t, err, ErrNotFound, "should return ErrNotFound") + assert.Equal(t, user, (*database.User)(nil), "user should be nil") + }) +} + +func TestGetAllUsers(t *testing.T) { + t.Run("success with multiple users", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user1 := testutils.SetupUserData(db, "alice@example.com", "password123") + user2 := testutils.SetupUserData(db, "bob@example.com", "password123") + user3 := testutils.SetupUserData(db, "charlie@example.com", "password123") + + a := NewTest() + a.DB = db + + users, err := a.GetAllUsers() + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, len(users), 3, "should return 3 users") + + // Verify all users are returned + emails := make(map[string]bool) + for _, user := range users { + emails[user.Email.String] = true + } + assert.Equal(t, emails["alice@example.com"], true, "alice should be in results") + assert.Equal(t, emails["bob@example.com"], true, "bob should be in results") + assert.Equal(t, emails["charlie@example.com"], true, "charlie should be in results") + + // Verify user details match + for _, user := range users { + if user.Email.String == "alice@example.com" { + assert.Equal(t, user.ID, user1.ID, "alice ID mismatch") + } else if user.Email.String == "bob@example.com" { + assert.Equal(t, user.ID, user2.ID, "bob ID mismatch") + } else if user.Email.String == "charlie@example.com" { + assert.Equal(t, user.ID, user3.ID, "charlie ID mismatch") + } + } + }) + + t.Run("empty database", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + + users, err := a.GetAllUsers() + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, len(users), 0, "should return 0 users") + }) + + t.Run("single user", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + a := NewTest() + a.DB = db + + users, err := a.GetAllUsers() + + assert.Equal(t, err, nil, "should not error") + assert.Equal(t, len(users), 1, "should return 1 user") + assert.Equal(t, users[0].Email.String, "alice@example.com", "email mismatch") + assert.Equal(t, users[0].ID, user.ID, "user ID mismatch") + }) +} + +func TestCreateUser(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + if _, err := a.CreateUser("alice@example.com", "pass1234", "pass1234"); err != nil { + t.Fatal(errors.Wrap(err, "executing")) + } + + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + assert.Equal(t, userCount, int64(1), "user count mismatch") + + var userRecord database.User + testutils.MustExec(t, db.First(&userRecord), "finding user") + + assert.Equal(t, userRecord.Email.String, "alice@example.com", "user email mismatch") + + passwordErr := bcrypt.CompareHashAndPassword([]byte(userRecord.Password.String), []byte("pass1234")) + assert.Equal(t, passwordErr, nil, "Password mismatch") + }) + + t.Run("duplicate email", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + testutils.SetupUserData(db, "alice@example.com", "somepassword") + + a := NewTest() + a.DB = db + _, err := a.CreateUser("alice@example.com", "newpassword", "newpassword") + + assert.Equal(t, err, ErrDuplicateEmail, "error mismatch") + + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting user") + + assert.Equal(t, userCount, int64(1), "user count mismatch") + }) +} + +func TestUpdateUserPassword(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + err := UpdateUserPassword(db, &user, "newpassword123") + + assert.Equal(t, err, nil, "should not error") + + // Verify password was updated in database + var updatedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&updatedUser), "finding updated user") + + // Verify new password works + passwordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("newpassword123")) + assert.Equal(t, passwordErr, nil, "New password should match") + + // Verify old password no longer works + oldPasswordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("oldpassword123")) + assert.NotEqual(t, oldPasswordErr, nil, "Old password should not match") + }) + + t.Run("password too short", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + err := UpdateUserPassword(db, &user, "short") + + assert.Equal(t, err, ErrPasswordTooShort, "should return ErrPasswordTooShort") + + // Verify password was NOT updated in database + var unchangedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") + + // Verify old password still works + passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, passwordErr, nil, "Old password should still match") + }) + + t.Run("empty password", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + err := UpdateUserPassword(db, &user, "") + + assert.Equal(t, err, ErrPasswordTooShort, "should return ErrPasswordTooShort") + + // Verify password was NOT updated in database + var unchangedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") + + // Verify old password still works + passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, passwordErr, nil, "Old password should still match") + }) + + t.Run("transaction rollback", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + // Start a transaction and rollback to verify UpdateUserPassword respects transactions + tx := db.Begin() + err := UpdateUserPassword(tx, &user, "newpassword123") + assert.Equal(t, err, nil, "should not error") + tx.Rollback() + + // Verify password was NOT updated after rollback + var unchangedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") + + // Verify old password still works + passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) + assert.Equal(t, passwordErr, nil, "Old password should still match after rollback") + }) + + t.Run("transaction commit", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") + + // Start a transaction and commit to verify UpdateUserPassword respects transactions + tx := db.Begin() + err := UpdateUserPassword(tx, &user, "newpassword123") + assert.Equal(t, err, nil, "should not error") + tx.Commit() + + // Verify password was updated after commit + var updatedUser database.User + testutils.MustExec(t, db.Where("id = ?", user.ID).First(&updatedUser), "finding updated user") + + // Verify new password works + passwordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("newpassword123")) + assert.Equal(t, passwordErr, nil, "New password should match after commit") + }) +} + +func TestRemoveUser(t *testing.T) { + t.Run("success", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + testutils.SetupUserData(db, "alice@example.com", "password123") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, nil, "should not error") + + // Verify user was deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(0), "user should be deleted") + }) + + t.Run("user not found", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + a := NewTest() + a.DB = db + + err := a.RemoveUser("nonexistent@example.com") + + assert.Equal(t, err, ErrNotFound, "should return ErrNotFound") + }) + + t.Run("user has notes", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + book := database.Book{UserID: user.ID, Label: "testbook", Deleted: false} + testutils.MustExec(t, db.Save(&book), "creating book") + + note := database.Note{UserID: user.ID, BookUUID: book.UUID, Body: "test note", Deleted: false} + testutils.MustExec(t, db.Save(¬e), "creating note") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, ErrUserHasExistingResources, "should return ErrUserHasExistingResources") + + // Verify user was NOT deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(1), "user should not be deleted") + + }) + + t.Run("user has books", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + book := database.Book{UserID: user.ID, Label: "testbook", Deleted: false} + testutils.MustExec(t, db.Save(&book), "creating book") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, ErrUserHasExistingResources, "should return ErrUserHasExistingResources") + + // Verify user was NOT deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(1), "user should not be deleted") + + }) + + t.Run("user has deleted notes and books", func(t *testing.T) { + db := testutils.InitMemoryDB(t) + + user := testutils.SetupUserData(db, "alice@example.com", "password123") + + book := database.Book{UserID: user.ID, Label: "testbook", Deleted: false} + testutils.MustExec(t, db.Save(&book), "creating book") + + note := database.Note{UserID: user.ID, BookUUID: book.UUID, Body: "test note", Deleted: false} + testutils.MustExec(t, db.Save(¬e), "creating note") + + // Soft delete the note and book + testutils.MustExec(t, db.Model(¬e).Update("deleted", true), "soft deleting note") + testutils.MustExec(t, db.Model(&book).Update("deleted", true), "soft deleting book") + + a := NewTest() + a.DB = db + + err := a.RemoveUser("alice@example.com") + + assert.Equal(t, err, nil, "should not error when user only has deleted notes and books") + + // Verify user was deleted + var userCount int64 + testutils.MustExec(t, db.Model(&database.User{}).Count(&userCount), "counting users") + assert.Equal(t, userCount, int64(0), "user should be deleted") + + }) +} diff --git a/pkg/server/assets/.gitignore b/pkg/server/assets/.gitignore new file mode 100644 index 00000000..7ba54dae --- /dev/null +++ b/pkg/server/assets/.gitignore @@ -0,0 +1,7 @@ +# Ignore CSS and JS files in static directory because +# those files are built from the sources + +/static/*.css +/static/*.css.map +/static/*.js +/static/*.js.map diff --git a/pkg/server/assets/embed.go b/pkg/server/assets/embed.go new file mode 100644 index 00000000..e0272d95 --- /dev/null +++ b/pkg/server/assets/embed.go @@ -0,0 +1,46 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package assets + +import ( + "embed" + "github.com/pkg/errors" + "io/fs" +) + +//go:embed static +var staticFiles embed.FS + +// GetStaticFS returns a filesystem for static files, with +// all files situated in the root of the filesystem +func GetStaticFS() (fs.FS, error) { + subFs, err := fs.Sub(staticFiles, "static") + if err != nil { + return nil, errors.Wrap(err, "getting sub filesystem") + } + + return subFs, nil +} + +// MustGetHTTP500ErrorPage returns the content of HTML file for HTTP 500 error +func MustGetHTTP500ErrorPage() []byte { + ret, err := staticFiles.ReadFile("static/500.html") + if err != nil { + panic(errors.Wrap(err, "reading HTML file for 500 HTTP error")) + } + + return ret +} diff --git a/pkg/server/assets/js/build.sh b/pkg/server/assets/js/build.sh new file mode 100755 index 00000000..f00ddf3c --- /dev/null +++ b/pkg/server/assets/js/build.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# build.sh builds styles +set -ex + +dir=$(dirname "${BASH_SOURCE[0]}") +basePath="$dir/../../.." +serverDir="$dir/../.." +outputDir="$serverDir/assets/static" +inputDir="$dir/src" + +task="cp $inputDir/main.js $outputDir" + + +if [[ "$1" == "true" ]]; then +( + cd "$basePath/watcher" && \ + go run main.go \ + --task="$task" \ + --context="$inputDir" \ + "$inputDir" +) +else + eval "$task" +fi diff --git a/pkg/server/assets/js/src/main.js b/pkg/server/assets/js/src/main.js new file mode 100644 index 00000000..9e58d92a --- /dev/null +++ b/pkg/server/assets/js/src/main.js @@ -0,0 +1,74 @@ +/* Copyright 2025 Dnote Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var getNextSibling = function (el, selector) { + var sibling = el.nextElementSibling; + + if (!selector) { + return sibling; + } + + while (sibling) { + if (sibling.matches(selector)) return sibling; + sibling = sibling.nextElementSibling; + } +}; + +var dropdownTriggerEls = document.getElementsByClassName('dropdown-trigger'); + +for (var i = 0; i < dropdownTriggerEls.length; i++) { + var dropdownTriggerEl = dropdownTriggerEls[i]; + + dropdownTriggerEl.addEventListener('click', function (e) { + var el = getNextSibling(e.target, '.dropdown-content'); + + el.classList.toggle('show'); + }); +} + +// Dropdown closer +window.onclick = function (e) { + // Close dropdown on click outside the dropdown content or trigger + function shouldClose(target) { + var dropdownContentEls = document.getElementsByClassName( + 'dropdown-content' + ); + + for (let i = 0; i < dropdownContentEls.length; ++i) { + var el = dropdownContentEls[i]; + if (el.contains(target)) { + return false; + } + } + for (let i = 0; i < dropdownTriggerEls.length; ++i) { + var el = dropdownTriggerEls[i]; + if (el.contains(target)) { + return false; + } + } + + return true; + } + + if (shouldClose(e.target)) { + var dropdowns = document.getElementsByClassName('dropdown-content'); + for (var i = 0; i < dropdowns.length; i++) { + var openDropdown = dropdowns[i]; + if (openDropdown.classList.contains('show')) { + openDropdown.classList.remove('show'); + } + } + } +}; diff --git a/pkg/server/assets/package-lock.json b/pkg/server/assets/package-lock.json new file mode 100644 index 00000000..7eb1c411 --- /dev/null +++ b/pkg/server/assets/package-lock.json @@ -0,0 +1,495 @@ +{ + "name": "assets", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "assets", + "version": "1.0.0", + "license": "Apache-2.0", + "devDependencies": { + "sass": "^1.50.1" + } + }, + "node_modules/@parcel/watcher": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher/-/watcher-2.5.1.tgz", + "integrity": "sha512-dfUnCxiN9H4ap84DvD2ubjw+3vUNpstxa0TneY/Paat8a3R4uQZDLSvWjmznAY/DoahqTHl9V46HF/Zs3F29pg==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "dependencies": { + "detect-libc": "^1.0.3", + "is-glob": "^4.0.3", + "micromatch": "^4.0.5", + "node-addon-api": "^7.0.0" + }, + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "@parcel/watcher-android-arm64": "2.5.1", + "@parcel/watcher-darwin-arm64": "2.5.1", + "@parcel/watcher-darwin-x64": "2.5.1", + "@parcel/watcher-freebsd-x64": "2.5.1", + "@parcel/watcher-linux-arm-glibc": "2.5.1", + "@parcel/watcher-linux-arm-musl": "2.5.1", + "@parcel/watcher-linux-arm64-glibc": "2.5.1", + "@parcel/watcher-linux-arm64-musl": "2.5.1", + "@parcel/watcher-linux-x64-glibc": "2.5.1", + "@parcel/watcher-linux-x64-musl": "2.5.1", + "@parcel/watcher-win32-arm64": "2.5.1", + "@parcel/watcher-win32-ia32": "2.5.1", + "@parcel/watcher-win32-x64": "2.5.1" + } + }, + "node_modules/@parcel/watcher-android-arm64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-android-arm64/-/watcher-android-arm64-2.5.1.tgz", + "integrity": "sha512-KF8+j9nNbUN8vzOFDpRMsaKBHZ/mcjEjMToVMJOhTozkDonQFFrRcfdLWn6yWKCmJKmdVxSgHiYvTCef4/qcBA==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-darwin-arm64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-darwin-arm64/-/watcher-darwin-arm64-2.5.1.tgz", + "integrity": "sha512-eAzPv5osDmZyBhou8PoF4i6RQXAfeKL9tjb3QzYuccXFMQU0ruIc/POh30ePnaOyD1UXdlKguHBmsTs53tVoPw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-darwin-x64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-darwin-x64/-/watcher-darwin-x64-2.5.1.tgz", + "integrity": "sha512-1ZXDthrnNmwv10A0/3AJNZ9JGlzrF82i3gNQcWOzd7nJ8aj+ILyW1MTxVk35Db0u91oD5Nlk9MBiujMlwmeXZg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-freebsd-x64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-freebsd-x64/-/watcher-freebsd-x64-2.5.1.tgz", + "integrity": "sha512-SI4eljM7Flp9yPuKi8W0ird8TI/JK6CSxju3NojVI6BjHsTyK7zxA9urjVjEKJ5MBYC+bLmMcbAWlZ+rFkLpJQ==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm-glibc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm-glibc/-/watcher-linux-arm-glibc-2.5.1.tgz", + "integrity": "sha512-RCdZlEyTs8geyBkkcnPWvtXLY44BCeZKmGYRtSgtwwnHR4dxfHRG3gR99XdMEdQ7KeiDdasJwwvNSF5jKtDwdA==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm-musl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm-musl/-/watcher-linux-arm-musl-2.5.1.tgz", + "integrity": "sha512-6E+m/Mm1t1yhB8X412stiKFG3XykmgdIOqhjWj+VL8oHkKABfu/gjFj8DvLrYVHSBNC+/u5PeNrujiSQ1zwd1Q==", + "cpu": [ + "arm" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm64-glibc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm64-glibc/-/watcher-linux-arm64-glibc-2.5.1.tgz", + "integrity": "sha512-LrGp+f02yU3BN9A+DGuY3v3bmnFUggAITBGriZHUREfNEzZh/GO06FF5u2kx8x+GBEUYfyTGamol4j3m9ANe8w==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-arm64-musl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-arm64-musl/-/watcher-linux-arm64-musl-2.5.1.tgz", + "integrity": "sha512-cFOjABi92pMYRXS7AcQv9/M1YuKRw8SZniCDw0ssQb/noPkRzA+HBDkwmyOJYp5wXcsTrhxO0zq1U11cK9jsFg==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-x64-glibc": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-x64-glibc/-/watcher-linux-x64-glibc-2.5.1.tgz", + "integrity": "sha512-GcESn8NZySmfwlTsIur+49yDqSny2IhPeZfXunQi48DMugKeZ7uy1FX83pO0X22sHntJ4Ub+9k34XQCX+oHt2A==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-linux-x64-musl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-linux-x64-musl/-/watcher-linux-x64-musl-2.5.1.tgz", + "integrity": "sha512-n0E2EQbatQ3bXhcH2D1XIAANAcTZkQICBPVaxMeaCVBtOpBZpWJuf7LwyWPSBDITb7In8mqQgJ7gH8CILCURXg==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-arm64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-arm64/-/watcher-win32-arm64-2.5.1.tgz", + "integrity": "sha512-RFzklRvmc3PkjKjry3hLF9wD7ppR4AKcWNzH7kXR7GUe0Igb3Nz8fyPwtZCSquGrhU5HhUNDr/mKBqj7tqA2Vw==", + "cpu": [ + "arm64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-ia32": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-ia32/-/watcher-win32-ia32-2.5.1.tgz", + "integrity": "sha512-c2KkcVN+NJmuA7CGlaGD1qJh1cLfDnQsHjE89E60vUEMlqduHGCdCLJCID5geFVM0dOtA3ZiIO8BoEQmzQVfpQ==", + "cpu": [ + "ia32" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/@parcel/watcher-win32-x64": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/@parcel/watcher-win32-x64/-/watcher-win32-x64-2.5.1.tgz", + "integrity": "sha512-9lHBdJITeNR++EvSQVUcaZoWupyHfXe1jZvGZ06O/5MflPcuPLtEphScIBL+AiCWBO46tDSHzWyD0uDmmZqsgA==", + "cpu": [ + "x64" + ], + "dev": true, + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 10.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, + "optional": true, + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/chokidar": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-4.0.3.tgz", + "integrity": "sha512-Qgzu8kfBvo+cA4962jnP1KkS6Dop5NS6g7R5LFYJr4b8Ub94PPQXUksCw9PvXoeXPRRddRNC5C1JQUR2SMGtnA==", + "dev": true, + "dependencies": { + "readdirp": "^4.0.1" + }, + "engines": { + "node": ">= 14.16.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/detect-libc": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-1.0.3.tgz", + "integrity": "sha512-pGjwhsmsp4kL2RTz08wcOlGN83otlqHeD/Z5T8GXZB+/YcpQ/dgo+lbU8ZsGxV0HIvqqxo9l7mqYwyYMD9bKDg==", + "dev": true, + "optional": true, + "bin": { + "detect-libc": "bin/detect-libc.js" + }, + "engines": { + "node": ">=0.10" + } + }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, + "optional": true, + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/immutable": { + "version": "5.1.5", + "resolved": "https://registry.npmjs.org/immutable/-/immutable-5.1.5.tgz", + "integrity": "sha512-t7xcm2siw+hlUM68I+UEOK+z84RzmN59as9DZ7P1l0994DKUWV7UXBMQZVxaoMSRQ+PBZbHCOoBt7a2wxOMt+A==", + "dev": true, + "license": "MIT" + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "optional": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "optional": true, + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "optional": true, + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/micromatch": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", + "dev": true, + "optional": true, + "dependencies": { + "braces": "^3.0.3", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=8.6" + } + }, + "node_modules/node-addon-api": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-7.1.1.tgz", + "integrity": "sha512-5m3bsyrjFWE1xf7nz7YXdN4udnVtXK6/Yfgn5qnahL6bCkf2yKt4k3nuTKAtT4r3IG8JNR2ncsIMdZuAzJjHQQ==", + "dev": true, + "optional": true + }, + "node_modules/picomatch": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz", + "integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==", + "dev": true, + "optional": true, + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/readdirp": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-4.1.2.tgz", + "integrity": "sha512-GDhwkLfywWL2s6vEjyhri+eXmfH6j1L7JE27WhqLeYzoh/A3DBaYGEj2H/HFZCn/kMfim73FXxEJTw06WtxQwg==", + "dev": true, + "engines": { + "node": ">= 14.18.0" + }, + "funding": { + "type": "individual", + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/sass": { + "version": "1.93.2", + "resolved": "https://registry.npmjs.org/sass/-/sass-1.93.2.tgz", + "integrity": "sha512-t+YPtOQHpGW1QWsh1CHQ5cPIr9lbbGZLZnbihP/D/qZj/yuV68m8qarcV17nvkOX81BCrvzAlq2klCQFZghyTg==", + "dev": true, + "dependencies": { + "chokidar": "^4.0.0", + "immutable": "^5.0.2", + "source-map-js": ">=0.6.2 <2.0.0" + }, + "bin": { + "sass": "sass.js" + }, + "engines": { + "node": ">=14.0.0" + }, + "optionalDependencies": { + "@parcel/watcher": "^2.4.1" + } + }, + "node_modules/source-map-js": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.0.2.tgz", + "integrity": "sha512-R0XvVJ9WusLiqTCEiGCmICCMplcCkIwwR11mOSD9CR5u+IXYdiseeEuXCVAjS54zqwkLcPNnmU4OeJ6tUrWhDw==", + "dev": true, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "optional": true, + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + } + } +} diff --git a/pkg/server/assets/package.json b/pkg/server/assets/package.json new file mode 100644 index 00000000..1561f6da --- /dev/null +++ b/pkg/server/assets/package.json @@ -0,0 +1,12 @@ +{ + "name": "assets", + "version": "1.0.0", + "description": "assets", + "main": "index.js", + "scripts": {}, + "author": "Dnote", + "license": "Apache-2.0", + "devDependencies": { + "sass": "^1.50.1" + } +} diff --git a/pkg/server/assets/static/500.html b/pkg/server/assets/static/500.html new file mode 100644 index 00000000..ccf85d20 --- /dev/null +++ b/pkg/server/assets/static/500.html @@ -0,0 +1,10 @@ + + + + + + + + 500 + + diff --git a/pkg/server/assets/static/android-icon-144x144.png b/pkg/server/assets/static/android-icon-144x144.png new file mode 100644 index 00000000..09b28d2f Binary files /dev/null and b/pkg/server/assets/static/android-icon-144x144.png differ diff --git a/pkg/server/assets/static/android-icon-192x192.png b/pkg/server/assets/static/android-icon-192x192.png new file mode 100644 index 00000000..48195236 Binary files /dev/null and b/pkg/server/assets/static/android-icon-192x192.png differ diff --git a/pkg/server/assets/static/android-icon-36x36.png b/pkg/server/assets/static/android-icon-36x36.png new file mode 100644 index 00000000..7c9f770f Binary files /dev/null and b/pkg/server/assets/static/android-icon-36x36.png differ diff --git a/pkg/server/assets/static/android-icon-48x48.png b/pkg/server/assets/static/android-icon-48x48.png new file mode 100644 index 00000000..d93d8cda Binary files /dev/null and b/pkg/server/assets/static/android-icon-48x48.png differ diff --git a/pkg/server/assets/static/android-icon-72x72.png b/pkg/server/assets/static/android-icon-72x72.png new file mode 100644 index 00000000..aa2e9876 Binary files /dev/null and b/pkg/server/assets/static/android-icon-72x72.png differ diff --git a/pkg/server/assets/static/android-icon-96x96.png b/pkg/server/assets/static/android-icon-96x96.png new file mode 100644 index 00000000..6d711b98 Binary files /dev/null and b/pkg/server/assets/static/android-icon-96x96.png differ diff --git a/pkg/server/assets/static/apple-icon-114x114.png b/pkg/server/assets/static/apple-icon-114x114.png new file mode 100644 index 00000000..e6b7fece Binary files /dev/null and b/pkg/server/assets/static/apple-icon-114x114.png differ diff --git a/pkg/server/assets/static/apple-icon-120x120.png b/pkg/server/assets/static/apple-icon-120x120.png new file mode 100644 index 00000000..871eb9d0 Binary files /dev/null and b/pkg/server/assets/static/apple-icon-120x120.png differ diff --git a/pkg/server/assets/static/apple-icon-144x144.png b/pkg/server/assets/static/apple-icon-144x144.png new file mode 100644 index 00000000..09b28d2f Binary files /dev/null and b/pkg/server/assets/static/apple-icon-144x144.png differ diff --git a/pkg/server/assets/static/apple-icon-152x152.png b/pkg/server/assets/static/apple-icon-152x152.png new file mode 100644 index 00000000..16a2dd70 Binary files /dev/null and b/pkg/server/assets/static/apple-icon-152x152.png differ diff --git a/pkg/server/assets/static/apple-icon-180x180.png b/pkg/server/assets/static/apple-icon-180x180.png new file mode 100644 index 00000000..77d31532 Binary files /dev/null and b/pkg/server/assets/static/apple-icon-180x180.png differ diff --git a/pkg/server/assets/static/apple-icon-57x57.png b/pkg/server/assets/static/apple-icon-57x57.png new file mode 100644 index 00000000..ca17d420 Binary files /dev/null and b/pkg/server/assets/static/apple-icon-57x57.png differ diff --git a/pkg/server/assets/static/apple-icon-60x60.png b/pkg/server/assets/static/apple-icon-60x60.png new file mode 100644 index 00000000..9dbf917e Binary files /dev/null and b/pkg/server/assets/static/apple-icon-60x60.png differ diff --git a/pkg/server/assets/static/apple-icon-72x72.png b/pkg/server/assets/static/apple-icon-72x72.png new file mode 100644 index 00000000..aa2e9876 Binary files /dev/null and b/pkg/server/assets/static/apple-icon-72x72.png differ diff --git a/pkg/server/assets/static/apple-icon-76x76.png b/pkg/server/assets/static/apple-icon-76x76.png new file mode 100644 index 00000000..5e4af4bc Binary files /dev/null and b/pkg/server/assets/static/apple-icon-76x76.png differ diff --git a/pkg/server/assets/static/apple-icon-precomposed.png b/pkg/server/assets/static/apple-icon-precomposed.png new file mode 100644 index 00000000..f3410b4e Binary files /dev/null and b/pkg/server/assets/static/apple-icon-precomposed.png differ diff --git a/pkg/server/assets/static/apple-icon.png b/pkg/server/assets/static/apple-icon.png new file mode 100644 index 00000000..f3410b4e Binary files /dev/null and b/pkg/server/assets/static/apple-icon.png differ diff --git a/web/static/browserconfig.xml b/pkg/server/assets/static/browserconfig.xml similarity index 100% rename from web/static/browserconfig.xml rename to pkg/server/assets/static/browserconfig.xml diff --git a/pkg/server/assets/static/favicon-16x16.png b/pkg/server/assets/static/favicon-16x16.png new file mode 100644 index 00000000..798fbd5c Binary files /dev/null and b/pkg/server/assets/static/favicon-16x16.png differ diff --git a/pkg/server/assets/static/favicon-32x32.png b/pkg/server/assets/static/favicon-32x32.png new file mode 100644 index 00000000..a1be50c4 Binary files /dev/null and b/pkg/server/assets/static/favicon-32x32.png differ diff --git a/pkg/server/assets/static/favicon-96x96.png b/pkg/server/assets/static/favicon-96x96.png new file mode 100644 index 00000000..6d711b98 Binary files /dev/null and b/pkg/server/assets/static/favicon-96x96.png differ diff --git a/pkg/server/assets/static/favicon.ico b/pkg/server/assets/static/favicon.ico new file mode 100644 index 00000000..c71572aa Binary files /dev/null and b/pkg/server/assets/static/favicon.ico differ diff --git a/pkg/server/assets/static/logo-512x512.png b/pkg/server/assets/static/logo-512x512.png new file mode 100644 index 00000000..141992ac Binary files /dev/null and b/pkg/server/assets/static/logo-512x512.png differ diff --git a/pkg/server/assets/static/manifest.json b/pkg/server/assets/static/manifest.json new file mode 100644 index 00000000..874b25a6 --- /dev/null +++ b/pkg/server/assets/static/manifest.json @@ -0,0 +1,52 @@ +{ + "name": "Dnote", + "short_name": "Dnote", + "icons": [ + { + "src": "ASSET_BASE_PLACEHOLDER\/android-icon-36x36.png", + "sizes": "36x36", + "type": "image\/png", + "density": "0.75" + }, + { + "src": "ASSET_BASE_PLACEHOLDER\/android-icon-48x48.png", + "sizes": "48x48", + "type": "image\/png", + "density": "1.0" + }, + { + "src": "ASSET_BASE_PLACEHOLDER\/android-icon-72x72.png", + "sizes": "72x72", + "type": "image\/png", + "density": "1.5" + }, + { + "src": "ASSET_BASE_PLACEHOLDER\/android-icon-96x96.png", + "sizes": "96x96", + "type": "image\/png", + "density": "2.0" + }, + { + "src": "ASSET_BASE_PLACEHOLDER\/android-icon-144x144.png", + "sizes": "144x144", + "type": "image\/png", + "density": "3.0" + }, + { + "src": "ASSET_BASE_PLACEHOLDER\/android-icon-192x192.png", + "sizes": "192x192", + "type": "image\/png", + "density": "4.0" + }, + { + "src": "ASSET_BASE_PLACEHOLDER\/logo-512x512.png", + "sizes": "512x512", + "type": "image\/png", + "density": "4.0" + } + ], + "start_url": "/", + "display": "standalone", + "background_color": "#072a40", + "theme_color": "#ffffff" +} diff --git a/pkg/server/assets/static/ms-icon-144x144.png b/pkg/server/assets/static/ms-icon-144x144.png new file mode 100644 index 00000000..09b28d2f Binary files /dev/null and b/pkg/server/assets/static/ms-icon-144x144.png differ diff --git a/pkg/server/assets/static/ms-icon-150x150.png b/pkg/server/assets/static/ms-icon-150x150.png new file mode 100644 index 00000000..0fa43061 Binary files /dev/null and b/pkg/server/assets/static/ms-icon-150x150.png differ diff --git a/pkg/server/assets/static/ms-icon-310x310.png b/pkg/server/assets/static/ms-icon-310x310.png new file mode 100644 index 00000000..c8974c78 Binary files /dev/null and b/pkg/server/assets/static/ms-icon-310x310.png differ diff --git a/pkg/server/assets/static/ms-icon-70x70.png b/pkg/server/assets/static/ms-icon-70x70.png new file mode 100644 index 00000000..78fdbd5a Binary files /dev/null and b/pkg/server/assets/static/ms-icon-70x70.png differ diff --git a/web/static/404.html b/pkg/server/assets/static/offline.html similarity index 71% rename from web/static/404.html rename to pkg/server/assets/static/offline.html index 84fff815..0aadab72 100644 --- a/web/static/404.html +++ b/pkg/server/assets/static/offline.html @@ -5,10 +5,8 @@ Page Not Found | Dnote - - - - - - - - You have requested to reset your password on Dnote. - - {{ template "header" }} - - - - - - - - - - - Hello. - - - - - Please click the link below to verify your email. This link will expire in 30 minutes. - - - - - - - - - - - - - - - - Verify email - - - - - - - - - - - - - - Alternatively you can manually go to the following URL: https://dnote.io/app/verify-email/{{ .Token }} - - - - - - — Dnote - - - - - - - - - - - {{ template "footer" . }} - - - - - - - - - -