| 1 |
package io.oxygenhub.android.api |
| 2 |
|
| 3 |
import io.oxygenhub.android.Fixtures |
| 4 |
import org.junit.Assert.assertEquals |
| 5 |
import org.junit.Assert.assertFalse |
| 6 |
import org.junit.Assert.assertNull |
| 7 |
import org.junit.Assert.assertTrue |
| 8 |
import org.junit.Test |
| 9 |
|
| 10 |
class ModelsTest { |
| 11 |
@Test |
| 12 |
fun proposalsAsTheHubServesThem() { |
| 13 |
val ps = Fixtures.proposals |
| 14 |
val landed = ps.first { it.number == 264L } |
| 15 |
assertEquals("landed", landed.state) |
| 16 |
assertEquals("andymccall", landed.landed?.by) |
| 17 |
assertEquals("Cancel a landed proposal's runs that have not started", landed.changes.single().subject) |
| 18 |
val blocked = ps.first { it.number == 301L } |
| 19 |
assertEquals(listOf("0 of 1 required sealed approvals", "required checks not passed on the projected state: test"), blocked.blockers) |
| 20 |
assertEquals("andymccall", blocked.autoLand) |
| 21 |
assertEquals(listOf("flow/build", "flow/clippy", "flow/fmt", "flow/test", "flow/test-postgres"), Fixtures.checks.map { it.name }) |
| 22 |
assertEquals(null, Fixtures.checks[2].finishedMs) |
| 23 |
} |
| 24 |
|
| 25 |
@Test |
| 26 |
fun user() { |
| 27 |
val u = Fixtures.user |
| 28 |
assertEquals("andymccall", u.username) |
| 29 |
assertEquals("Andy McCall", u.displayName) |
| 30 |
assertEquals("Andy McCall", u.shownName) |
| 31 |
assertTrue(u.isAdmin) |
| 32 |
assertEquals(1, u.keys.size) |
| 33 |
assertEquals(1790530000000, u.createdMs) |
| 34 |
} |
| 35 |
|
| 36 |
@Test |
| 37 |
fun shownNameFallsBackToUsername() { |
| 38 |
assertEquals("claude", User(username = "claude").shownName) |
| 39 |
} |
| 40 |
|
| 41 |
@Test |
| 42 |
fun repositoriesWithEveryVisibilityAndAnUnknownField() { |
| 43 |
val r = Fixtures.repos |
| 44 |
assertEquals(3, r.size) |
| 45 |
assertEquals("andymccall/oxygenflow", r[0].slug) |
| 46 |
assertEquals(Visibility.Public, r[0].visibility) |
| 47 |
assertEquals(Visibility.Private, r[1].visibility) |
| 48 |
assertEquals(Visibility.Internal, r[2].visibility) |
| 49 |
assertEquals("trunk", r[2].defaultBookmark) |
| 50 |
assertEquals(1792878439999, r[0].updatedMs) |
| 51 |
assertEquals("https://oxygenhub.io/andymccall/oxygenflow", r[0].cloneUrl) |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
fun issues() { |
| 56 |
val i = Fixtures.issues |
| 57 |
assertEquals(listOf(3L, 2L, 1L), i.map { it.number }) |
| 58 |
val two = i[1] |
| 59 |
assertTrue(two.isOpen) |
| 60 |
assertTrue(two.pinned) |
| 61 |
assertEquals("0.1", two.milestone) |
| 62 |
assertEquals(listOf("claude"), two.assignees) |
| 63 |
assertEquals(Label("feature", "#0064e0", "Something new"), two.labels.single()) |
| 64 |
assertNull(two.closedMs) |
| 65 |
val one = i[2] |
| 66 |
assertFalse(one.isOpen) |
| 67 |
assertEquals("completed", one.reason) |
| 68 |
assertEquals(1792875000000, one.closedMs) |
| 69 |
assertNull(one.milestone) |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
fun inbox() { |
| 74 |
val inbox = Fixtures.inbox |
| 75 |
assertEquals(2, inbox.unread) |
| 76 |
assertEquals(listOf(41L, 40L, 12L), inbox.items.map { it.id }) |
| 77 |
assertEquals("issue.opened", inbox.items[0].kind) |
| 78 |
assertFalse(inbox.items[0].read) |
| 79 |
assertEquals("", inbox.items[1].actor) |
| 80 |
assertTrue(inbox.items[2].read) |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
fun deviceCodeAndToken() { |
| 85 |
val code = HubJson.decodeFromString(DeviceCode.serializer(), Fixtures.text("device_code.json")) |
| 86 |
assertEquals("BCDF-GHJK", code.userCode) |
| 87 |
assertEquals("https://oxygenhub.io/device?code=BCDF-GHJK", code.pageToOpen) |
| 88 |
assertEquals(900, code.expiresIn) |
| 89 |
assertEquals(5, code.interval) |
| 90 |
val grant = HubJson.decodeFromString(TokenGrant.serializer(), Fixtures.text("device_token.json")) |
| 91 |
assertEquals(listOf("repo:read", "user:read"), grant.scopes) |
| 92 |
assertEquals("andymccall", grant.username) |
| 93 |
} |
| 94 |
|
| 95 |
@Test |
| 96 |
fun pageToOpenWithoutTheCompleteUri() { |
| 97 |
val code = DeviceCode("d", "BCDF-GHJK", "https://hub.example/device") |
| 98 |
assertEquals("https://hub.example/device", code.pageToOpen) |
| 99 |
} |
| 100 |
|
| 101 |
@Test |
| 102 |
fun secretsStayOutOfToString() { |
| 103 |
val code = HubJson.decodeFromString(DeviceCode.serializer(), Fixtures.text("device_code.json")) |
| 104 |
assertFalse(code.toString().contains("oxydev_")) |
| 105 |
val grant = HubJson.decodeFromString(TokenGrant.serializer(), Fixtures.text("device_token.json")) |
| 106 |
assertFalse(grant.toString().contains("oxy_test_not_a_real_token")) |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
fun errorDetails() { |
| 111 |
assertEquals("this token lacks the user:read scope", errorDetail(Fixtures.text("problem_403.json"))) |
| 112 |
assertEquals("not yet approved", errorDetail(Fixtures.text("device_authorization_pending.json"))) |
| 113 |
assertEquals("plain text", errorDetail("plain text\n")) |
| 114 |
} |
| 115 |
|
| 116 |
@Test |
| 117 |
fun hubUrls() { |
| 118 |
assertEquals("https://oxygenhub.io", normalizeHubUrl("https://oxygenhub.io/")) |
| 119 |
assertEquals("https://oxygenhub.io", normalizeHubUrl(" oxygenhub.io ")) |
| 120 |
assertEquals("https://hub.example:7343/forge", normalizeHubUrl("hub.example:7343/forge/")) |
| 121 |
assertNull(normalizeHubUrl("http://oxygenhub.io")) |
| 122 |
assertNull(normalizeHubUrl("")) |
| 123 |
assertNull(normalizeHubUrl("https://")) |
| 124 |
assertNull(normalizeHubUrl("https://oxygenhub.io/?x=1")) |
| 125 |
} |
| 126 |
} |