package mock import ( "fmt" "io" "os" "strconv" "strings" "time" "go.jolheiser.com/cabinet/internal/workspace" ) type Mock struct { files map[string]*File tokens map[string]workspace.Token } func New() *Mock { return &Mock{ files: make(map[string]*File), tokens: make(map[string]workspace.Token), } } func (m Mock) List() ([]os.FileInfo, error) { files := make([]os.FileInfo, 0) for _, file := range m.files { files = append(files, file) } return files, nil } func (m Mock) Meta(id string) (workspace.Meta, error) { if file, ok := m.files[id]; ok { return file.Meta, nil } return workspace.Meta{}, fmt.Errorf("no file meta found for %s", id) } func (m Mock) Read(id string) (io.ReadCloser, error) { if file, ok := m.files[id]; ok { return io.NopCloser(strings.NewReader(file.Content)), nil } return nil, fmt.Errorf("no file content found for %s", id) } func (m Mock) Add(r io.Reader, meta workspace.Meta) (string, error) { content, err := io.ReadAll(r) if err != nil { return "", err } id := strconv.Itoa(len(m.files) + 1) m.files[id] = &File{ name: meta.Name, Content: string(content), size: int64(len(content)), modTime: time.Now(), Meta: meta, } return id, nil } func (m Mock) Delete(id string) error { if _, ok := m.files[id]; ok { delete(m.files, id) return nil } return fmt.Errorf("no file found for %s", id) } func (m Mock) IsProtected() (bool, error) { return len(m.tokens) > 0, nil } func (m Mock) Token(key string) (workspace.Token, error) { if token, ok := m.tokens[key]; ok { return token, nil } return workspace.Token{}, fmt.Errorf("no token found for %s", key) } func (m Mock) AddToken(token workspace.Token) error { m.tokens[token.Key] = token return nil } func (m Mock) DeleteTokens(keys ...string) error { for _, key := range keys { delete(m.tokens, key) } return nil }