Compare commits

...

23 commits

Author SHA1 Message Date
dependabot[bot]
f34a96abbe
Bump immutable from 5.1.3 to 5.1.5 in /pkg/server/assets (#718)
Bumps [immutable](https://github.com/immutable-js/immutable-js) from 5.1.3 to 5.1.5.
- [Release notes](https://github.com/immutable-js/immutable-js/releases)
- [Changelog](https://github.com/immutable-js/immutable-js/blob/main/CHANGELOG.md)
- [Commits](https://github.com/immutable-js/immutable-js/compare/v5.1.3...v5.1.5)

---
updated-dependencies:
- dependency-name: immutable
  dependency-version: 5.1.5
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-03-04 22:11:45 -08:00
dependabot[bot]
9fa312e3fc
Bump golang.org/x/crypto from 0.42.0 to 0.45.0 (#716)
Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.42.0 to 0.45.0.
- [Commits](https://github.com/golang/crypto/compare/v0.42.0...v0.45.0)

---
updated-dependencies:
- dependency-name: golang.org/x/crypto
  dependency-version: 0.45.0
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-12 21:48:59 -08:00
Sung
8f37d34df6
Remove ls and cat commands (#715) 2025-11-07 23:53:41 -08:00
Sung
5c416e3a32
Add user list command (#714) 2025-11-01 19:59:51 -07:00
Sung
d5e11c23f6
Update self-hosting doc (#713) 2025-11-01 19:20:18 -07:00
Sung
f1d7123596
Fix log level (#712) 2025-11-01 14:06:33 -07:00
Sung
ce5c9b242a
Rename webUrl to baseUrl (#711) 2025-11-01 00:57:37 -07:00
Sung
e72322f847
Simplify email backend and remove --appEnv (#710)
* Improve logging

* Remove AppEnv

* Simplify email backend
2025-11-01 00:54:27 -07:00
Sung
e0c4cb1545
Use Apache 2.0 license (#708) 2025-10-31 23:41:21 -07:00
Sung
6314749263
Improve empty server sync when multiple clients exist (#706)
* Fix dbPath

* Require full sync when after another client uploads to an empty server

* Avoid orphan notes with empty sync
2025-10-26 17:54:24 -07:00
Sung
ae290a226f
Auto vacuum and manage connections (#705)
* Test concurrent sync

* Auto vacuum and manage connection
2025-10-26 16:59:53 -07:00
Sung
a46afb821f
Fix an edge case of repeated syncs due to orphaned note (#704)
* Split sync test

* Reproduce a bug

* Fix a bug

* Fix in a more correct way

* Add debug logs
2025-10-26 11:43:17 -07:00
Sung
f6a4c6344c
Remove public from CLI (#703)
* Remove public from CLI

* Write migration and test

* Use in-memory db for a test server

* Simplify CLI test db teardown

* Restructure packages to reduce duplication
2025-10-25 20:51:57 -07:00
Sung
e3380a4dfa
Remove unused templates (#702) 2025-10-19 21:42:53 -07:00
Sung
0a5728faf3
Merge user and account (#701) 2025-10-19 21:05:47 -07:00
Sung
b03ca999a5
Remove the unused encrypted and public fields (#700)
* Remove encrypted fields from notes and books

* Remove public from notes

* Use consistent flags
2025-10-19 18:32:20 -07:00
Sung
83ac43b737
Specify DBPath for docker (#699) 2025-10-19 15:38:52 -07:00
Sung
505fc67966
Fix server release for freebsd (#698) 2025-10-19 14:57:29 -07:00
Sung
7d44c541a4
Add Docker images for linux arm64, armv7, 386 (#697)
* Add multi-platform Docker support for ARM64, ARMv7, and 386

* Support freebsd amd64 for server

* Build docker images locally
2025-10-19 14:30:55 -07:00
Sung
850f9cc6c9
Manage users with server CLI (#696) 2025-10-19 11:01:48 -07:00
Sung
41f25514f0
Link to the doc (#695) 2025-10-19 01:01:26 -07:00
Sung
03889a3d7e
Converge if using same book names while syncing (#694)
* Add healthcheck for Docker

* Prevent nil pointer if endpoint is wrong

* Converge if using same book names while syncing
2025-10-18 16:03:12 -07:00
Sung
c8238aa327
Handle errors (#693) 2025-10-12 16:17:01 -07:00
257 changed files with 8223 additions and 8284 deletions

View file

@ -55,10 +55,19 @@ jobs:
./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
@ -71,11 +80,12 @@ jobs:
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: |
tarballName=dnote_server_${{ steps.version.outputs.version }}_linux_amd64.tar.gz
version=${{ steps.version.outputs.version }}
- name: Create GitHub release
env:

2
.gitignore vendored
View file

@ -6,4 +6,4 @@ node_modules
/test
tmp
*.db
server
/server

209
LICENSE
View file

@ -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. Unless
otherwise noted, 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
Dnote contributors
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.

View file

@ -33,7 +33,7 @@ endif
test: test-cli test-api test-e2e
.PHONY: test
test-cli:
test-cli: generate-cli-schema
@echo "==> running CLI test"
@(${currentDir}/scripts/cli/test.sh)
.PHONY: test-cli
@ -67,7 +67,23 @@ endif
@${currentDir}/scripts/server/build.sh $(version)
.PHONY: build-server
build-cli:
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
@ -82,53 +98,6 @@ endif
endif
.PHONY: build-cli
## release
release-cli: clean build-cli
ifndef version
$(error version is required. Usage: make version=0.1.0 release-cli)
endif
ifndef GH
$(error please install github-cli)
endif
@echo "==> releasing cli"
@${currentDir}/scripts/release.sh cli $(version) ${cliOutputDir}
.PHONY: release-cli
release-cli-homebrew:
ifndef version
$(error version is required. Usage: make version=0.1.0 release-cli-homebrew)
endif
@echo "==> releasing cli on Homebrew"
@${currentDir}/scripts/cli/release-homebrew.sh $(version)
.PHONY: release-cli
release-server:
ifndef version
$(error version is required. Usage: make version=0.1.0 release-server)
endif
ifndef GH
$(error please install github-cli)
endif
@echo "==> releasing server"
@${currentDir}/scripts/release.sh server $(version) ${serverOutputDir}
@echo "==> building and releasing docker image"
@(cd ${currentDir}/host/docker && ./build.sh $(version))
@(cd ${currentDir}/host/docker && ./release.sh $(version))
.PHONY: release-server
# migrations
create-migration:
ifndef filename
$(error filename is required. Usage: make filename=your-filename create-migration)
endif
@(cd ${currentDir}/pkg/server/database && ./scripts/create-migration.sh $(filename))
.PHONY: create-migration
clean:
@git clean -f
@rm -rf build

View file

@ -35,9 +35,9 @@ Or [download binary](https://github.com/dnote/dnote/releases).
## Server (Optional)
Just run a binary. No database setup required.
Server is a binary with SQLite embedded. No database setup is required.
Run with Docker Compose using [compose.yml](./host/docker/compose.yml):
If using docker, create a compose.yml:
```yaml
services:
@ -51,8 +51,14 @@ services:
restart: unless-stopped
```
Or see the [guide](https://github.com/dnote/dnote/blob/master/SELF_HOSTING.md) for binary installation and configuration options.
Then run:
```bash
docker-compose up -d
```
Or see the [guide](https://www.getdnote.com/docs/server/manual) for binary installation.
## Documentation
See the [Dnote wiki](https://github.com/dnote/dnote/wiki) for full documentation.
See the [Dnote doc](https://www.getdnote.com/docs).

View file

@ -1,112 +1,43 @@
# Self-Hosting Dnote Server
For Docker installation, see [the Docker guide](https://github.com/dnote/dnote/blob/master/host/docker/README.md).
Please see the [doc](https://www.getdnote.com/docs/server) for more.
## Quick Start
## 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 --webUrl=https://your.server
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.
## Optional guide
### Nginx
Create `/etc/nginx/sites-enabled/dnote`:
```
server {
server_name my-dnote-server.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:3001;
}
}
```
Replace `my-dnote-server.com` with your domain, then reload:
```bash
sudo service nginx reload
```
### Apache2
Enable `mod_proxy`, then create `/etc/apache2/sites-available/dnote.conf`:
```
<VirtualHost *:80>
ServerName notes.example.com
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:3001/ keepalive=On
ProxyPassReverse / http://127.0.0.1:3001/
RequestHeader set X-Forwarded-HTTPS "0"
</VirtualHost>
```
Enable and restart:
```bash
a2ensite dnote
sudo service apache2 restart
```
### TLS
Use LetsEncrypt to obtain a certificate and configure HTTPS in your reverse proxy.
### systemd Daemon
Create `/etc/systemd/system/dnote.service`:
```
[Unit]
Description=Starts the dnote server
Requires=network.target
After=network.target
[Service]
Type=simple
User=$user
Restart=always
RestartSec=3
WorkingDirectory=/home/$user
ExecStart=/usr/local/bin/dnote-server start --webUrl=$WebURL
[Install]
WantedBy=multi-user.target
```
Replace `$user` and `$WebURL`. Add `--dbPath` to `ExecStart` if you want a custom database location.
Enable and start:
```bash
sudo systemctl daemon-reload
sudo systemctl enable dnote
sudo systemctl start dnote
```
### Email Support
If you want emails, add these environment variables:
- `SmtpHost` - SMTP hostname
- `SmtpPort` - SMTP port
- `SmtpUsername` - SMTP username
- `SmtpPassword` - SMTP password
For systemd, add as `Environment=` lines in the service file.

9
go.mod
View file

@ -14,10 +14,9 @@ require (
github.com/mattn/go-sqlite3 v1.14.32
github.com/pkg/errors v0.9.1
github.com/radovskyb/watcher v1.0.7
github.com/robfig/cron v1.2.0
github.com/sergi/go-diff v1.3.1
github.com/spf13/cobra v1.10.1
golang.org/x/crypto v0.42.0
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
@ -36,9 +35,9 @@ require (
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.36.0 // indirect
golang.org/x/term v0.35.0 // indirect
golang.org/x/text v0.29.0 // 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
)

18
go.sum
View file

@ -53,8 +53,6 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
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/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
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=
@ -73,15 +71,15 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
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.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
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.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.35.0 h1:bZBVKBudEyhRcajGcNc3jIfWPqV4y/Kt2XcoigOWtDQ=
golang.org/x/term v0.35.0/go.mod h1:TPGtkTLesOwf2DE8CgVYiZinHAOuy5AYUYT1lENIZnA=
golang.org/x/text v0.29.0 h1:1neNs90w9YzJ9BocxfsQNHKuAT4pkghyXc4nhZ6sJvk=
golang.org/x/text v0.29.0/go.mod h1:7MhJOA9CD2qZyOKYazxdYMF85OwPdEr9jTtBpO7ydH4=
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=

View file

@ -1,12 +1,33 @@
FROM busybox:glibc
ARG tarballName
RUN test -n "$tarballName"
ARG TARGETPLATFORM
ARG version
WORKDIR dnote
RUN test -n "$TARGETPLATFORM" || (echo "TARGETPLATFORM is required" && exit 1)
RUN test -n "$version" || (echo "version is required" && exit 1)
COPY "$tarballName" .
RUN tar -xvzf "$tarballName"
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"]
@ -14,3 +35,6 @@ 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

View file

@ -1,30 +0,0 @@
# Dnote Docker Image
The official Dnote docker image.
## Installing Dnote Server Using Docker
1. Install [Docker](https://docs.docker.com/install/).
2. Install Docker [Compose plugin](https://docs.docker.com/compose/install/linux/).
3. Download the [compose.yml](https://raw.githubusercontent.com/dnote/dnote/master/host/docker/compose.yml) file by running:
```
curl https://raw.githubusercontent.com/dnote/dnote/master/host/docker/compose.yml > compose.yml
```
4. Run the following to download the images and run the containers
```
docker compose pull
docker compose up -d
```
Visit http://localhost:3001 in your browser to see Dnote running.
Please see [the installation guide](https://github.com/dnote/dnote/blob/master/SELF_HOSTING.md) for further configuration.
## Supported platform
Currently, the official Docker image for Dnote supports Linux running AMD64 CPU architecture.
If you run ARM64, please install Dnote server by downloading a binary distribution by following [the guide](https://github.com/dnote/dnote/blob/master/SELF_HOSTING.md).

View file

@ -1,13 +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/../.."
tarballName="dnote_server_${version}_linux_amd64.tar.gz"
# copy over the build artifact to the Docker build context
cp "$projectDir/build/server/$tarballName" "$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/"
docker build --network=host -t dnote/dnote:"$version" --build-arg tarballName="$tarballName" .
# 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

View file

@ -1,13 +0,0 @@
#!/usr/bin/env bash
set -eux
version=$1
docker login
# tag the release
docker tag dnote/dnote:"$version" dnote/dnote:latest
# publish
docker push dnote/dnote:"$version"
docker push dnote/dnote:latest

View file

@ -1,661 +0,0 @@
GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
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.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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 <https://www.gnu.org/licenses/>.
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
<https://www.gnu.org/licenses/>.

View file

@ -1,674 +0,0 @@
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
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.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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 <https://www.gnu.org/licenses/>.
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:
<program> Copyright (C) <year> <name of author>
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
<https://www.gnu.org/licenses/>.
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
<https://www.gnu.org/licenses/why-not-lgpl.html>.

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

84
pkg/assert/prompt.go Normal file
View file

@ -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
}

View file

@ -96,7 +96,7 @@ dnote find "merge sort" -b algorithm
_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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -176,7 +173,7 @@ func doReq(ctx context.DnoteCtx, method, path, body string, options *requestOpti
return nil, errors.Wrap(err, "getting request")
}
log.Debug("HTTP request: %+v\n", req)
log.Debug("HTTP %s %s\n", method, path)
hc := getHTTPClient(ctx, options)
res, err := hc.Do(req)
@ -184,7 +181,7 @@ func doReq(ctx context.DnoteCtx, method, path, body string, options *requestOpti
return res, errors.Wrap(err, "making http request")
}
log.Debug("HTTP response: %+v\n", res)
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")
@ -246,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"`
}
@ -286,6 +282,9 @@ func GetSyncFragment(ctx context.DnoteCtx, afterUSN int) (GetSyncFragmentResp, e
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 := io.ReadAll(res.Body)
if err != nil {
@ -458,7 +457,6 @@ func CreateNote(ctx context.DnoteCtx, bookUUID, content string) (CreateNoteResp,
type updateNotePayload struct {
BookUUID *string `json:"book_uuid"`
Body *string `json:"content"`
Public *bool `json:"public"`
}
// UpdateNoteResp is the response from create book api
@ -468,11 +466,10 @@ type UpdateNoteResp struct {
}
// UpdateNote updates a note in the server
func UpdateNote(ctx context.DnoteCtx, uuid, bookUUID, content string, public bool) (UpdateNoteResp, error) {
func UpdateNote(ctx context.DnoteCtx, uuid, bookUUID, content string) (UpdateNoteResp, error) {
payload := updateNotePayload{
BookUUID: &bookUUID,
Body: &content,
Public: &public,
}
b, err := json.Marshal(payload)
if err != nil {
@ -579,10 +576,12 @@ func Signin(ctx context.DnoteCtx, email, password string) (SigninResponse, error
return SigninResponse{}, errors.Wrap(err, "marshaling payload")
}
res, err := doReq(ctx, "POST", "/v3/signin", string(b), nil)
if res.StatusCode == http.StatusUnauthorized {
return SigninResponse{}, ErrInvalidLogin
} else if err != 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")
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -124,6 +121,18 @@ func TestSignIn(t *testing.T) {
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) {

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -134,7 +131,7 @@ func newRun(ctx context.DnoteCtx) infra.RunEFunc {
return err
}
output.NoteInfo(info)
output.NoteInfo(os.Stdout, info)
if err := upgrade.Check(ctx); err != nil {
log.Error(errors.Wrap(err, "automatically checking updates").Error())
@ -173,7 +170,7 @@ func writeNote(ctx context.DnoteCtx, bookLabel string, content string, ts int64)
return 0, errors.Wrap(err, "generating uuid")
}
n := database.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, false, true)
n := database.NewNote(noteUUID, bookUUID, content, ts, 0, 0, false, true)
err = n.Insert(tx)
if err != nil {

View file

@ -1,98 +0,0 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
*
* This file is part of Dnote.
*
* Dnote 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 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. If not, see <https://www.gnu.org/licenses/>.
*/
package cat
import (
"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/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 the future version.
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 context.DnoteCtx) *cobra.Command {
cmd := &cobra.Command{
Use: "cat <book name> <note index>",
Aliases: []string{"c"},
Short: "See a note",
Example: example,
RunE: NewRun(ctx, false),
PreRunE: preRun,
Deprecated: deprecationWarning,
}
return cmd
}
// NewRun returns a new run function
func NewRun(ctx context.DnoteCtx, contentOnly bool) infra.RunEFunc {
return func(cmd *cobra.Command, args []string) error {
var noteRowIDArg string
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"))
noteRowIDArg = args[1]
} else {
noteRowIDArg = args[0]
}
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(info)
} else {
output.NoteInfo(info)
}
return nil
}
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -169,7 +166,7 @@ func runNote(ctx context.DnoteCtx, rowIDArg string) error {
}
log.Success("edited the note\n")
output.NoteInfo(noteInfo)
output.NoteInfo(os.Stdout, noteInfo)
return nil
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,25 +1,23 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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"
@ -132,7 +130,7 @@ func runNote(ctx context.DnoteCtx, rowIDArg string) error {
return err
}
output.NoteInfo(noteInfo)
output.NoteInfo(os.Stdout, noteInfo)
ok, err := maybeConfirm("remove this note?", false)
if err != nil {

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,35 +0,0 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
*
* This file is part of Dnote.
*
* Dnote 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 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. If not, see <https://www.gnu.org/licenses/>.
*/
package sync
import (
"github.com/dnote/dnote/pkg/cli/context"
"path/filepath"
)
var testDir = "../../tmp"
var paths context.Paths = context.Paths{
Home: testDir,
Cache: testDir,
Config: testDir,
Data: testDir,
}
var dbPath = filepath.Join(testDir, "test.db")

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -90,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
}
@ -97,14 +95,14 @@ 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.
// 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 {
@ -124,6 +122,9 @@ func processFragments(fragments []client.SyncFragment) (syncList, error) {
if fragment.FragMaxUSN > maxUSN {
maxUSN = fragment.FragMaxUSN
}
if fragment.UserMaxUSN > userMaxUSN {
userMaxUSN = fragment.UserMaxUSN
}
if fragment.CurrentTime > maxCurrentTime {
maxCurrentTime = fragment.CurrentTime
}
@ -135,6 +136,7 @@ func processFragments(fragments []client.SyncFragment) (syncList, error) {
ExpungedNotes: expungedNotes,
ExpungedBooks: expungedBooks,
MaxUSN: maxUSN,
UserMaxUSN: userMaxUSN,
MaxCurrentTime: maxCurrentTime,
}
@ -181,11 +183,68 @@ func getSyncFragments(ctx context.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 "<redacted>"
}
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 "<redacted>"
}
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 *database.DB, label string) (string, error) {
@ -222,7 +281,7 @@ func mergeBook(tx *database.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")
}
}
@ -281,8 +340,8 @@ func mergeNote(tx *database.DB, serverNote client.SyncFragNote, localNote databa
// 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)
}
@ -312,7 +371,7 @@ func stepSyncNote(tx *database.DB, n client.SyncFragNote) error {
// if note exists in the server and does not exist in the client, insert the note.
if err == sql.ErrNoRows {
note := database.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)
@ -336,7 +395,7 @@ func fullSyncNote(tx *database.DB, n client.SyncFragNote) error {
// if note exists in the server and does not exist in the client, insert the note.
if err == sql.ErrNoRows {
note := database.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)
@ -541,6 +600,8 @@ 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")
@ -548,6 +609,8 @@ func fullSync(ctx context.DnoteCtx, tx *database.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")
@ -578,7 +641,7 @@ func fullSync(ctx context.DnoteCtx, tx *database.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,6 +656,8 @@ func stepSync(ctx context.DnoteCtx, tx *database.DB, afterUSN int) error {
log.Info("resolving delta.")
log.DebugNewline()
list, err := getSyncList(ctx, afterUSN)
if err != nil {
return errors.Wrap(err, "getting sync list")
@ -622,7 +687,7 @@ func stepSync(ctx context.DnoteCtx, tx *database.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")
}
@ -678,13 +743,9 @@ func sendBooks(ctx context.DnoteCtx, tx *database.DB) (bool, error) {
} else {
resp, err := client.CreateBook(ctx, book.Label)
if err != nil {
// If we get a 409 conflict, it means another client uploaded data.
if isConflictError(err) {
log.Debug("409 conflict creating book %s, will retry after sync\n", book.Label)
isBehind = true
continue
}
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)
@ -756,10 +817,92 @@ func sendBooks(ctx context.DnoteCtx, tx *database.DB) (bool, error) {
return isBehind, nil
}
// 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(&noteUUID, &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")
}
@ -768,11 +911,11 @@ func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) {
for rows.Next() {
var note database.Note
if err = rows.Scan(&note.UUID, &note.BookUUID, &note.Body, &note.Public, &note.Deleted, &note.USN, &note.AddedOn); err != nil {
if err = rows.Scan(&note.UUID, &note.BookUUID, &note.Body, &note.Deleted, &note.USN, &note.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
@ -789,8 +932,7 @@ func sendNotes(ctx context.DnoteCtx, tx *database.DB) (bool, error) {
} else {
resp, err := client.CreateNote(ctx, note.BookUUID, note.Body)
if err != nil {
// If we get a 409 conflict, it means another client uploaded data.
log.Debug("error creating note (will retry after sync): %v\n", err)
log.Debug("failed to create note %s (book: %s): %v\n", note.UUID, note.BookUUID, err)
isBehind = true
continue
}
@ -823,7 +965,7 @@ func sendNotes(ctx context.DnoteCtx, tx *database.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")
}
@ -867,6 +1009,8 @@ func sendChanges(ctx context.DnoteCtx, tx *database.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")
@ -900,10 +1044,24 @@ func updateLastSyncAt(tx *database.DB, val int64) error {
return nil
}
func saveSyncState(tx *database.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")
}
@ -1012,6 +1170,28 @@ func newRun(ctx context.DnoteCtx) infra.RunEFunc {
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)
@ -1060,10 +1240,14 @@ func newRun(ctx context.DnoteCtx) infra.RunEFunc {
}
}
tx.Commit()
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "committing transaction")
}
log.Success("success\n")
checkPostSyncIntegrity(ctx.DB)
if err := upgrade.Check(ctx); err != nil {
log.Error(errors.Wrap(err, "automatically checking updates").Error())
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -97,6 +94,7 @@ func TestProcessFragments(t *testing.T) {
ExpungedNotes: map[string]bool{},
ExpungedBooks: map[string]bool{},
MaxUSN: 10,
UserMaxUSN: 10,
MaxCurrentTime: 1550436136,
}
@ -106,8 +104,7 @@ func TestProcessFragments(t *testing.T) {
func TestGetLastSyncAt(t *testing.T) {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
database.MustExec(t, "setting up last_sync_at", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastSyncAt, 1541108743)
// exec
@ -129,8 +126,7 @@ func TestGetLastSyncAt(t *testing.T) {
func TestGetLastMaxUSN(t *testing.T) {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
database.MustExec(t, "setting up last_max_usn", db, "INSERT INTO system (key, value) VALUES (?, ?)", consts.SystemLastMaxUSN, 20001)
// exec
@ -176,8 +172,7 @@ func TestResolveLabel(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
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")
@ -206,8 +201,7 @@ func TestResolveLabel(t *testing.T) {
func TestSyncDeleteNote(t *testing.T) {
t.Run("exists on server only", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
// execute
tx, err := db.Begin()
@ -235,8 +229,7 @@ func TestSyncDeleteNote(t *testing.T) {
b1UUID := testutils.MustGenerateUUID(t)
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -305,8 +298,7 @@ func TestSyncDeleteNote(t *testing.T) {
b1UUID := testutils.MustGenerateUUID(t)
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -361,8 +353,7 @@ func TestSyncDeleteNote(t *testing.T) {
func TestSyncDeleteBook(t *testing.T) {
t.Run("exists on server only", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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
@ -406,8 +397,7 @@ func TestSyncDeleteBook(t *testing.T) {
b1UUID := testutils.MustGenerateUUID(t)
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -472,8 +462,7 @@ func TestSyncDeleteBook(t *testing.T) {
b2UUID := testutils.MustGenerateUUID(t)
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -538,8 +527,7 @@ func TestSyncDeleteBook(t *testing.T) {
b1UUID := testutils.MustGenerateUUID(t)
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -590,8 +578,7 @@ func TestSyncDeleteBook(t *testing.T) {
func TestFullSyncNote(t *testing.T) {
t.Run("exists on server only", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
b1UUID := testutils.MustGenerateUUID(t)
database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label")
@ -822,8 +809,7 @@ n1 body edited
for idx, tc := range testCases {
func() {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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")
@ -884,8 +870,7 @@ n1 body edited
func TestFullSyncBook(t *testing.T) {
t.Run("exists on server only", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1023,8 +1008,7 @@ func TestFullSyncBook(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1076,8 +1060,7 @@ func TestFullSyncBook(t *testing.T) {
func TestStepSyncNote(t *testing.T) {
t.Run("exists on server only", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
b1UUID := testutils.MustGenerateUUID(t)
database.MustExec(t, "inserting book", db, "INSERT INTO books (uuid, label) VALUES (?, ?)", b1UUID, "b1-label")
@ -1234,8 +1217,7 @@ n1 body edited
for idx, tc := range testCases {
func() {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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")
@ -1296,8 +1278,7 @@ n1 body edited
func TestStepSyncBook(t *testing.T) {
t.Run("exists on server only", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1419,8 +1400,7 @@ func TestStepSyncBook(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1483,8 +1463,7 @@ func TestStepSyncBook(t *testing.T) {
func TestMergeBook(t *testing.T) {
t.Run("insert, no duplicates", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
// test
tx, err := db.Begin()
@ -1527,8 +1506,7 @@ func TestMergeBook(t *testing.T) {
t.Run("insert, 1 duplicate", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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
@ -1579,8 +1557,7 @@ func TestMergeBook(t *testing.T) {
t.Run("insert, 3 duplicates", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1648,8 +1625,7 @@ func TestMergeBook(t *testing.T) {
t.Run("update, no duplicates", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
// test
tx, err := db.Begin()
@ -1695,8 +1671,7 @@ func TestMergeBook(t *testing.T) {
t.Run("update, 1 duplicate", func(t *testing.T) {
// set up
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1749,8 +1724,7 @@ func TestMergeBook(t *testing.T) {
t.Run("update, 3 duplicate", func(t *testing.T) {
// set uj
db := database.InitTestDB(t, dbPath, nil)
defer database.TeardownTestDB(t, db)
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)
@ -1819,44 +1793,132 @@ func TestMergeBook(t *testing.T) {
}
func TestSaveServerState(t *testing.T) {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
testutils.Login(t, &ctx)
t.Run("with data received", func(t *testing.T) {
// set up
db := database.InitTestMemoryDB(t)
testutils.LoginDB(t, db)
db := ctx.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)
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())
}
// execute
tx, err := db.Begin()
if err != nil {
t.Fatal(errors.Wrap(err, "beginning a transaction").Error())
}
serverTime := int64(1541108743)
serverMaxUSN := 100
userMaxUSN := 100
serverTime := int64(1541108743)
serverMaxUSN := 100
err = saveSyncState(tx, serverTime, serverMaxUSN, userMaxUSN)
if err != nil {
tx.Rollback()
t.Fatal(errors.Wrap(err, "executing").Error())
}
err = saveSyncState(tx, serverTime, serverMaxUSN)
if err != nil {
tx.Rollback()
t.Fatal(errors.Wrap(err, "executing").Error())
}
tx.Commit()
tx.Commit()
// test
var lastSyncedAt int64
var lastMaxUSN int
// 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)
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")
})
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
@ -1864,8 +1926,7 @@ func TestSaveServerState(t *testing.T) {
// are updated accordingly based on the server response.
func TestSendBooks(t *testing.T) {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
testutils.Login(t, &ctx)
db := ctx.DB
@ -1900,7 +1961,7 @@ func TestSendBooks(t *testing.T) {
var updatesUUIDs []string
var deletedUUIDs []string
// fire up a test server. It decrypts the payload for test purposes.
// 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
@ -1967,7 +2028,7 @@ func TestSendBooks(t *testing.T) {
// test
// First, decrypt data so that they can be asserted
// 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
})
@ -2097,9 +2158,8 @@ func TestSendBooks_isBehind(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
ctx := context.InitTestCtx(t, paths, nil)
ctx := context.InitTestCtx(t)
ctx.APIEndpoint = ts.URL
defer context.TeardownTestCtx(t, ctx)
testutils.Login(t, &ctx)
db := ctx.DB
@ -2145,9 +2205,8 @@ func TestSendBooks_isBehind(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
ctx := context.InitTestCtx(t, paths, nil)
ctx := context.InitTestCtx(t)
ctx.APIEndpoint = ts.URL
defer context.TeardownTestCtx(t, ctx)
testutils.Login(t, &ctx)
db := ctx.DB
@ -2193,9 +2252,8 @@ func TestSendBooks_isBehind(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
ctx := context.InitTestCtx(t, paths, nil)
ctx := context.InitTestCtx(t)
ctx.APIEndpoint = ts.URL
defer context.TeardownTestCtx(t, ctx)
testutils.Login(t, &ctx)
db := ctx.DB
@ -2228,8 +2286,7 @@ func TestSendBooks_isBehind(t *testing.T) {
// uuid from the incoming data.
func TestSendNotes(t *testing.T) {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
testutils.Login(t, &ctx)
db := ctx.DB
@ -2264,7 +2321,7 @@ func TestSendNotes(t *testing.T) {
var updatedUUIDs []string
var deletedUUIDs []string
// fire up a test server. It decrypts the payload for test purposes.
// 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
@ -2381,8 +2438,7 @@ func TestSendNotes(t *testing.T) {
func TestSendNotes_addedOn(t *testing.T) {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
testutils.Login(t, &ctx)
db := ctx.DB
@ -2393,7 +2449,7 @@ func TestSendNotes_addedOn(t *testing.T) {
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. It decrypts the payload for test purposes.
// 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{
@ -2513,8 +2569,7 @@ func TestSendNotes_isBehind(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
testutils.Login(t, &ctx)
ctx.APIEndpoint = ts.URL
@ -2562,8 +2617,7 @@ func TestSendNotes_isBehind(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
testutils.Login(t, &ctx)
ctx.APIEndpoint = ts.URL
@ -2611,8 +2665,7 @@ func TestSendNotes_isBehind(t *testing.T) {
for idx, tc := range testCases {
func() {
// set up
ctx := context.InitTestCtx(t, paths, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
testutils.Login(t, &ctx)
ctx.APIEndpoint = ts.URL
@ -2777,8 +2830,7 @@ n1 body edited
for idx, tc := range testCases {
func() {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
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)
@ -2859,8 +2911,7 @@ n1 body edited
func TestCheckBookPristine(t *testing.T) {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
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)
@ -3033,8 +3084,7 @@ func TestCheckBookInList(t *testing.T) {
func TestCleanLocalNotes(t *testing.T) {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
list := syncList{
Notes: map[string]client.SyncFragNote{
@ -3105,8 +3155,7 @@ func TestCleanLocalNotes(t *testing.T) {
func TestCleanLocalBooks(t *testing.T) {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
list := syncList{
Notes: map[string]client.SyncFragNote{
@ -3173,8 +3222,7 @@ func TestCleanLocalBooks(t *testing.T) {
func TestPrepareEmptyServerSync(t *testing.T) {
// set up
db := database.InitTestDB(t, "../../tmp/.dnote", nil)
defer database.TeardownTestDB(t, db)
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)

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,91 +1,31 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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 ls
package view
import (
"database/sql"
"fmt"
"io"
"strings"
"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"
)
var example = `
* List all books
dnote ls
* List notes in a book
dnote ls javascript
`
var deprecationWarning = `and "view" will replace it in the future version.
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 context.DnoteCtx) *cobra.Command {
cmd := &cobra.Command{
Use: "ls <book name?>",
Aliases: []string{"l", "notes"},
Short: "List all notes",
Example: example,
RunE: NewRun(ctx, false),
PreRunE: preRun,
Deprecated: deprecationWarning,
}
return cmd
}
// NewRun returns a new run function for ls
func NewRun(ctx context.DnoteCtx, nameOnly bool) infra.RunEFunc {
return func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
if err := printBooks(ctx, nameOnly); 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
@ -100,15 +40,13 @@ type noteInfo struct {
// 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")
// Check for \r\n first
if idx := strings.Index(str, "\r\n"); idx != -1 {
return idx
}
return ret
// Then check for \n
return strings.Index(str, "\n")
}
// formatBody returns an excerpt of the given raw note content and a boolean
@ -126,15 +64,15 @@ func formatBody(noteBody string) (string, bool) {
return strings.Trim(trimmed, " "), false
}
func printBookLine(info bookInfo, nameOnly bool) {
func printBookLine(w io.Writer, info bookInfo, nameOnly bool) {
if nameOnly {
fmt.Println(info.BookLabel)
fmt.Fprintln(w, info.BookLabel)
} else {
log.Printf("%s %s\n", info.BookLabel, log.ColorYellow.Sprintf("(%d)", info.NoteCount))
fmt.Fprintf(w, "%s %s\n", info.BookLabel, log.ColorYellow.Sprintf("(%d)", info.NoteCount))
}
}
func printBooks(ctx context.DnoteCtx, nameOnly bool) error {
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
@ -160,13 +98,13 @@ func printBooks(ctx context.DnoteCtx, nameOnly bool) error {
}
for _, info := range infos {
printBookLine(info, nameOnly)
printBookLine(w, info, nameOnly)
}
return nil
}
func printNotes(ctx context.DnoteCtx, bookName string) error {
func listNotes(ctx context.DnoteCtx, w io.Writer, bookName string) error {
db := ctx.DB
var bookUUID string
@ -194,7 +132,7 @@ func printNotes(ctx context.DnoteCtx, bookName string) error {
infos = append(infos, info)
}
log.Infof("on book %s\n", bookName)
fmt.Fprintf(w, "on book %s\n", bookName)
for _, info := range infos {
body, isExcerpt := formatBody(info.Body)
@ -204,7 +142,7 @@ func printNotes(ctx context.DnoteCtx, bookName string) error {
body = fmt.Sprintf("%s %s", body, log.ColorYellow.Sprintf("[---More---]"))
}
log.Plainf("%s %s\n", rowid, body)
fmt.Fprintf(w, "%s %s\n", rowid, body)
}
return nil

View file

@ -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")
}

47
pkg/cli/cmd/view/note.go Normal file
View file

@ -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
}

View file

@ -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")
}

View file

@ -1,32 +1,28 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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"
"github.com/dnote/dnote/pkg/cli/cmd/cat"
"github.com/dnote/dnote/pkg/cli/cmd/ls"
"github.com/dnote/dnote/pkg/cli/utils"
)
var example = `
@ -71,27 +67,26 @@ func NewCmd(ctx context.DnoteCtx) *cobra.Command {
func newRun(ctx context.DnoteCtx) infra.RunEFunc {
return func(cmd *cobra.Command, args []string) error {
var run infra.RunEFunc
if len(args) == 0 {
run = ls.NewRun(ctx, nameOnly)
// 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]) {
run = cat.NewRun(ctx, contentOnly)
// View a note by index
return viewNote(ctx, os.Stdout, args[0], contentOnly)
} else {
run = ls.NewRun(ctx, false)
// List notes in a book
return listNotes(ctx, os.Stdout, args[0])
}
} else if len(args) == 2 {
// DEPRECATED: passing book name to view command is deprecated
run = cat.NewRun(ctx, false)
} else {
return errors.New("Incorrect number of arguments")
// View a note in a book (book name + note index)
return viewNote(ctx, os.Stdout, args[1], contentOnly)
}
return run(cmd, args)
return errors.New("Incorrect number of arguments")
}
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

48
pkg/cli/context/files.go Normal file
View file

@ -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
}

View file

@ -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)
}

View file

@ -1,26 +1,22 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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 (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/dnote/dnote/pkg/cli/consts"
@ -29,11 +25,27 @@ import (
"github.com/pkg/errors"
)
// InitTestCtx initializes a test context
func InitTestCtx(t *testing.T, paths Paths, dbOpts *database.TestDBOptions) DnoteCtx {
dbPath := fmt.Sprintf("%s/%s/%s", paths.Data, consts.DnoteDirName, consts.DnoteDBFileName)
// 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,
}
}
db := database.InitTestDB(t, dbPath, dbOpts)
// 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,
@ -42,17 +54,47 @@ func InitTestCtx(t *testing.T, paths Paths, dbOpts *database.TestDBOptions) Dnot
}
}
// TeardownTestCtx cleans up the test context
func TeardownTestCtx(t *testing.T, ctx DnoteCtx) {
database.TeardownTestDB(t, ctx.DB)
// 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 := os.RemoveAll(ctx.Paths.Data); err != nil {
t.Fatal(errors.Wrap(err, "removing test data directory"))
if err := InitDnoteDirs(paths); err != nil {
t.Fatal(errors.Wrap(err, "creating test directories"))
}
if err := os.RemoveAll(ctx.Paths.Config); err != nil {
t.Fatal(errors.Wrap(err, "removing test config directory"))
}
if err := os.RemoveAll(ctx.Paths.Cache); err != nil {
t.Fatal(errors.Wrap(err, "removing test cache directory"))
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
}
}

View file

@ -1,123 +0,0 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
*
* This file is part of Dnote.
*
* Dnote 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 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. If not, see <https://www.gnu.org/licenses/>.
*/
// Package crypt provides cryptographic funcitonalities
package crypt
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"io"
"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)
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
}

View file

@ -1,118 +0,0 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
*
* This file is part of Dnote.
*
* Dnote 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 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. If not, see <https://www.gnu.org/licenses/>.
*/
package crypt
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
"testing"
"github.com/dnote/dnote/pkg/assert"
"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"))
}
assert.DeepEqual(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"))
}
assert.DeepEqual(t, plaintext, []byte(tc.expectedPlaintext), "plaintext mismatch")
})
}
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -41,13 +38,12 @@ type Note struct {
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,7 +51,6 @@ func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, publ
AddedOn: addedOn,
EditedOn: editedOn,
USN: usn,
Public: public,
Deleted: deleted,
Dirty: dirty,
}
@ -63,8 +58,8 @@ func NewNote(uuid, bookUUID, body string, addedOn, editedOn int64, usn int, publ
// Insert inserts a new note
func (n Note) Insert(db *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)
_, 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)
@ -75,8 +70,8 @@ func (n Note) Insert(db *DB) error {
// Update updates the note with the given data
func (n Note) Update(db *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)
_, 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)

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -34,7 +31,6 @@ func TestNewNote(t *testing.T) {
addedOn int64
editedOn int64
usn int
public bool
deleted bool
dirty bool
}{
@ -45,7 +41,6 @@ func TestNewNote(t *testing.T) {
addedOn: 1542058875,
editedOn: 0,
usn: 0,
public: false,
deleted: false,
dirty: false,
},
@ -56,14 +51,13 @@ func TestNewNote(t *testing.T) {
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)
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))
@ -71,7 +65,6 @@ func TestNewNote(t *testing.T) {
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.Public, tc.public, fmt.Sprintf("Public 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))
}
@ -85,7 +78,6 @@ func TestNoteInsert(t *testing.T) {
addedOn int64
editedOn int64
usn int
public bool
deleted bool
dirty bool
}{
@ -96,7 +88,6 @@ func TestNoteInsert(t *testing.T) {
addedOn: 1542058875,
editedOn: 0,
usn: 0,
public: false,
deleted: false,
dirty: false,
},
@ -107,7 +98,6 @@ func TestNoteInsert(t *testing.T) {
addedOn: 1542058875,
editedOn: 1542058876,
usn: 1008,
public: true,
deleted: true,
dirty: true,
},
@ -116,8 +106,7 @@ func TestNoteInsert(t *testing.T) {
for idx, tc := range testCases {
func() {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
n := Note{
UUID: tc.uuid,
@ -126,7 +115,6 @@ func TestNoteInsert(t *testing.T) {
AddedOn: tc.addedOn,
EditedOn: tc.editedOn,
USN: tc.usn,
Public: tc.public,
Deleted: tc.deleted,
Dirty: tc.dirty,
}
@ -148,10 +136,10 @@ func TestNoteInsert(t *testing.T) {
var uuid, bookUUID, body string
var addedOn, editedOn int64
var usn int
var public, deleted, dirty bool
var deleted, dirty bool
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)
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))
@ -159,7 +147,6 @@ func TestNoteInsert(t *testing.T) {
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, public, tc.public, fmt.Sprintf("public 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))
}()
@ -174,14 +161,12 @@ func TestNoteUpdate(t *testing.T) {
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
}{
@ -192,14 +177,12 @@ func TestNoteUpdate(t *testing.T) {
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,
},
@ -210,14 +193,12 @@ func TestNoteUpdate(t *testing.T) {
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,
},
@ -228,14 +209,12 @@ func TestNoteUpdate(t *testing.T) {
addedOn: 1542058875,
editedOn: 0,
usn: 10,
public: false,
deleted: false,
dirty: false,
newBookUUID: "",
newBody: "",
newEditedOn: 1542058879,
newUSN: 151,
newPublic: false,
newDeleted: true,
newDirty: false,
},
@ -246,14 +225,12 @@ func TestNoteUpdate(t *testing.T) {
addedOn: 1542058875,
editedOn: 0,
usn: 0,
public: false,
deleted: false,
dirty: false,
newBookUUID: "",
newBody: "",
newEditedOn: 1542058879,
newUSN: 15,
newPublic: false,
newDeleted: true,
newDirty: false,
},
@ -262,8 +239,7 @@ func TestNoteUpdate(t *testing.T) {
for idx, tc := range testCases {
func() {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
n1 := Note{
UUID: tc.uuid,
@ -272,7 +248,6 @@ func TestNoteUpdate(t *testing.T) {
AddedOn: tc.addedOn,
EditedOn: tc.editedOn,
USN: tc.usn,
Public: tc.public,
Deleted: tc.deleted,
Dirty: tc.dirty,
}
@ -283,13 +258,12 @@ func TestNoteUpdate(t *testing.T) {
AddedOn: 1542058875,
EditedOn: 0,
USN: 39,
Public: false,
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, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1.UUID, n1.BookUUID, n1.USN, n1.AddedOn, n1.EditedOn, n1.Body, n1.Public, 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, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n2.UUID, n2.BookUUID, n2.USN, n2.AddedOn, n2.EditedOn, n2.Body, n2.Public, n2.Deleted, n2.Dirty)
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()
@ -301,7 +275,6 @@ func TestNoteUpdate(t *testing.T) {
n1.Body = tc.newBody
n1.EditedOn = tc.newEditedOn
n1.USN = tc.newUSN
n1.Public = tc.newPublic
n1.Deleted = tc.newDeleted
n1.Dirty = tc.newDirty
@ -315,11 +288,11 @@ func TestNoteUpdate(t *testing.T) {
// test
var n1Record, n2Record Note
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)
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, 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)
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))
@ -327,7 +300,6 @@ func TestNoteUpdate(t *testing.T) {
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.Public, tc.newPublic, fmt.Sprintf("n1 public 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))
@ -337,7 +309,6 @@ func TestNoteUpdate(t *testing.T) {
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.Public, n2.Public, fmt.Sprintf("n2 public 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))
}()
@ -359,8 +330,7 @@ func TestNoteUpdateUUID(t *testing.T) {
for idx, tc := range testCases {
t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
n1 := Note{
UUID: "n1-uuid",
@ -414,8 +384,7 @@ func TestNoteUpdateUUID(t *testing.T) {
func TestNoteExpunge(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
n1 := Note{
UUID: "n1-uuid",
@ -424,7 +393,6 @@ func TestNoteExpunge(t *testing.T) {
AddedOn: 1542058874,
EditedOn: 0,
USN: 22,
Public: false,
Deleted: false,
Dirty: false,
}
@ -435,13 +403,12 @@ func TestNoteExpunge(t *testing.T) {
AddedOn: 1542058875,
EditedOn: 0,
USN: 39,
Public: false,
Deleted: false,
Dirty: false,
}
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)
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)
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()
@ -464,8 +431,8 @@ func TestNoteExpunge(t *testing.T) {
var n2Record Note
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)
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")
@ -473,7 +440,6 @@ func TestNoteExpunge(t *testing.T) {
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.Public, n2.Public, "n2 public mismatch")
assert.Equal(t, n2Record.Deleted, n2.Deleted, "n2 deleted mismatch")
assert.Equal(t, n2Record.Dirty, n2.Dirty, "n2 dirty mismatch")
}
@ -540,8 +506,7 @@ func TestBookInsert(t *testing.T) {
for idx, tc := range testCases {
func() {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
b := Book{
UUID: tc.uuid,
@ -621,8 +586,7 @@ func TestBookUpdate(t *testing.T) {
for idx, tc := range testCases {
func() {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
b1 := Book{
UUID: "b1-uuid",
@ -700,8 +664,7 @@ func TestBookUpdateUUID(t *testing.T) {
t.Run(fmt.Sprintf("testCase%d", idx), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
b1 := Book{
UUID: "b1-uuid",
@ -751,8 +714,7 @@ func TestBookUpdateUUID(t *testing.T) {
func TestBookExpunge(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
b1 := Book{
UUID: "b1-uuid",
@ -806,8 +768,7 @@ func TestBookExpunge(t *testing.T) {
// 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 := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
// execute - insert
n := Note{
@ -817,7 +778,6 @@ func TestNoteFTS(t *testing.T) {
AddedOn: 1542058875,
EditedOn: 0,
USN: 0,
Public: false,
Deleted: false,
Dirty: false,
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -170,7 +167,6 @@ func GetActiveNote(db *DB, rowid int) (Note, error) {
added_on,
edited_on,
usn,
public,
deleted,
dirty
FROM notes WHERE rowid = ? AND deleted = false;`, rowid).Scan(
@ -181,7 +177,6 @@ func GetActiveNote(db *DB, rowid int) (Note, error) {
&ret.AddedOn,
&ret.EditedOn,
&ret.USN,
&ret.Public,
&ret.Deleted,
&ret.Dirty,
)

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -47,8 +44,7 @@ func TestInsertSystem(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
// execute
tx, err := db.Begin()
@ -95,8 +91,7 @@ func TestUpsertSystem(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("insert %s %s", tc.key, tc.val), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "baz", "quz")
@ -134,8 +129,7 @@ func TestUpsertSystem(t *testing.T) {
func TestGetSystem(t *testing.T) {
t.Run(fmt.Sprintf("get string value"), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
// execute
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", "bar")
@ -157,8 +151,7 @@ func TestGetSystem(t *testing.T) {
t.Run(fmt.Sprintf("get int64 value"), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
// execute
MustExec(t, "inserting a system configuration", db, "INSERT INTO system (key, value) VALUES (?, ?)", "foo", 1234)
@ -198,8 +191,7 @@ func TestUpdateSystem(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("update %s %s", tc.key, tc.val), func(t *testing.T) {
// Setup
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
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")
@ -238,11 +230,10 @@ func TestUpdateSystem(t *testing.T) {
func TestGetActiveNote(t *testing.T) {
t.Run("not deleted", func(t *testing.T) {
// set up
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
n1UUID := "n1-uuid"
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, false, true)
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)
@ -261,18 +252,16 @@ func TestGetActiveNote(t *testing.T) {
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.Public, true, "Public 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 := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
n1UUID := "n1-uuid"
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", n1UUID, "b1-uuid", "n1 content", 1542058875, 1542058876, 1, true, true, true)
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)
@ -292,11 +281,10 @@ func TestGetActiveNote(t *testing.T) {
func TestUpdateNoteContent(t *testing.T) {
// set up
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
uuid := "n1-uuid"
MustExec(t, "inserting n1", db, "INSERT INTO notes (uuid, book_uuid, body, added_on, edited_on, usn, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", uuid, "b1-uuid", "n1 content", 1542058875, 0, 1, false, false, false)
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)
@ -324,8 +312,7 @@ func TestUpdateNoteContent(t *testing.T) {
func TestUpdateNoteBook(t *testing.T) {
// set up
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
db := InitTestMemoryDB(t)
b1UUID := "b1-uuid"
b2UUID := "b2-uuid"
@ -333,7 +320,7 @@ func TestUpdateNoteBook(t *testing.T) {
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, public, deleted, dirty) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", uuid, b1UUID, "n1 content", 1542058875, 0, 1, false, false, false)
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)
@ -361,8 +348,7 @@ func TestUpdateNoteBook(t *testing.T) {
func TestUpdateBookName(t *testing.T) {
// set up
db := InitTestDB(t, "../tmp/dnote-test.db", nil)
defer TeardownTestDB(t, db)
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)

View file

@ -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);

View file

@ -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
}

View file

@ -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")
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,27 +1,24 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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"
"os"
"path/filepath"
"testing"
@ -30,56 +27,13 @@ import (
"github.com/pkg/errors"
)
var defaultSchemaSQL = `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);`
//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{}) {
@ -99,29 +53,48 @@ func MustExec(t *testing.T, message string, db *DB, query string, args ...interf
return result
}
// TestDBOptions contains options for test database
type TestDBOptions struct {
SchemaSQLPath string
SkipMigration bool
// InitTestMemoryDB initializes an in-memory test database with the default schema.
func InitTestMemoryDB(t *testing.T) *DB {
return InitTestMemoryDBRaw(t, "")
}
// InitTestDB initializes a test database and opens connection to it
func InitTestDB(t *testing.T, dbPath string, options *TestDBOptions) *DB {
// 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 connection"))
t.Fatal(errors.Wrap(err, "opening database"))
}
dir, _ := filepath.Split(dbPath)
err = os.MkdirAll(dir, 0777)
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, "creating the directory for test database file"))
t.Fatal(errors.Wrap(err, "opening in-memory database"))
}
var schemaSQL string
if options != nil && options.SchemaSQLPath != "" {
b := utils.ReadFileAbs(options.SchemaSQLPath)
schemaSQL = string(b)
if schemaPath != "" {
schemaSQL = string(utils.ReadFileAbs(schemaPath))
} else {
schemaSQL = defaultSchemaSQL
}
@ -130,24 +103,10 @@ func InitTestDB(t *testing.T, dbPath string, options *TestDBOptions) *DB {
t.Fatal(errors.Wrap(err, "running schema sql"))
}
if options == nil || !options.SkipMigration {
MarkMigrationComplete(t, db)
}
t.Cleanup(func() { db.Close() })
return db
}
// TeardownTestDB closes the test database and removes the its file
func TeardownTestDB(t *testing.T, db *DB) {
if err := db.Close(); err != nil {
t.Fatal(errors.Wrap(err, "closing database"))
}
if err := os.RemoveAll(db.Filepath); err != nil {
t.Fatal(errors.Wrap(err, "removing database file"))
}
}
// OpenTestDB opens the database connection to a test database
// without initializing any schema
func OpenTestDB(t *testing.T, dnoteDir string) *DB {
@ -160,12 +119,11 @@ func OpenTestDB(t *testing.T, dnoteDir string) *DB {
return db
}
// MarkMigrationComplete marks all migrations as complete in the database
func MarkMigrationComplete(t *testing.T, db *DB) {
if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", consts.SystemSchema, 12); err != nil {
t.Fatal(errors.Wrap(err, "inserting schema"))
}
if _, err := db.Exec("INSERT INTO system (key, value) VALUES (? , ?);", consts.SystemRemoteSchema, 1); err != nil {
t.Fatal(errors.Wrap(err, "inserting remote schema"))
// 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
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -24,7 +21,6 @@ import (
"database/sql"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
@ -138,7 +134,7 @@ func Init(versionTag, apiEndpoint, dbPath string) (*context.DnoteCtx, error) {
return nil, errors.Wrap(err, "setting up the context")
}
log.Debug("Running with Dnote context: %+v\n", context.Redact(ctx))
log.Debug("context: %+v\n", context.Redact(ctx))
return &ctx, nil
}
@ -291,7 +287,9 @@ func InitSystem(ctx context.DnoteCtx) error {
return errors.Wrapf(err, "initializing system config for %s", consts.SystemLastSyncAt)
}
tx.Commit()
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "committing transaction")
}
return nil
}
@ -327,36 +325,6 @@ func getEditorCommand() string {
return ret
}
func initDir(path string) error {
ok, err := utils.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 a directory at %s", path)
}
return nil
}
// initDnoteDir initializes missing directories that Dnote uses
func initDnoteDir(ctx context.DnoteCtx) error {
if err := initDir(filepath.Join(ctx.Paths.Config, consts.DnoteDirName)); err != nil {
return errors.Wrap(err, "initializing config dir")
}
if err := initDir(filepath.Join(ctx.Paths.Data, consts.DnoteDirName)); err != nil {
return errors.Wrap(err, "initializing data dir")
}
if err := initDir(filepath.Join(ctx.Paths.Cache, consts.DnoteDirName)); err != nil {
return errors.Wrap(err, "initializing cache dir")
}
return nil
}
// initConfigFile populates a new config file if it does not exist yet
func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error {
@ -392,7 +360,7 @@ func initConfigFile(ctx context.DnoteCtx, apiEndpoint string) error {
// initFiles creates, if necessary, the dnote directory and files inside
func initFiles(ctx context.DnoteCtx, apiEndpoint string) error {
if err := initDnoteDir(ctx); err != nil {
if err := context.InitDnoteDirs(ctx.Paths); err != nil {
return errors.Wrap(err, "creating the dnote dir")
}
if err := initConfigFile(ctx, apiEndpoint); err != nil {

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -26,13 +23,13 @@ import (
"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.InitTestDB(t, "../tmp/dnote-test.db", nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
var originalCount int
database.MustScan(t, "counting system configs", db.QueryRow("SELECT count(*) FROM system"), &originalCount)
@ -63,8 +60,7 @@ func TestInitSystemKV(t *testing.T) {
func TestInitSystemKV_existing(t *testing.T) {
// Setup
db := database.InitTestDB(t, "../tmp/dnote-test.db", nil)
defer database.TeardownTestDB(t, db)
db := database.InitTestMemoryDB(t)
database.MustExec(t, "inserting a system config", db, "INSERT INTO system (key, value) VALUES (?, ?)", "testKey", "testVal")
@ -108,6 +104,9 @@ func TestInit_APIEndpoint(t *testing.T) {
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 {

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -25,6 +22,11 @@ import (
"github.com/fatih/color"
)
const (
debugEnvName = "DNOTE_DEBUG"
debugEnvValue = "1"
)
var (
// ColorRed is a red foreground color
ColorRed = color.New(color.FgRed)
@ -105,9 +107,21 @@ func Askf(msg string, masked bool, v ...interface{}) {
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()
}
}

View file

@ -1,25 +1,23 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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"
@ -28,12 +26,10 @@ import (
// commands
"github.com/dnote/dnote/pkg/cli/cmd/add"
"github.com/dnote/dnote/pkg/cli/cmd/cat"
"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/ls"
"github.com/dnote/dnote/pkg/cli/cmd/remove"
"github.com/dnote/dnote/pkg/cli/cmd/root"
"github.com/dnote/dnote/pkg/cli/cmd/sync"
@ -45,10 +41,29 @@ import (
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
root.GetRoot().ParseFlags(os.Args[1:])
dbPath := root.GetDBPathFlag()
// 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)
@ -62,10 +77,8 @@ func main() {
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))

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -23,6 +20,7 @@ import (
"log"
"os"
"os/exec"
"strings"
"testing"
"github.com/dnote/dnote/pkg/assert"
@ -35,14 +33,17 @@ import (
var binaryName = "test-dnote"
var testDir = "./tmp/.dnote"
var 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),
},
// 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) {
@ -55,10 +56,11 @@ func TestMain(m *testing.M) {
}
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")
defer testutils.RemoveDir(t, testDir)
db := database.OpenTestDB(t, testDir)
@ -107,12 +109,12 @@ func TestInit(t *testing.T) {
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")
defer testutils.RemoveDir(t, testDir)
db := database.OpenTestDB(t, testDir)
// Test
@ -138,13 +140,14 @@ func TestAddNote(t *testing.T) {
})
t.Run("existing book", func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup3(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "add", "js", "-c", "foo")
defer testutils.RemoveDir(t, testDir)
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "add", "js", "-c", "foo")
// Test
@ -179,13 +182,14 @@ func TestAddNote(t *testing.T) {
func TestEditNote(t *testing.T) {
t.Run("content flag", func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup4(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "edit", "2", "-c", "foo bar")
defer testutils.RemoveDir(t, testDir)
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-c", "foo bar")
// Test
var noteCount, bookCount int
@ -212,13 +216,14 @@ func TestEditNote(t *testing.T) {
})
t.Run("book flag", func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup5(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "edit", "2", "-b", "linux")
defer testutils.RemoveDir(t, testDir)
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-b", "linux")
// Test
var noteCount, bookCount int
@ -246,13 +251,14 @@ func TestEditNote(t *testing.T) {
})
t.Run("book flag and content flag", func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup5(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "edit", "2", "-b", "linux", "-c", "n2 body updated")
defer testutils.RemoveDir(t, testDir)
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "2", "-b", "linux", "-c", "n2 body updated")
// Test
var noteCount, bookCount int
@ -282,13 +288,14 @@ func TestEditNote(t *testing.T) {
func TestEditBook(t *testing.T) {
t.Run("name flag", func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup1(t, db)
// Execute
testutils.RunDnoteCmd(t, opts, binaryName, "edit", "js", "-n", "js-edited")
defer testutils.RemoveDir(t, testDir)
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "edit", "js", "-n", "js-edited")
// Test
var noteCount, bookCount int
@ -341,17 +348,18 @@ func TestRemoveNote(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup2(t, db)
// Execute
if tc.yesFlag {
testutils.RunDnoteCmd(t, opts, binaryName, "remove", "-y", "1")
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "remove", "-y", "1")
} else {
testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveNote, binaryName, "remove", "1")
testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveNote, binaryName, "--dbPath", dbPath, "remove", "1")
}
defer testutils.RemoveDir(t, testDir)
// Test
var noteCount, bookCount, jsNoteCount, linuxNoteCount int
@ -428,19 +436,19 @@ func TestRemoveBook(t *testing.T) {
for _, tc := range testCases {
t.Run(fmt.Sprintf("--yes=%t", tc.yesFlag), func(t *testing.T) {
_, opts := setupTestEnv(t)
// Setup
db := database.InitTestDB(t, fmt.Sprintf("%s/%s/%s", testDir, consts.DnoteDirName, consts.DnoteDBFileName), nil)
db, dbPath := database.InitTestFileDB(t)
testutils.Setup2(t, db)
// Execute
if tc.yesFlag {
testutils.RunDnoteCmd(t, opts, binaryName, "remove", "-y", "js")
testutils.RunDnoteCmd(t, opts, binaryName, "--dbPath", dbPath, "remove", "-y", "js")
} else {
testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveBook, binaryName, "remove", "js")
testutils.MustWaitDnoteCmd(t, opts, testutils.ConfirmRemoveBook, binaryName, "--dbPath", dbPath, "remove", "js")
}
defer testutils.RemoveDir(t, testDir)
// Test
var noteCount, bookCount, jsNoteCount, linuxNoteCount int
database.MustScan(t, "counting books", db.QueryRow("SELECT count(*) FROM books"), &bookCount)
@ -537,17 +545,9 @@ func TestDBPathFlag(t *testing.T) {
}
// Setup - use two different custom database paths
customDBPath1 := "./tmp/custom-test1.db"
customDBPath2 := "./tmp/custom-test2.db"
defer testutils.RemoveDir(t, "./tmp")
customOpts := 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),
},
}
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")
@ -569,3 +569,65 @@ func TestDBPathFlag(t *testing.T) {
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")
})
}

View file

@ -38,13 +38,5 @@ 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);

View file

@ -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);

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -353,14 +350,18 @@ func TestMigrateToV7(t *testing.T) {
}
func TestMigrateToV8(t *testing.T) {
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true}
db := database.InitTestDB(t, "../tmp/.dnote/dnote-test.db", &opts)
defer database.TeardownTestDB(t, db)
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: "../tmp",
LegacyDnote: "../tmp/.dnote",
Home: tmpDir,
LegacyDnote: dnoteDir,
},
DB: db,
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -49,6 +46,7 @@ var LocalSequence = []migration{
lm11,
lm12,
lm13,
lm14,
}
// RemoteSequence is a list of remote migrations to be run
@ -143,7 +141,7 @@ func Run(ctx context.DnoteCtx, migrations []migration, mode int) error {
return errors.Wrap(err, "getting the current schema")
}
log.Debug("current schema: %s %d of %d\n", consts.SystemSchema, schema, len(migrations))
log.Debug("%s: %d of %d\n", schemaKey, schema, len(migrations))
toRun := migrations[schema:]

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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,8 +19,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"net/http/httptest"
"os"
"testing"
"time"
@ -38,11 +35,13 @@ import (
"github.com/pkg/errors"
)
var paths context.Paths = context.Paths{
Home: "../../tmp",
Cache: "../../tmp",
Config: "../../tmp",
Data: "../../tmp",
// 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) {
@ -60,11 +59,8 @@ func TestExecute_bump_schema(t *testing.T) {
for _, tc := range testCases {
func() {
// set up
opts := database.TestDBOptions{SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
db := initTestDBNoMigration(t)
ctx := context.InitTestCtxWithDB(t, db)
database.MustExec(t, "inserting a schema", db, "INSERT INTO system (key, value) VALUES (?, ?)", tc.schemaKey, 8)
@ -117,11 +113,8 @@ func TestRun_nonfresh(t *testing.T) {
for _, tc := range testCases {
func() {
// set up
opts := database.TestDBOptions{SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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 )")
@ -197,11 +190,8 @@ func TestRun_fresh(t *testing.T) {
for _, tc := range testCases {
func() {
// set up
opts := database.TestDBOptions{SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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 )")
@ -271,11 +261,8 @@ func TestRun_up_to_date(t *testing.T) {
for _, tc := range testCases {
func() {
// set up
opts := database.TestDBOptions{SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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 )")
@ -326,11 +313,8 @@ func TestRun_up_to_date(t *testing.T) {
func TestLocalMigration1(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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,
@ -404,11 +388,8 @@ func TestLocalMigration1(t *testing.T) {
func TestLocalMigration2(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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"
@ -491,11 +472,8 @@ func TestLocalMigration2(t *testing.T) {
func TestLocalMigration3(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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,
@ -566,11 +544,8 @@ func TestLocalMigration3(t *testing.T) {
func TestLocalMigration4(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-1-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -610,11 +585,8 @@ func TestLocalMigration4(t *testing.T) {
func TestLocalMigration5(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-5-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -672,11 +644,8 @@ func TestLocalMigration5(t *testing.T) {
func TestLocalMigration6(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-5-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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)
@ -705,11 +674,8 @@ func TestLocalMigration6(t *testing.T) {
func TestLocalMigration7_trash(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-7-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -738,11 +704,8 @@ func TestLocalMigration7_trash(t *testing.T) {
func TestLocalMigration7_conflicts(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-7-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -771,11 +734,8 @@ func TestLocalMigration7_conflicts(t *testing.T) {
func TestLocalMigration7_conflicts_dup(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-7-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -809,11 +769,8 @@ func TestLocalMigration7_conflicts_dup(t *testing.T) {
func TestLocalMigration8(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-8-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -846,13 +803,13 @@ func TestLocalMigration8(t *testing.T) {
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)
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)
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")
@ -875,11 +832,8 @@ func TestLocalMigration8(t *testing.T) {
func TestLocalMigration9(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-9-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -921,11 +875,8 @@ func TestLocalMigration9(t *testing.T) {
func TestLocalMigration10(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-10-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -993,11 +944,8 @@ func TestLocalMigration10(t *testing.T) {
func TestLocalMigration11(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-11-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := ctx.DB
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")
@ -1073,9 +1021,8 @@ func TestLocalMigration11(t *testing.T) {
func TestLocalMigration12(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-12-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
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)
@ -1110,9 +1057,8 @@ func TestLocalMigration12(t *testing.T) {
func TestLocalMigration13(t *testing.T) {
// set up
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/local-12-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := database.InitTestMemoryDBRaw(t, "./fixtures/local-12-pre-schema.sql")
ctx := context.InitTestCtxWithDB(t, db)
data := []byte("editor: vim\napiEndpoint: https://test.com/api")
@ -1150,11 +1096,57 @@ func TestLocalMigration13(t *testing.T) {
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
opts := database.TestDBOptions{SchemaSQLPath: "./fixtures/remote-1-pre-schema.sql", SkipMigration: true}
ctx := context.InitTestCtx(t, paths, &opts)
defer context.TeardownTestCtx(t, ctx)
db := database.InitTestMemoryDBRaw(t, "./fixtures/remote-1-pre-schema.sql")
ctx := context.InitTestCtxWithDB(t, db)
testutils.Login(t, &ctx)
JSBookUUID := "existing-js-book-uuid"
@ -1194,7 +1186,6 @@ func TestRemoteMigration1(t *testing.T) {
ctx.APIEndpoint = server.URL
db := ctx.DB
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")

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -572,6 +569,18 @@ var lm13 = migration{
},
}
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 context.DnoteCtx, tx *database.DB) error {

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -22,6 +19,7 @@ package output
import (
"fmt"
"io"
"time"
"github.com/dnote/dnote/pkg/cli/database"
@ -29,7 +27,7 @@ import (
)
// NoteInfo prints a note information
func NoteInfo(info database.NoteInfo) {
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 {
@ -38,13 +36,13 @@ func NoteInfo(info database.NoteInfo) {
log.Infof("note id: %d\n", info.RowID)
log.Infof("note uuid: %s\n", info.UUID)
fmt.Printf("\n------------------------content------------------------\n")
fmt.Printf("%s", info.Content)
fmt.Printf("\n-------------------------------------------------------\n")
fmt.Fprintf(w, "\n------------------------content------------------------\n")
fmt.Fprintf(w, "%s", info.Content)
fmt.Fprintf(w, "\n-------------------------------------------------------\n")
}
func NoteContent(info database.NoteInfo) {
fmt.Printf("%s", info.Content)
func NoteContent(w io.Writer, info database.NoteInfo) {
fmt.Fprintf(w, "%s", info.Content)
}
// BookInfo prints a note information

View file

@ -1,26 +1,22 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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 (
"bufio"
"bytes"
"encoding/json"
"io"
@ -31,6 +27,7 @@ import (
"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"
@ -48,12 +45,15 @@ const (
// Timeout for waiting for prompts in tests
const promptTimeout = 10 * time.Second
// Login simulates a logged in user by inserting credentials in the local database
func Login(t *testing.T, ctx *context.DnoteCtx) {
db := ctx.DB
// 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()
@ -144,7 +144,7 @@ type RunDnoteCmdOptions struct {
}
// RunDnoteCmd runs a dnote command
func RunDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, binaryName string, arg ...string) {
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...)
@ -162,6 +162,8 @@ func RunDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, binaryName string, arg .
// 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.
@ -223,103 +225,32 @@ func MustWaitDnoteCmd(t *testing.T, opts RunDnoteCmdOptions, runFunc func(io.Rea
return output
}
// 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)
}
}
// 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 := waitForPrompt(stdout, expectedPrompt, promptTimeout); err != nil {
if err := assert.WaitForPrompt(stdout, expectedPrompt, promptTimeout); err != nil {
t.Fatal(err)
}
}
// userRespondToPrompt is a helper that waits for a prompt and sends a response.
func userRespondToPrompt(stdout io.Reader, stdin io.WriteCloser, expectedPrompt, response, action string) error {
if err := waitForPrompt(stdout, expectedPrompt, promptTimeout); err != nil {
return err
}
if _, err := io.WriteString(stdin, response); err != nil {
return errors.Wrapf(err, "indicating %s in stdin", action)
}
return nil
}
// userConfirmOutput simulates confirmation from the user by writing to stdin.
// It waits for the expected prompt with a timeout to prevent deadlocks.
func userConfirmOutput(stdout io.Reader, stdin io.WriteCloser, expectedPrompt string) error {
return userRespondToPrompt(stdout, stdin, expectedPrompt, "y\n", "confirmation")
}
// userCancelOutput simulates cancellation from the user by writing to stdin.
// It waits for the expected prompt with a timeout to prevent deadlocks.
func userCancelOutput(stdout io.Reader, stdin io.WriteCloser, expectedPrompt string) error {
return userRespondToPrompt(stdout, stdin, expectedPrompt, "n\n", "cancellation")
}
// ConfirmRemoveNote waits for prompt for removing a note and confirms.
func ConfirmRemoveNote(stdout io.Reader, stdin io.WriteCloser) error {
return userConfirmOutput(stdout, stdin, PromptRemoveNote)
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 userConfirmOutput(stdout, stdin, PromptDeleteBook)
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 userConfirmOutput(stdout, stdin, PromptEmptyServer)
return assert.RespondToPrompt(stdout, stdin, PromptEmptyServer, "y\n", promptTimeout)
}
// UserCancelEmptyServerSync waits for an empty server prompt and confirms.
// UserCancelEmptyServerSync waits for an empty server prompt and cancels.
func UserCancelEmptyServerSync(stdout io.Reader, stdin io.WriteCloser) error {
return userCancelOutput(stdout, stdin, PromptEmptyServer)
return assert.RespondToPrompt(stdout, stdin, PromptEmptyServer, "n\n", promptTimeout)
}
// UserContent simulates content from the user by writing to stdin.
@ -368,3 +299,12 @@ func MustGenerateUUID(t *testing.T) string {
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
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -30,11 +27,7 @@ import (
func TestGetTmpContentPath(t *testing.T) {
t.Run("no collision", func(t *testing.T) {
ctx := context.InitTestCtx(t, context.Paths{
Data: "../tmp",
Cache: "../tmp",
}, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
res, err := GetTmpContentPath(ctx)
if err != nil {
@ -47,11 +40,7 @@ func TestGetTmpContentPath(t *testing.T) {
t.Run("one existing session", func(t *testing.T) {
// set up
ctx := context.InitTestCtx(t, context.Paths{
Data: "../tmp2",
Cache: "../tmp2",
}, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
p := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md")
if _, err := os.Create(p); err != nil {
@ -71,11 +60,7 @@ func TestGetTmpContentPath(t *testing.T) {
t.Run("two existing sessions", func(t *testing.T) {
// set up
ctx := context.InitTestCtx(t, context.Paths{
Data: "../tmp3",
Cache: "../tmp3",
}, nil)
defer context.TeardownTestCtx(t, ctx)
ctx := context.InitTestCtx(t)
p1 := fmt.Sprintf("%s/%s", ctx.Paths.Cache, "DNOTE_TMPCONTENT_0.md")
if _, err := os.Create(p1); err != nil {

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -26,6 +23,7 @@ import (
"syscall"
"github.com/dnote/dnote/pkg/cli/log"
"github.com/dnote/dnote/pkg/prompt"
"github.com/pkg/errors"
"golang.org/x/crypto/ssh/terminal"
)
@ -73,26 +71,16 @@ func PromptPassword(message string, dest *string) error {
// Confirm prompts for user input to confirm a choice
func Confirm(question string, optimistic bool) (bool, error) {
var choices string
if optimistic {
choices = "(Y/n)"
} else {
choices = "(y/N)"
}
message := prompt.FormatQuestion(question, optimistic)
message := fmt.Sprintf("%s %s", question, choices)
// Use log.Askf for colored prompt in CLI
log.Askf(message, false)
var input string
if err := PromptInput(message, &input); err != nil {
confirmed, err := prompt.ReadYesNo(os.Stdin, optimistic)
if err != nil {
return false, errors.Wrap(err, "Failed to get user input")
}
confirmed := input == "y"
if optimistic {
confirmed = confirmed || input == ""
}
return confirmed, nil
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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
@ -55,6 +52,24 @@ func FileExists(filepath string) (bool, error) {
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 {

View file

@ -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")
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,30 +1,26 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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.
type Clock interface {
@ -39,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
}

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,19 +1,16 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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

View file

@ -1,24 +1,22 @@
/* Copyright (C) 2019, 2020, 2021, 2022, 2023, 2024, 2025 Dnote contributors
/* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dnote. If not, see <https://www.gnu.org/licenses/>.
* 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"
@ -49,11 +47,9 @@ func TestServerStart(t *testing.T) {
port := "13456" // Use different port to avoid conflicts with main test server
// Start server in background
cmd := exec.Command(testServerBinary, "start", "-port", port)
cmd := exec.Command(testServerBinary, "start", "--port", port)
cmd.Env = append(os.Environ(),
"DBPath="+tmpDB,
"WebURL=http://localhost:"+port,
"APP_ENV=PRODUCTION",
)
if err := cmd.Start(); err != nil {
@ -142,17 +138,16 @@ func TestServerStartHelp(t *testing.T) {
outputStr := string(output)
assert.Equal(t, strings.Contains(outputStr, "dnote-server start [flags]"), true, "output should contain usage")
assert.Equal(t, strings.Contains(outputStr, "-appEnv"), true, "output should contain appEnv flag")
assert.Equal(t, strings.Contains(outputStr, "-port"), true, "output should contain port flag")
assert.Equal(t, strings.Contains(outputStr, "-webUrl"), true, "output should contain webUrl 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")
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 WebURL to trigger validation failure
cmd.Env = []string{"WebURL=not-a-valid-url"}
// Set invalid BaseURL to trigger validation failure
cmd.Env = []string{"BaseURL=not-a-valid-url"}
output, err := cmd.CombinedOutput()
@ -163,9 +158,9 @@ func TestServerStartInvalidConfig(t *testing.T) {
outputStr := string(output)
assert.Equal(t, strings.Contains(outputStr, "Error:"), true, "output should contain error message")
assert.Equal(t, strings.Contains(outputStr, "Invalid WebURL"), true, "output should mention invalid WebURL")
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, "-webUrl"), true, "output should show flags")
assert.Equal(t, strings.Contains(outputStr, "--baseUrl"), true, "output should show flags")
}
func TestServerUnknownCommand(t *testing.T) {
@ -181,3 +176,178 @@ func TestServerUnknownCommand(t *testing.T) {
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")
}

File diff suppressed because it is too large Load diff

View file

@ -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")
}

View file

@ -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"), &note.USN, &note.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")
})
}

54
pkg/e2e/sync/main_test.go Normal file
View file

@ -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())
}

295
pkg/e2e/sync/testutils.go Normal file
View file

@ -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")
}

Some files were not shown because too many files have changed in this diff Show more