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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,26 @@ jobs:
general_build:
name: General - Build debug
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: writeopia
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready"
--health-interval=10s
--health-timeout=5s
--health-retries=5
env:
isCI: "true"
IN_MEMORY_DATABASE: 'true'
DB_USER: 'user'
WRITEOPIA_FIREBASE_ID: 'id'
WRITEOPIA_CLIENT_BASE_URL: "baseurl"
steps:
- name: Check out code
uses: actions/checkout@v4
Expand All @@ -67,6 +85,18 @@ jobs:
with:
distribution: adopt
java-version: '21'
- name: Wait for Postgres to be healthy
run: |
for i in {1..10}; do
pg_isready -h localhost -p 5432 && echo "Postgres is ready" && break
echo "Waiting for postgres..." && sleep 5
done
- name: Run SQL init script
run: |
sudo apt-get install -y postgresql-client
psql -h localhost -U postgres -d writeopia -f ./docker/postgres/init.sql
env:
PGPASSWORD: postgres
- name: Build debug
run: ./gradlew assembleDebug test --no-daemon
ios_build:
Expand All @@ -87,6 +117,20 @@ jobs:
backend_build_and_test:
name: Backend - build
runs-on: ubuntu-22.04
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: writeopia
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready"
--health-interval=10s
--health-timeout=5s
--health-retries=5
env:
isCI: "true"
IN_MEMORY_DATABASE: 'true'
Expand All @@ -101,6 +145,18 @@ jobs:
with:
distribution: adopt
java-version: '21'
- name: Wait for Postgres to be healthy
run: |
for i in {1..10}; do
pg_isready -h localhost -p 5432 && echo "Postgres is ready" && break
echo "Waiting for postgres..." && sleep 5
done
- name: Run SQL init script
run: |
sudo apt-get install -y postgresql-client
psql -h localhost -U postgres -d writeopia -f ./docker/postgres/init.sql
env:
PGPASSWORD: postgres
- name: Build backend
run: ./gradlew :backend:gateway:test --no-daemon
- name: upload results
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ object DocumentsService {

suspend fun receiveDocuments(
documents: List<Document>,
writeopiaDb: WriteopiaDbBackend
writeopiaDb: WriteopiaDbBackend,
useAi: Boolean
): Boolean {
documents.forEach { document ->
writeopiaDb.saveDocument(document)
}

return sendToAiHub(documents)
return if(useAi) sendToAiHub(documents) else true
}

suspend fun search(query: String, writeopiaDb: WriteopiaDbBackend): ResultData<List<Document>> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.writeopia.sdk.serialization.extensions.toApi
import io.writeopia.sdk.serialization.extensions.toModel
import io.writeopia.sql.WriteopiaDbBackend

fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend) {
fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend, useAi: Boolean) {
route("api/document") {
get("/{id}") {
val id = call.pathParameters["id"]!!
Expand Down Expand Up @@ -74,7 +74,6 @@ fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend) {
}

get("/search") {
println("received search request!")
val query = call.queryParameters["q"]

if (query == null) {
Expand All @@ -93,13 +92,12 @@ fun Routing.documentsRoute(writeopiaDb: WriteopiaDbBackend) {
}

post<List<DocumentApi>> { documentApiList ->
println("Received documents!")

try {
if (documentApiList.isNotEmpty()) {
val addedToHub = DocumentsService.receiveDocuments(
documentApiList.map { it.toModel() },
writeopiaDb
writeopiaDb,
useAi
)

if (addedToHub) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ fun main() {
).start(wait = true)
}

fun Application.module(writeopiaDb: WriteopiaDbBackend= configurePersistence()) {
configureRouting(writeopiaDb)
fun Application.module(
writeopiaDb: WriteopiaDbBackend = configurePersistence(),
useAi: Boolean = System.getenv("WRITEOPIA_USE_AI")?.toBoolean() ?: false
) {
configureRouting(writeopiaDb, useAi)
configureSerialization()
configureEditorSockets()
configureHTTP()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import io.ktor.server.routing.routing
import io.writeopia.api.documents.routing.documentsRoute
import io.writeopia.sql.WriteopiaDbBackend

fun Application.configureRouting(writeopiaDb: WriteopiaDbBackend) {
fun Application.configureRouting(writeopiaDb: WriteopiaDbBackend, useAi: Boolean) {
routing {
documentsRoute(writeopiaDb)
documentsRoute(writeopiaDb, useAi)

get {
call.respondText("Hi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,78 +19,87 @@ import kotlin.test.assertEquals

class ApplicationTest {

val db = configurePersistence()
private val db = configurePersistence()

@Test
@Ignore
fun `it should be possible to save and query document by id`() = testApplication {
application {
module(db)
}

val client = defaultClient()

val documentApi = DocumentApi(
id = "testiaskkakakaka",
title = "Test Note",
userId = "some user",
parentId = "parentIdddd",
isLocked = false,
createdAt = 1000L,
lastUpdatedAt = 2000L
val documentApiList = listOf(
DocumentApi(
id = "testiaskkakakaka",
title = "Test Note",
userId = "some user",
parentId = "parentIdddd",
isLocked = false,
createdAt = 1000L,
lastUpdatedAt = 2000L,
lastSyncedAt = 2000
)
)

val response = client.post("/api/document") {
contentType(ContentType.Application.Json)
setBody(documentApi)
setBody(documentApiList)
}

assertEquals(HttpStatusCode.OK, response.status)

val response1 = client.get("/api/document/${documentApi.id}")
val response1 = client.get("/api/document/${documentApiList.first().id}")

assertEquals(HttpStatusCode.OK, response1.status)
assertEquals(documentApi, response1.body())
assertEquals(documentApiList.first(), response1.body())

db.deleteDocumentById(documentApi.id)
documentApiList.forEach { documentApi ->
db.deleteDocumentById(documentApi.id)
}
}

@Test
@Ignore
fun `it should be possible to save and query documents by parent id`() = testApplication {
application {
module(db)
}

val client = defaultClient()

val documentApi = DocumentApi(
id = "testias",
title = "Test Note",
userId = "some user",
parentId = "parentId",
isLocked = false,
createdAt = 1000L,
lastUpdatedAt = 2000L
val documentApiList = listOf(
DocumentApi(
id = "testiaskkakakaka",
title = "Test Note",
userId = "some user",
parentId = "parentIdddd",
isLocked = false,
createdAt = 1000L,
lastUpdatedAt = 2000L,
lastSyncedAt = 2000
)
)

val response = client.post("/api/document") {
contentType(ContentType.Application.Json)
setBody(documentApi)
setBody(documentApiList)
}

assertEquals(HttpStatusCode.OK, response.status)

val response1 = client.get("/api/document/parent/${documentApi.parentId}")
val response1 = client.get(
"/api/document/parent/${documentApiList.first().parentId}"
)

assertEquals(HttpStatusCode.OK, response1.status)
assertEquals(listOf(documentApi), response1.body())
assertEquals(listOf(documentApiList.first()), response1.body())

db.deleteDocumentById(documentApi.id)
documentApiList.forEach { documentApi ->
db.deleteDocumentById(documentApi.id)
}
}

@Test
@Ignore
fun `it should be possible to save and query ids by parent id`() = testApplication {
application {
module(db)
Expand Down Expand Up @@ -125,7 +134,6 @@ class ApplicationTest {
}

@Test
@Ignore
fun `it should be possible to get diff of folders`() = testApplication {
application {
module(db)
Expand All @@ -140,21 +148,22 @@ class ApplicationTest {
parentId = "parentId",
isLocked = false,
createdAt = 1000L,
lastUpdatedAt = 2000L
lastUpdatedAt = 2000L,
lastSyncedAt = 4000
)

val documentApi2 = documentApi.copy(id = "testias2", lastUpdatedAt = 4000L)

val response = client.post("/api/document") {
contentType(ContentType.Application.Json)
setBody(documentApi)
setBody(listOf(documentApi))
}

assertEquals(HttpStatusCode.OK, response.status)

val response1 = client.post("/api/document") {
contentType(ContentType.Application.Json)
setBody(documentApi2)
setBody(listOf(documentApi2))
}

assertEquals(HttpStatusCode.OK, response1.status)
Expand Down
2 changes: 1 addition & 1 deletion docker/postgres/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
- ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro
- .docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
restart: unless-stopped

volumes:
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ platformtools = "0.2.9"
postgresSocketFactory = "1.15.1"
r2dbcDriver = "2.0.2"
room = "2.7.0-rc03"
agp = "8.9.2"
agp = "8.10.0"
kotlin = "2.1.10"
composeTest = "1.7.8"
webWorkerDriver = "2.0.0"
Expand Down