Compare commits

..

No commits in common. "21c2bfd6300f65f1c0395d06f0f1045b3cf266d0" and "17878b2f32baabf8db41249ad42725f7eb48d695" have entirely different histories.

4 changed files with 9 additions and 178 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
.env

View File

@ -1,132 +0,0 @@
package sn_test
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"testing"
sn "github.com/ekzyis/snappy"
)
var (
c = testClient()
)
func TestQueryItems(t *testing.T) {
var (
cursor *sn.ItemsCursor
err error
)
if cursor, err = c.Items(nil); err != nil {
t.Error(err)
return
}
if len(cursor.Items) == 0 {
t.Error("items cursor empty")
return
}
}
func TestMutationCreateComment(t *testing.T) {
var (
parentId = 349
text = "test comment"
err error
)
// TODO: return result, invoice, paymentMethod from CreateComment and run assertions on that
if _, err = c.CreateComment(parentId, text); err != nil {
t.Error(err)
return
}
}
func TestMutationPostDiscussion(t *testing.T) {
var (
title = "test discussion"
text = "test discussion text"
sub = "bitcoin"
err error
)
// TODO: return result, invoice, paymentMethod from CreateComment and run assertions on that
if _, err = c.PostDiscussion(title, text, sub); err != nil {
t.Error(err)
return
}
}
func TestMutationPostLink(t *testing.T) {
var (
url = "https://stacker.news"
title = "test discussion"
text = "test discussion text"
sub = "bitcoin"
err error
)
// TODO: return result, invoice, paymentMethod from CreateComment and run assertions on that
if _, err = c.PostLink(url, title, text, sub); err != nil {
t.Error(err)
return
}
}
func testClient() *sn.Client {
loadEnv()
baseUrl, set := os.LookupEnv("TEST_SN_BASE_URL")
if !set {
baseUrl = "http://localhost:3000"
}
log.Printf("baseUrl=%s\n", baseUrl)
apiKey, set := os.LookupEnv("TEST_SN_API_KEY")
if !set {
log.Fatalf("TEST_SN_API_KEY is not set")
}
log.Printf("apiKey=%s\n", apiKey)
return sn.NewClient(
sn.WithBaseUrl(baseUrl),
sn.WithApiKey(apiKey),
)
}
func loadEnv() {
var (
f *os.File
s *bufio.Scanner
err error
)
if f, err = os.Open(".env"); err != nil {
log.Fatalf("error opening .env: %v", err)
}
defer f.Close()
s = bufio.NewScanner(f)
s.Split(bufio.ScanLines)
for s.Scan() {
line := s.Text()
parts := strings.SplitN(line, "=", 2)
// Check if we have exactly 2 parts (key and value)
if len(parts) == 2 {
os.Setenv(parts[0], parts[1])
} else {
log.Fatalf(".env: invalid line: %s\n", line)
}
}
// Check for errors during scanning
if err = s.Err(); err != nil {
fmt.Println("error scanning .env:", err)
}
}

View File

@ -1,30 +0,0 @@
package sn
import "time"
type Invoice struct {
Id int `json:"id,string"`
Hash string `json:"hash"`
Hmac string `json:"hmac"`
Bolt11 string `json:"bolt11"`
SatsRequested int `json:"satsRequested"`
SatsReceived int `json:"satsReceived"`
Cancelled bool `json:"cancelled"`
ConfirmedAt time.Time `json:"createdAt"`
ExpiresAt time.Time `json:"expiresAt"`
Nostr map[string]interface{} `json:"nostr"`
IsHeld bool `json:"isHeld"`
Comment string `json:"comment"`
Lud18Data map[string]interface{} `json:"lud18Data"`
ConfirmedPreimage string `json:"confirmedPreimage"`
ActionState string `json:"actionState"`
ActionType string `json:"actionType"`
}
type PaymentMethod string
const (
PaymentMethodFeeCredits PaymentMethod = "FEE_CREDIT"
PaymentMethodOptimistic PaymentMethod = "OPTIMISTIC"
PaymentMethodPessimistic PaymentMethod = "PESSIMISTIC"
)

View File

@ -50,30 +50,24 @@ type ItemsResponse struct {
} `json:"data"` } `json:"data"`
} }
type ItemPaidAction struct {
Result Item `json:"result"`
Invoice Invoice `json:"invoice"`
PaymentMethod PaymentMethod `json:"paymentMethod"`
}
type UpsertDiscussionResponse struct { type UpsertDiscussionResponse struct {
Errors []GqlError `json:"errors"` Errors []GqlError `json:"errors"`
Data struct { Data struct {
UpsertDiscussion ItemPaidAction `json:"upsertDiscussion"` UpsertDiscussion Item `json:"upsertDiscussion"`
} `json:"data"` } `json:"data"`
} }
type UpsertLinkResponse struct { type UpsertLinkResponse struct {
Errors []GqlError `json:"errors"` Errors []GqlError `json:"errors"`
Data struct { Data struct {
UpsertLink ItemPaidAction `json:"upsertLink"` UpsertLink Item `json:"upsertLink"`
} `json:"data"` } `json:"data"`
} }
type CreateCommentsResponse struct { type CreateCommentsResponse struct {
Errors []GqlError `json:"errors"` Errors []GqlError `json:"errors"`
Data struct { Data struct {
CreateComment ItemPaidAction `json:"createComment"` CreateComment Comment `json:"createComment"`
} `json:"data"` } `json:"data"`
} }
@ -195,7 +189,7 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
Query: ` Query: `
mutation upsertDiscussion($title: String!, $text: String, $sub: String) { mutation upsertDiscussion($title: String!, $text: String, $sub: String) {
upsertDiscussion(title: $title, text: $text, sub: $sub) { upsertDiscussion(title: $title, text: $text, sub: $sub) {
result { id } id
} }
}`, }`,
Variables: map[string]interface{}{ Variables: map[string]interface{}{
@ -223,7 +217,7 @@ func (c *Client) PostDiscussion(title string, text string, sub string) (int, err
return -1, err return -1, err
} }
return respBody.Data.UpsertDiscussion.Result.Id, nil return respBody.Data.UpsertDiscussion.Id, nil
} }
func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) { func (c *Client) PostLink(url string, title string, text string, sub string) (int, error) {
@ -231,7 +225,7 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
Query: ` Query: `
mutation upsertLink($url: String!, $title: String!, $text: String, $sub: String!) { mutation upsertLink($url: String!, $title: String!, $text: String, $sub: String!) {
upsertLink(url: $url, title: $title, text: $text, sub: $sub) { upsertLink(url: $url, title: $title, text: $text, sub: $sub) {
result { id } id
} }
}`, }`,
Variables: map[string]interface{}{ Variables: map[string]interface{}{
@ -260,7 +254,7 @@ func (c *Client) PostLink(url string, title string, text string, sub string) (in
return -1, err return -1, err
} }
return respBody.Data.UpsertLink.Result.Id, nil return respBody.Data.UpsertLink.Id, nil
} }
func (c *Client) CreateComment(parentId int, text string) (int, error) { func (c *Client) CreateComment(parentId int, text string) (int, error) {
@ -268,7 +262,7 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
Query: ` Query: `
mutation upsertComment($parentId: ID!, $text: String!) { mutation upsertComment($parentId: ID!, $text: String!) {
upsertComment(parentId: $parentId, text: $text) { upsertComment(parentId: $parentId, text: $text) {
result { id } id
} }
}`, }`,
Variables: map[string]interface{}{ Variables: map[string]interface{}{
@ -295,7 +289,7 @@ func (c *Client) CreateComment(parentId int, text string) (int, error) {
return -1, err return -1, err
} }
return respBody.Data.CreateComment.Result.Id, nil return respBody.Data.CreateComment.Id, nil
} }
func (c *Client) Dupes(url string) (*[]Dupe, error) { func (c *Client) Dupes(url string) (*[]Dupe, error) {