jsenzyme_test.go raw
1 package enzyme
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 func TestJSSourceDigestClass(t *testing.T) {
9 src := `
10 import { Event, kinds } from 'nostr-tools'
11 import { Pubkey } from '../shared'
12
13 export type NoteType = 'root' | 'reply' | 'quote'
14
15 export class Note {
16 private readonly _event: Event
17 private readonly _mentions: NoteMention[]
18
19 get id(): EventId {
20 return EventId.fromHex(this._event.id)
21 }
22
23 get author(): Pubkey {
24 return Pubkey.fromHex(this._event.pubkey)
25 }
26
27 mentionsUser(pubkey: Pubkey): boolean {
28 return this._mentions.some((m) => m.pubkey.equals(pubkey))
29 }
30
31 static fromEvent(event: Event): Note {
32 return new Note(event, [], [], [])
33 }
34 }
35
36 export function createNote(event: Event): Note {
37 return Note.fromEvent(event)
38 }
39 `
40 js := JSSource{}
41 if !js.CanDigest([]byte(src)) {
42 t.Fatal("expected CanDigest to return true")
43 }
44
45 elements := js.Digest(strings.NewReader(src))
46
47 counts := make(map[string]int)
48 names := make(map[string][]string)
49 for e := range elements {
50 counts[e.Type()]++
51 if v, ok := e.Value().(string); ok && v != "" {
52 names[e.Type()] = append(names[e.Type()], v)
53 }
54 }
55
56 // Should find imports.
57 if counts["import"] < 2 {
58 t.Errorf("expected >=2 imports, got %d", counts["import"])
59 }
60
61 // Should find type declarations (NoteType + Note class).
62 if counts["type"] < 2 {
63 t.Errorf("expected >=2 types, got %d: %v", counts["type"], names["type"])
64 }
65
66 // Should find struct (class).
67 if counts["struct"] < 1 {
68 t.Errorf("expected >=1 struct (class), got %d", counts["struct"])
69 }
70
71 // Should find methods (id, author, mentionsUser, fromEvent).
72 if counts["method"] < 3 {
73 t.Errorf("expected >=3 methods, got %d: %v", counts["method"], names["method"])
74 }
75
76 // Should find fields (_event, _mentions).
77 if counts["field"] < 2 {
78 t.Errorf("expected >=2 fields, got %d: %v", counts["field"], names["field"])
79 }
80
81 // Should find the createNote function.
82 if counts["func"] < 1 {
83 t.Errorf("expected >=1 func, got %d: %v", counts["func"], names["func"])
84 }
85 }
86
87 func TestJSSourceDigestSvelte(t *testing.T) {
88 src := `<script lang="ts">
89 import { onMount } from 'svelte'
90 import NoteCard from './NoteCard.svelte'
91
92 let notes = []
93
94 function handleClick() {
95 console.log("clicked")
96 }
97 </script>
98
99 <div class="feed">
100 {#each notes as note}
101 <NoteCard {note} on:click={handleClick} />
102 {/each}
103 </div>
104 `
105 js := JSSource{}
106 if !js.CanDigest([]byte(src)) {
107 t.Fatal("expected CanDigest to return true for svelte")
108 }
109
110 elements := js.Digest(strings.NewReader(src))
111 counts := make(map[string]int)
112 for e := range elements {
113 counts[e.Type()]++
114 }
115
116 if counts["import"] < 2 {
117 t.Errorf("expected >=2 imports, got %d", counts["import"])
118 }
119 if counts["func"] < 1 {
120 t.Errorf("expected >=1 func, got %d", counts["func"])
121 }
122 }
123